hik*_*ume 2 c++ string compare tchar
我有这个变量dirpath2,我存储路径的最深目录名称:
typedef std::basic_string<TCHAR> tstring;
tstring dirPath = destPath;
tstring dirpath2 = dirPath.substr(destPathLenght - 7,destPathLenght - 1);
Run Code Online (Sandbox Code Playgroud)
我希望能够将它与另一个字符串进行比较,例如:
if ( _tcscmp(dirpath2,failed) == 0 )
{
...
}
Run Code Online (Sandbox Code Playgroud)
我尝试过很多东西,但似乎没什么用.任何人都可以告诉我如何做到这一点或我做错了什么?
请记住,我对C++几乎一无所知,这一切都让我疯狂.
提前
std::basic_string<T>有一个超载operator==,试试这个:
if (dirpath2 == failed)
{
...
}
Run Code Online (Sandbox Code Playgroud)
或者你可以这样做.由于std::basic_string<T>没有隐式转换运算符const T*,您需要使用c_str成员函数转换为const T*:
if ( _tcscmp(dirpath2.c_str(), failed.c_str()) == 0 )
{
...
}
Run Code Online (Sandbox Code Playgroud)
你为什么使用_tcscmpC++字符串?只需使用它的内置相等运算符:
if(dirpath2==failed)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
通常,如果使用C++字符串,则不需要使用C字符串函数; 但是,如果需要将C++字符串传递给期望C字符串的函数,则可以使用该c_str()方法获取具有const指定C++字符串实例内容的C字符串.
顺便说一句,如果你知道"几乎旁边没有关于C++",你应该真正得到一个C++的书,读它,即使你来自C.