在 std::string 中,是否可以在不使用循环的情况下找到一组字符串中的第一个字符串?
E.g.:
std::string str("aaa bbb ccc ddd eee fff ggg");
std::vector<std::string> vs;
vs.push_back("ccc");
vs.push_back("fff");
size_t pos = 0
pos = str.find(vs, pos); //< pseudo code
谢谢你!
您可以将字符串(使用字符串流)拆分为向量,然后使用std::find_first_of
与四个迭代器。
这是一个完整的代码示例
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <iterator>
using namespace std;
int main(void)
{
string str("aaa bbb ccc ddd eee fff ggg");
vector<string> vs;
vs.push_back("ccc");
vs.push_back("fff");
vector<string> scheck;
istringstream instr(str);
copy(istream_iterator<string>(instr),
istream_iterator<string>(),
back_inserter(scheck));
vector<string>::iterator it = find_first_of (scheck.begin(), scheck.end(), vs.begin(), vs.end());
if (it != scheck.end())
cout << "first match is: " << *it << endl;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)