首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::algorithm::compare & const char

boost::algorithm::compare & const char
EN

Stack Overflow用户
提问于 2013-04-09 02:39:42
回答 1查看 282关注 0票数 0

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

代码语言:javascript
复制
std::string commandLine

std::string::size_type position

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

比较

代码语言:javascript
复制
while (!boost::algorithm::contains(lexeme, std::to_string(commandLine.at(position)))){
    position--;
}

生成以下错误

代码语言:javascript
复制
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?我不是在定义字符串吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-09 02:54:27

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

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

代码语言:javascript
复制
while (std::find(lexeme.begin(), lexeme.end(), 
                 std::to_string(commandLine.at(position))) == lexeme.end())
{
    --position;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15886488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档