我正在寻找一种方法来检查 2 个字符串在文件系统路径(目录)方面是否相同。例如,该集合中的所有字符串在文件系统路径方面都是相同的: { /x
, \x
, //x
, /x/
} ,但这两个 -/x
并且/y
不是,即使/y
是到/x
. 我要编写的程序应该适用于 Linux 以及 Windows,所以我正在寻找可移植的解决方案。
编辑:
我只使用 boost 的仅标头库,所以解决方案boost::filesystem
对我来说不合适。我知道UrlCompare
在 Windows API 中有,在 linux 中有类似的东西吗?
让我们来看看这两个功能:
std::string get_string()
{
std::string ret_value;
// Calculate ret_value ...
return ret_value;
}
void process_c_string(const char* s)
{
std::cout << s << endl;
}
Run Code Online (Sandbox Code Playgroud)
这里有两个可能的process_c_string
带有参数的调用get_string
.
没有绑定const引用的返回对象get_string
.
process_c_string(get_string().c_str());
Run Code Online (Sandbox Code Playgroud)用绑定const引用返回的对象get_string
.
const std::string& tmp_str = get_string();
process_c_string(tmp_str.c_str());
Run Code Online (Sandbox Code Playgroud)我知道第二种方式是有效的,但第一种方式是什么,标准对此案例有什么看法呢?返回的临时对象是否get_string
会在process_c_str
完成之前删除,因为没有const reference
它?
注意:这两个版本在MSVC中都可以.
同时复制和重置shared_ptr是否安全?
即考虑以下代码
// Main thread (before creating any other threads)
shared_ptr<A> a(new A(1));
// Thread 1
shared_ptr<A> a_copy = a;
// Thread 2
a.reset(new(A(2));
Run Code Online (Sandbox Code Playgroud)
线程1和2并行运行.我可以确定,它a_copy
会将指针存储到较旧的A(1)
或较新的A(2)
共享对象吗?
我想写程序创建子进程并将其stdout和stderr流重定向到父进程.那么为了获得良好的性能tmpfile()或pipe(),这样做有什么好的选择?为什么?
我的主要有以下签名:
int _tmain(int argc, _TCHAR* argv[])
Run Code Online (Sandbox Code Playgroud)
我想预先形成以下内容:
FILE *inputFilePtr;
inputFilePtr = fopen(argv[2], "_r");
Run Code Online (Sandbox Code Playgroud)
但是存在类型不匹配.我该怎么办?我应该使用:
inputFilePtr = _tfopen(argv[2], ??????);
Run Code Online (Sandbox Code Playgroud)
谢谢!
可能重复:
为什么我不能创建一个大小由全局变量确定的数组?
这是常量大小为4的简单数组的定义,它存储在堆栈内存中:
int array[4];
Run Code Online (Sandbox Code Playgroud)
现在如果我想在堆栈中声明动态大小的数组,我似乎应该编写这段代码:
int n;
cin >> n;
int array[n];
Run Code Online (Sandbox Code Playgroud)
但正如我们所知,这在C++中是不允许的,而是我们可以编写这个,它将在动态内存(即堆)中创建数组:
int n;
cin >> n;
int *array = new int[n];
Run Code Online (Sandbox Code Playgroud)
但这更慢并且(因为使用了新的运算符)并且需要在我们完成数组工作后调用delete []运算符.
所以我的问题在这里:
在boost/utility/swap.hpp
我发现这段代码:
template<class T, std::size_t N>
void swap_impl(T (& left)[N], T (& right)[N])
{
for (std::size_t i = 0; i < N; ++i)
{
::boost_swap_impl::swap_impl(left[i], right[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
什么是left
和right
?它们是否引用了数组?C++ ISO标准2003或更高版本是否允许此代码?
我已经看到在perl中有时会打开一个文件进行编写,他们使用:
open(my $file_handle, ">$file_name");
Run Code Online (Sandbox Code Playgroud)
而有时:
open(FILE_HANDLE, ">$file_name");
Run Code Online (Sandbox Code Playgroud)
有什么不同?
我正在尝试将xerces 3.1.1与我的程序集成,但它需要libcurl for linux和libicui18n,libicuuc,libicudata,libm for Mac OS X和Solaris.是否有一些可以传递的标志configure
以避免所有这些依赖?
如何将Windows中的文件锁定为只有当前线程(来自同一进程的其他线程,没有其他进程)才能访问(读/写)该文件?
如果有可能请告诉我一些类似fcntl的解决方案(锁定具有其描述符的文件的解决方案).但无论如何,其他解决方案也是受欢迎的.
c++ ×9
file ×3
c ×2
arrays ×1
boost ×1
comparison ×1
dependencies ×1
file-locking ×1
memory ×1
parent-child ×1
path ×1
perl ×1
shared-ptr ×1
tchar ×1
temporary ×1
xerces ×1