济南网站seo外包,asp黑网站源码,网站建设优化服务流程,网站策划怎么做1、string字符串中是否存在某个字符#xff08;char#xff09;
string中find()返回值是字母在母串中的位置#xff08;下标索引#xff09;#xff0c;如果没有找到#xff0c;那么会返回一个特别的标记npos。#xff08;返回值可以看成是一个int型的数#xff09;
…1、string字符串中是否存在某个字符char
string中find()返回值是字母在母串中的位置下标索引如果没有找到那么会返回一个特别的标记npos。返回值可以看成是一个int型的数
string sentence I am a bad girl;
char s c;
if(string::npos sentence.find(s)) cout 不存在 endl;2、vector中是否存在某个元素
2.1 std::count()
最简单的方式是对vector中的指定元素进行计数如果count不为零表示该元素存在那么std::count可以很容易实现。
vectorstring words {wk,xf,ot,je}
string word wk;
if(count(words.begin(), words.end(), word)) cout Found endl;
else cout Not Found endl;2.2 std::find()
较之count()std::find()算法能够更快速的查找给定范围内的值因为std::count()会变量整个容器以获得元素计数而find()在找到匹配元素后就立即停止搜索。
vectorstring words {wk,xf,ot,je}
string word wk;
if(find(words.begin(), words.end(), word)!words.end()) cout Found endl;
else cout Not Found endl;2.3 std::binary_search()
如果vector是有序的那么可以考虑使用这种算法如果在给定范围内找到元素则返回true否则返回false。该方式是采用二分法查找时间复杂度为O(log(n))速度比较快。
vectorstring words {wk,xf,ot,je}
string word wk;
if(binary_search(words.begin(), words.end(), word)) cout Found endl;
else cout Not Found endl;