【问题标题】:Find_if function in vector向量中的 Find_if 函数
【发布时间】:2011-03-29 06:44:43
【问题描述】:

这个函数是否对给定的向量数组进行排序?

请提出你的建议

问候,

瓦桑塔姆

【问题讨论】:

标签: c++ stl visual-c++


【解决方案1】:

std::find_if 不是vector 的成员,它是来自<algorithm> 的免费函数模板。

它不对给定的范围进行排序,它只是返回一个迭代器,指向给定范围的第一个元素,给定谓词为其返回true

【讨论】:

    【解决方案2】:

    Find_if 是 an 的函数,它返回一个迭代器,指向范围内的第一个元素。

       #include <list>
      #include <algorithm>
      #include <iostream>
    
      bool greater10 ( int value )
      {
      if(value == 10)
       {
        return true;
       }
       return false;
      }
    
     int main( )
      {
           using namespace std;
    
           list <int> L;
           list <int>::iterator Iter;
          list <int>::iterator result;
    
          L.push_back( 10 );
          L.push_back( 9 );
          L.push_back( 15 );
          L.push_back( 8);
          L.push_back( 10 );
    
         cout << "L = ( " ;
          for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
              cout << *Iter << " ";
          cout << ")" << endl;
    
    
          result = find_if( L.begin( ), L.end( ), &greater10 );
          for( ;result != L.end(); result++ )
           {
          cout << *result <<"\t";
             cout <<endl;
          }
      }
    

    此函数将第一个元素作为迭代器返回,它不会对向量数组进行排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2018-05-28
      • 2018-09-15
      • 2017-03-20
      • 1970-01-01
      • 2021-12-24
      • 2021-10-07
      相关资源
      最近更新 更多