Inside a std::string, is it possible to find the first of a set of strings without using a loop?
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
Thank you!
You could split the string (using a stringstream) into a vector and then use std::find_first_of
with the four iterators.
Here is a complete code example
#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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With