我有这个可变的dirpath2,其中我存储了路径的最深目录名称:

typedef std::basic_string<TCHAR> tstring;
tstring dirPath = destPath;
tstring dirpath2 = dirPath.substr(destPathLenght - 7,destPathLenght - 1);

我希望能够将其比较另一个字符串,类似:

if ( _tcscmp(dirpath2,failed) == 0 )
{
...       
}

我尝试了很多事情,但似乎没有任何作用。谁能告诉我该怎么做,或者我在做什么错?

请记住,我几乎对C ++一无所知,这整个事情使我发疯。

提前

有帮助吗?

解决方案

std::basic_string<T> 超载 operator==, , 尝试这个:

if (dirpath2 == failed)
{
...
}

另外,您可以这样做。作为 std::basic_string<T> 没有隐性转换操作员 const T*, ,您需要使用 c_str 成员功能转换为 const T*:

if ( _tcscmp(dirpath2.c_str(), failed.c_str()) == 0 )
{
...
}

其他提示

你为什么要使用 _tcscmp 使用C ++字符串?只需使用它的内置平等操作员:

if(dirpath2==failed)
{
    // ...
}

看看所提供的 比较操作员方法 可以与STL字符串一起使用。

通常,如果使用C ++字符串,则无需使用C字符串函数;但是,如果您需要将C ++字符串传递到期望C串的功能,则可以使用 c_str() 获取一个方法 const C-string带有指定的C ++字符串实例的内容。

顺便说一句,如果您知道“几乎一无所有C ++”,那么您应该 真的 即使您来自C。

STD :: BASIC_STRING具有a ==运算符。使用字符串类模板:

if (dirpath2 == failed)
{
...
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top