CHAPTER10-1.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:3k
- //文件名:CHAPTER10-1.cpp
- #include <map>
- #include <iostream>
- #if _MSC_VER > 1020 // if VC++ version is > 4.2
- using namespace std; // std c++ libs implemented in std
- #endif
- int main( )
- {
- typedef pair <int, int> Int_Pair;
- map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter;
- map <int, int, greater<int> >::iterator m2_Iter;
- // 创建一个空的map变量m0,类型为整数
- map <int, int> m0;
- // 创建带有谓词的一个空的map变量m1,谓词为小于less than
- map <int, int, less<int> > m1;
- //进行元素插入
- m1.insert( Int_Pair( 1, 10 ) );
- m1.insert( Int_Pair( 2, 20 ) );
- m1.insert( Int_Pair( 3, 30 ) );
- m1.insert( Int_Pair( 4, 40 ) );
- //创建带有谓词的一个空的map变量m1,谓词为大于greater than
- map <int, int, greater<int> > m2;
- m2.insert( Int_Pair( 1, 10 ) );
- m2.insert( Int_Pair( 2, 20 ) );
- // 利用m1的内存分配器创建一个map 变量m3
- map <int, int>::allocator_type m1_Alloc;
- m1_Alloc = m1.get_allocator( );
- map <int, int> m3( less<int>( ), m1_Alloc );
- m3.insert( Int_Pair( 3, 30 ) );
- // 根据m1创建一个拷贝型map变量m4
- map <int, int> m4( m1 );
- //根据m1[_First, _Last]创建一个拷贝型map变量m5
- map <int, int>::const_iterator m1_bcIter, m1_ecIter;
- m1_bcIter = m1.begin( );
- m1_ecIter = m1.begin( );
- m1_ecIter++;
- m1_ecIter++;
- //p <int, int> m5(m1_bcIter, m1_ecIter);
- map <int, int> m5;
- m5.insert( Int_Pair( 1, 10 ) );
- m5.insert( Int_Pair( 2, 20 ) );
- //根据m4[_First, _Last]创建一个拷贝型map变量m6,并且其内存分配器为m2
- map <int, int>::allocator_type m2_Alloc;
- m2_Alloc = m2.get_allocator( );
- //map <int, int> m6( m4.begin( ), ++m4.begin( ), less<int>( ), m2_Alloc);
- map <int, int> m6;
- m6.insert( Int_Pair( 2, 20 ) );
- m6.insert( Int_Pair( 1, 10 ) );
- cout << "m1 =";
- for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
- cout << " " << m1_Iter -> second;
- cout << endl;
- cout << "m2 =";
- for ( m2_Iter = m2.begin( ); m2_Iter != m2.end( ); m2_Iter++ )
- cout << " " << m2_Iter -> second;
- cout << endl;
- cout << "m3 =";
- for ( m3_Iter = m3.begin( ); m3_Iter != m3.end( ); m3_Iter++ )
- cout << " " << m3_Iter -> second;
- cout << endl;
- cout << "m4 =";
- for ( m4_Iter = m4.begin( ); m4_Iter != m4.end( ); m4_Iter++ )
- cout << " " << m4_Iter -> second;
- cout << endl;
- cout << "m5 =";
- for ( m5_Iter = m5.begin( ); m5_Iter != m5.end( ); m5_Iter++ )
- cout << " " << m5_Iter -> second;
- cout << endl;
- cout << "m6 =";
- for ( m6_Iter = m6.begin( ); m6_Iter != m6.end( ); m6_Iter++ )
- cout << " " << m6_Iter -> second;
- cout << endl;
- }