⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter11-4.cpp

📁 C++STL程序员开发指南
💻 CPP
字号:
//文件名:CHAPTER11-4.cpp
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
// Return whether second element is twice the first
bool twice ( int elem1, int elem2 )
{   return 2 * elem1 == elem2;}
int main( )
{
   using namespace std;
   list <int> L;
   list <int>::iterator Iter;
   list <int>::iterator result;
   L.push_back( 40 );
   L.push_back( 20 );
   L.push_back( 10 );
   L.push_back( 40 );
   L.push_back( 10 );
   cout << "L = ( " ;
   for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ ) cout << *Iter << " ";
   cout << ")" << endl;
   result = find( L.begin( ), L.end( ), 10 );
   if  ( result == L.end( ) ) cout << "There is no 10 in list L." << endl;
   else  result++;
      cout << "There is a 10 in list L and it is"<< " followed by a " << *(result) << "." << endl;
   vector <int> v1, v2;
   list <int> L1;
   vector <int>::iterator Iter1, Iter2;
   list <int>::iterator L1_Iter, L1_inIter;
   int i;
   for ( i = 0 ; i <= 5 ; i++ ){v1.push_back( 5 * i );}
   for ( i = 0 ; i <= 5 ; i++ ){v1.push_back( 5 * i );}
   int ii;
   for ( ii = 1 ; ii <= 4 ; ii++ ){L1.push_back( 5 * ii );}
   int iii;
   for ( iii = 2 ; iii <= 4 ; iii++ ){v2.push_back( 10 * iii );}
   cout << "Vector v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " ";
   cout << ")" << endl;
   cout << "List L1 = ( " ;
   for ( L1_Iter = L1.begin( ) ; L1_Iter!= L1.end( ) ; L1_Iter++ )cout << *L1_Iter << " ";
   cout << ")" << endl;
   cout << "Vector v2 = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) cout << *Iter2 << " ";
      cout << ")" << endl;
   // Searching v1 for a match to L1 under identity
   vector <int>::iterator result1;
   result1 = find_end ( v1.begin( ), v1.end( ), L1.begin( ), L1.end( ) );
   if ( result1 == v1.end( ) ) cout << "There is no match of L1 in v1."<< endl;
   else   cout << "There is a match of L1 in v1 that begins at "
           << "position "<< result1 - v1.begin( ) << "." << endl;
   // Searching v1 for a match to L1 under the binary predicate twice
   vector <int>::iterator result2;
   result2 = find_end ( v1.begin( ), v1.end( ), v2.begin( ), v2.end( ), twice );
   if ( result2 == v1.end( ) ) cout << "There is no match of L1 in v1."<< endl;
   else    cout << "There is a sequence of elements in v1 that "
           << "are equivalent to those\n in v2 under the binary "
           << "predicate twice and that begins at position "<< result2 - v1.begin( ) << "." << endl;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -