EDIT: This coincides with my interest from the answer here:
Currently, I have been using this but it is obviously problematic if one needs to find str3, str4,....
size_t find(const std::string& line, const std::string& str1, const std::string& str2, int pos) {
int eol1 = line.find(str1,pos);
int eol2 = line.find(str2,pos);
return (eol1 < eol2) ? eol2 : eol1;
}
size_t find(const std::string& line, std::vector<std::string> vect, int pos ) {
int eol1;
eol1 = 0;
for (std::vector<std::string>::iterator iter = vect.begin(); iter != vect.end(); ++iter){
//std::cout << *iter << std::endl;
int eol2 = line.find(*iter, pos);
if (eol1 == 0 && eol2 > 0)
eol1 = eol2;
else if ( eol2 > 0 && eol2 < eol1)
eol1 = eol2;
}
return eol1;
}
Question: Why cannot std::begin() work for static while NOT for dynamic and what is the most simple or efficient alternative(s)?
Curiously, I have used frequently two or three words searching in Fortran routines, but no one compact "Multi-string search" function is populated in c++ communities. Would one has to implement the complicated "grep" family or "regex" if you needs this functionality?
bool contains(const std::string& input, const std::string keywords[]){//cannot work
//std::string keywords[] = {"white","black","green"}; // can work
return std::any_of(std::begin(keywords), std::end(keywords),
[&](const std::string& str) {return input.find(str) != std::string::npos; });
}
Why the vectorized version cannot work either?
bool contains(const std::string& input, const std::vector<std::string> keywords){
// do not forget to make the array static!
//std::string keywords[] = {"white","black","green"};
return std::any_of(std::begin(keywords), std::end(keywords),
[&](const std::string& str) {return input.find(str) != std::string::npos; });
}
Appended: on the way of learning "parameter pack", but still something goes wrong ...
//base
size_t fin(const std::string& line, const std::string& str1) {
std::cout << var1 << std::endl;
return line.find(str1);
}
//vargin
template <typename... Types>
size_t fin(const std::string& line, const Types... var1) {
return fin(line, var1...);
}
const std::string keywords[]isn't static or dynamic - it's just a function argument undergoing array decay to a pointer. Just pass astd::vector, or astd::array, or an array ref if you don't want raw arrays to behave like raw arrays always do.std::vector, then that's not the correct term. Vectorization is how to calculate using multiple values in parallel.const std::string keywords[]is parsed asconst std::string* keywords. It's not an array just a pointer.