加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

比较一个字符和一组字符c

发布时间:2020-12-16 06:45:39 所属栏目:百科 来源:网络整理
导读:有没有办法比较单个字符和一组字符? 例如: char a;if(a in {'q','w','e','r','t','y','u'}) return true;else return false; 我需要这样的东西. 解决方法 std::string chars = "qwertyu";char c = 'w';if (chars.find(c) != std::string::npos) { // It's
有没有办法比较单个字符和一组字符?

例如:

char a;
if(a in {'q','w','e','r','t','y','u'})
      return true;
else return false;

我需要这样的东西.

解决方法

std::string chars = "qwertyu";
char c = 'w';
if (chars.find(c) != std::string::npos) {
  // It's there
}

或者您可以使用一组 – 如果您需要经常查找更快.

std::set<char> chars;
char const * CharList = "qwertyu";
chars.insert(CharList,CharList + strlen(CharList));
if (chars.find('q') != chars.end())  {
  // It's there
}

编辑:正如Steve Jessop的建议:你也可以使用chars.count(‘q’)代替find(‘q’)!= end()

您也可以使用当前字符的位图(例如vector< bool>),但这过于复杂,除非您每秒执行几百万次.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读