c++ - boost::algorithm::compare & const char

标签 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 ?我不是在定义字符串吗?

最佳答案

boost::algorithm::contains测试一个 sequence 是否包含在另一个序列中,而不是一个 item 是否包含在一个序列中。您正在传递一系列字符串和一系列字符(也称为字符串);因此,当它尝试将字符串与字符进行比较时会出错。

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

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

关于c++ - boost::algorithm::compare & const char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886488/

相关文章:

c++ - 在 AIX 上编译 boost 示例程序

string - 如何比较字符串和整数 Postgres

c++ - 如何 fread 成类内的 vector ?

c++ - 为什么我们不能在 C++ 中转发声明具有已知大小的类/结构?

c++ - 为什么 CreatePointFont() 会为我返回 NULL?

c++ - GetCurrentDirectory 用于启动 App。 C++

c++ - 高效的 boost 分配使用

c++ - boost spirit 递归解析

c# - Fast FileSize 与 Linq 比较

Java - 文件比较;在下一个并发行中搜索字符串(忽略空格)