boost :: algorithm :: compare& const char

时间:2013-04-08 18:39:42

标签: c++ boost compare

使用#include <boost/algorithm/string.hpp>std::stringstd::vector<std::string>

进行比较
std::string commandLine

std::string::size_type position

std::string delimiters[] = {" ", ",", "(", ")", ";", "=", ".", "*", "-"};
std::vector<std::string> lexeme(std::begin(delimiters), std::end(delimiters));

比较

while (!boost::algorithm::contains(lexeme, std::to_string(commandLine.at(position)))){
    position--;
}

生成以下错误

Error   1   error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const char' (or there is no acceptable conversion)

const char?我没有定义字符串吗?

1 个答案:

答案 0 :(得分:1)

boost::algorithm::contains测试一个序列是否包含在另一个序列中,而不是序列中是否包含一个。你传递的是一系列字符串和一系列字符(也就是一个字符串);因此在尝试将字符串与字符进行比较时出错。

相反,如果要在字符串序列中查找字符串,请使用std::find

while (std::find(lexeme.begin(), lexeme.end(), 
                 std::to_string(commandLine.at(position))) == lexeme.end())
{
    --position;
}
相关问题