?

解决方案 »

  1.   

    关联数据
    提供一个键/值对
    map<int,string> test;
    test 就是一个由作为索引,并拥有一个相关联的string
      

  2.   

    copy from MSDNMFC 数组集合类——基于模板的和不基于模板的——都使用索引访问它们的元素。MFC 列表和映射集合类——基于模板的和不基于模板的——都使用 POSITION 类型的指针描述集合内的给定位置。若要访问这些集合的一个或多个成员,请首先初始化位置指针,然后重复将该位置传递到集合并要求集合返回下一个元素。集合不负责维护迭代进度的状态信息。此信息保留在位置指针中。但是,如果给定特定位置,则集合负责返回下一个元素。下面的过程显示了如何在由 MFC 提供的三种主要集合类型上迭代: 迭代数组 
    迭代列表 
    迭代映射 
    迭代数组 对 GetAt 成员函数使用顺序索引号: 
    CTypedPtrArray<CObArray, CPerson*> myArray;for( int i = 0; i < myArray.GetSize();i++ )
    {
        CPerson* thePerson = myArray.GetAt( i );
        ...
    }
    此示例使用包含指向 CPerson 对象的指针的类型指针数组。数组从 CObArray 类(非模板预定义类之一)中派生。GetAt 返回指向 CPerson 对象的指针。对于类型指针集合类——数组或列表——第一个参数指定基类;第二个参数指定要存储的类型。 CTypedPtrArray 类也重载 [ ] 运算符,以便可以使用自定义的数组下标语法访问数组元素。以上 for 循环主体中语句的另一种替换方法是: CPerson* thePerson = myArray[ i ];
    此运算符既在 const 版本中存在,也在非 const 版本中存在。const 版本(为 const 数组调用)只能显示在赋值语句的右侧。 迭代列表 使用成员函数 GetHeadPosition 和 GetNext 迭代列表: 
    CTypedPtrList<CObList, CPerson*> myList;POSITION pos = myList.GetHeadPosition();
    while( pos != NULL )
    {
        CPerson* thePerson = myList.GetNext( pos );
        ...
    }
    此示例使用类型指针列表包含指向 CPerson 对象的指针。该列表声明类似于过程迭代数组中的数组列表声明,但它是从 CObList 类派生的。GetNext 返回指向 CPerson 对象的指针。 
      

  3.   

    Example
    // map_map.cpp
    // compile with: /EHsc
    #include <map>
    #include <iostream>int main( )
    {
       using namespace std;
       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;   // Create an empty map m0 of key type integer
       map <int, int> m0;   // Create an empty map m1 with the key comparison
       // function of less than, then insert 4 elements
       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 ) );   // Create an empty map m2 with the key comparison
       // function of geater than, then insert 2 elements
       map <int, int, greater<int> > m2;
       m2.insert( Int_Pair( 1, 10 ) );
       m2.insert( Int_Pair( 2, 20 ) );   // Create a map m3 with the 
       // allocator of map m1
       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 ) );   // Create a copy, map m4, of map m1
       map <int, int> m4( m1 );   // Create a map m5 by copying the range m1[_First, _Last)
       map <int, int>::const_iterator m1_bcIter, m1_ecIter;
       m1_bcIter = m1.begin( );
       m1_ecIter = m1.begin( );
       m1_ecIter++;
       m1_ecIter++;
       map <int, int> m5(m1_bcIter, m1_ecIter);   // Create a map m6 by copying the range m4[_First, _Last)
       // and with the allocator of map 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);   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;
    }
    Output
    m1 = 10 20 30 40
    m2 = 20 10
    m3 = 30
    m4 = 10 20 30 40
    m5 = 10 20
    m6 = 10