May 18, 2012 at 7:36pm UTC
I have a structure like this:
vector<vector<string> > ptextarr;
And I want to search in this vector of vectors, for a certain word, say "root". I think the function "find" would be usefull, but I don't see how to apply it on a vector of vectors.
Anyone got an idea?
May 18, 2012 at 8:05pm UTC
sure, if you're looking for exact match:
1 2 3 4 5 6
for (size_t n = 0; n < ptextarr.size(); ++n)
{
auto i = find(ptextarr[n].begin(), ptextarr[n].end(), "root" );
if (ptextarr[n].end() != i)
std::cout << "Found root at row " << n << " col " << i-ptextarr[n].begin() << '\n' ;
}
demo:
http://ideone.com/5S1Sl
Last edited on May 18, 2012 at 8:07pm UTC
May 18, 2012 at 10:03pm UTC
If you are going to find the vector that contains an element that stores the searched string then you can write
1 2 3 4 5 6
auto it = std::find( ptextarr.begin(), ptextarr.end(),
[] ( const std::vector &v )
{
return
( std::find( v.begin(), v.end(), "Target String" ) != v.end() );
} );
Last edited on May 18, 2012 at 10:04pm UTC