c++如何理解map对象的value_type是pair类型
map<K,V>::value_type,它是一个pair类型,如何理解????象map的键值对的键对应的值如果为1,value_type是不是就为1,而pair包含两个...
map<K, V>::value_type,它是一个pair类型,如何理解????象map的键值对的键对应的值如果为1,value_type是不是就为1,而pair包含两个数据值,且有可能为不同类型,那么value_type该怎么理解??谢谢
展开
2个回答
展开全部
map 是以 pair形式插入的。
map中的元素的类型value_type
typedef pair<const Key, Type> value_type;
value_type 被声明为 pair <const key_type, mapped_type> 但并不是简单的 pair <key_type, mapped_type> 因为侍启用一个非常量的迭代器或枣猜引用不能改变关联容器的Key。
#include <map>
#include <iostream>
int main( )
{
using namespace std;
typedef pair <const int, int> cInt2Int;
map <int, int> m1;
map <int, int> :: key_type key1;
map <int, int> :: mapped_type mapped1;
map <int, int> :: value_type value1;
map <int, int> :: iterator pIter;
// value_type can be used to pass the correct type
// explicitly to avoid implicit type conversion
m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
// Compare 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 );
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;
}
Output
The key of first element in the map is 1.
The data value of first element in the map is 10.
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.
map中的元素的类型value_type
typedef pair<const Key, Type> value_type;
value_type 被声明为 pair <const key_type, mapped_type> 但并不是简单的 pair <key_type, mapped_type> 因为侍启用一个非常量的迭代器或枣猜引用不能改变关联容器的Key。
#include <map>
#include <iostream>
int main( )
{
using namespace std;
typedef pair <const int, int> cInt2Int;
map <int, int> m1;
map <int, int> :: key_type key1;
map <int, int> :: mapped_type mapped1;
map <int, int> :: value_type value1;
map <int, int> :: iterator pIter;
// value_type can be used to pass the correct type
// explicitly to avoid implicit type conversion
m1.insert ( map <int, int> :: value_type ( 1, 10 ) );
// Compare 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 );
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;
}
Output
The key of first element in the map is 1.
The data value of first element in the map is 10.
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.
展开全部
map<string, int>::key_type v1; //string
map<string, int>::mapped_type v2; //int
map<毕差string, int>::value_type v3; //pair<const string, int>
对于泛型编程来说,可能不知道具体类型是什脊衫么,所有就有了key_type、mapped_type、value_type这些类型。
key_type就表示map中手野皮key的类型。以下两句是一个意思:
map<string, int>::key_type v1;
string v1;
同理,mapped_type表示map中value的类型。
value_type这是具体值的类型,由于map中存的是键值对pair。而key又不能修改。
故为pair<const string,int>。
map<string, int>::mapped_type v2; //int
map<毕差string, int>::value_type v3; //pair<const string, int>
对于泛型编程来说,可能不知道具体类型是什脊衫么,所有就有了key_type、mapped_type、value_type这些类型。
key_type就表示map中手野皮key的类型。以下两句是一个意思:
map<string, int>::key_type v1;
string v1;
同理,mapped_type表示map中value的类型。
value_type这是具体值的类型,由于map中存的是键值对pair。而key又不能修改。
故为pair<const string,int>。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询