How could I search a vector element with multiple strings
i.e. vector.at(i) = This is a test
How could I search that to look for the word "test" in that element?
Thanks in advance!
How could I search a vector element with multiple strings
i.e. vector.at(i) = This is a test
How could I search that to look for the word "test" in that element?
Thanks in advance!
Thanks for the reply but not sure what you mean.
for_each is a standard algorithm (specified in the standard header <algorithm>) can be used to perform some action on every element of a vector. A custom lambda is one way to specify that action as being to find a particular substring.
Look up the concepts of algorithms (in all standard versions of C++) and lambdas (in C++ 11).
As the canadian guy quoted once in his signature, simplicity is the ultimate virtue.
Why not use string::find() ?
I think that you already now, that the "vector.at(i)", will give you actually an std::string, which of course can contain a colletction of strings.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
By your question, it would seem as if you know how to search through a string normally. Now searching through a string in a vector is no different.
A particularly easy option would be to just declare a const-reference to the string in the container, then it should reduce the problem to exactly what you appear to know how to do already.
Code:const std::string s = vector.at(i); // Now do your search within s.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
There is more than one Canadian here for sure. ???As the canadian guy quoted once in his signature, simplicity is the ultimate virtue
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson