Use find in vector of vectors

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?
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
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
Topic archived. No new replies allowed.