转自微博:
http://blog.sina.com.cn/s/blog_74112f030101cm1j.html
http://msdn.microsoft.com/en-us/library/y6ec19ye(v=vs.80).aspx
typedef pair value_type;
这个value_type实际上就是一个pair用它可以给map插入一些值,具体例子如下
#include
#include
using namespace std;
int main()
{
using namespace std;
typedef paircInt2Int;
mapm1;
map::key_type key1;//定义了一个map的键
map::mapped_type mapped1;//定义了一个map的值
map::value_type value1;//定义了一个map的变量组合
map::iterator pIter;
//value_type can be used to pass the correct type
//explicitly to avoid implicit type conversion
m1.insert(map::value_type(1,10));//此处可以看出value_type到底是什么东西
//compair other ways to insert objects into a map;
m1.insert(cInt2Int(2,20));
m1[3]=30;
//Initializing key1 and mapped1
key1=(m1.begin()->first);
mapped1=(m1.begin()->second);
cout<<"the key of first element in the map is "
<<key1<<"."<<endl;
cout<<"the data value of first element in the map is "
<<mapped1<<"."<<endl;
//The following line would cause an error because
//the value_type is not assignable
//value1=cInt2Int(4,40);//type_value不能被赋值!
cout<<"The keys of the mapped elements are:";
for(pIter=m1.begin();pIter!=m1.end();pIter++)
cout<<" "<<pIter->first;
cout<<"."<<endl;
cout<<"The values of the mapped elements are:";
for(pIter=m1.begin();pIter!=m1.end();pIter++)
cout<<" "<<pIter->second;
cout<<"."<<endl;
}