Hot to find vector<string> element in a vector<string>

<>

This question already has an answer here:

Answers


You can use standard algorithm std::find

For example

#include <algorithm>
#include <vector>
//..
if ( std::find( v1.begin(), v1.end(), *v2iter ) != v1.end() )
{
   std::cout << *v2iter << " is found in v1" << std::endl;
}

Or you can use standard algorithm std::any_of

For example

#include <algorithm>
#include <vector>
#include <functional>
//..
if ( std::any_of( v1.begin(), v1.end(), std::bind2nd( std::equal_to<std::string>(), *v2iter ) ) )
{
   std::cout << *v2iter << " is found in v1" << std::endl;
}

Or if the first vector is ordered then you can use algorithm std::binary_search

For example

#include <algorithm>
#include <vector>
//..
if ( std::binary_search( v1.begin(), v1.end(), *v2iter ) )
{
   std::cout << *v2iter << " is found in v1" << std::endl;
}

You can find that using :

    vector <string > :: iterator it , fit ;
    for ( it = v2.begin() ; it != v2.end() ; it++ ){
       fit = find ( v1.begin() , v1.end() , *it ) ;
       if ( fit != v1.end() ){
        // means that the element is present in v1 
       }
    }

Need Your Help

Best way to use Multiple Pages on Smarty

php smarty

Is this the most effective way to use smarty with multiple pages?:

Can Clojure Korma produce WITH - RETURNING query in Postgres?

postgresql clojure korma sqlkorma

I'm trying to reproduce this query from the Postgres docs: