Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following line of code returns <code>true</code> only if all elements from <code>v</code> are not present in <code>m</code>:</p> <pre><code>bool a = v.end() == std::find_if( v.begin(), v.end(), boost::bind( &amp;str_map_t::const_iterator::operator!=, boost::bind&lt;str_map_t::const_iterator&gt;( &amp;str_map_t::find, &amp;m, _1 ), m.end() ) ); </code></pre> <hr> <p><strong>Explanation:</strong></p> <p>Here we have two functors: </p> <ol> <li><p><code>boost::bind&lt;str_map_t::const_iterator&gt;( &amp;str_map_t::find, &amp;m, _1 )</code><br> This functor will return <code>const_iterator</code> which points to the element from <code>m</code> or to <code>m.end()</code> if not found. Here you should explicitly point return type <code>str_map_t::const_iterator</code> for <code>boost::bind</code> to get rid of ambiguity. </p></li> <li><p><code>boost::bind( &amp;str_map_t::const_iterator::operator!=, _1, _2 )</code><br> This one will return <code>true</code> if <code>_1!=_2</code> and <code>false</code> otherwise.</p></li> </ol> <p>Combine 1 and 2 and we'll get the full code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;iterator&gt; #include &lt;string&gt; #include &lt;map&gt; #include &lt;boost/bind.hpp&gt; using namespace std; int main(int argc, char *argv[]) { vector&lt;string&gt; v; v.push_back("x"); v.push_back("a"); v.push_back("6"); typedef map&lt;string, string&gt; str_map_t; str_map_t m; m.insert( str_map_t::value_type( "b", "f" ) ); bool a = v.end() == std::find_if( v.begin(), v.end(), boost::bind( &amp;str_map_t::const_iterator::operator!=, boost::bind&lt;str_map_t::const_iterator&gt;( &amp;str_map_t::find, &amp;m, _1 ), m.end() ) ); std::cout &lt;&lt; a &lt;&lt; endl; return 0; } </code></pre> <p>I wouldn't say it is readable code and I'd recommend to write a custom functor to get it more readable. A more readable version could look like the following (without <code>bind</code>):</p> <pre><code>struct not_in_map { not_in_map( const str_map_t&amp; map ) : map_(map) {} bool operator()( const string&amp; val ) { return map_.end() != map_.find( val ); } private: const str_map_t&amp; map_; }; bool a = v.end() == std::find_if( v.begin(), v.end(), not_in_map(m) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload