谁能告诉我<map> 是什么用的? ? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 关联数据提供一个键/值对map<int,string> test;test 就是一个由作为索引,并拥有一个相关联的string 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 对象的指针。 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;}Outputm1 = 10 20 30 40m2 = 20 10m3 = 30m4 = 10 20 30 40m5 = 10 20m6 = 10 VC 下的连接错误 1104 vs2008 创建启动画面 如何实现windows平台发送一个大数据文件到linux平台 一个关于KEYDOWN消息的问题 用什么函数可以弹出一个选择路径的对话框? 如何生成dvd的vob文件 如果让EXE文件在不结束自己的情况下返回给调用者一个数值? 下载vc++帮助 谁能给我一个最简单的发送email的源码 如何在编辑框控件中响应回车键以将其中的内容显示在视图中? 关于在ActiveX中打开文件的问题!! 右键弹出的快捷菜单中,一般都有个默认选项,这个选项是加粗显示的。如何实现???
提供一个键/值对
map<int,string> test;
test 就是一个由作为索引,并拥有一个相关联的string
迭代列表
迭代映射
迭代数组 对 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 对象的指针。
// 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