一、标准库的map类型
使用map得包含map类所在的头文件
template < class Key, class Type, class Traits = less<Key>, class Allocator=allocator<pair <const Key, Type> > > class map
#include <map>
定义一个map对象: map<string, int> mapTest;
//用string作为索引,存储int对象
例程1:map 插入数据
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <map>
#include <string> #include <iostream> using namespace std; int main( void) { // 插入到map容器内部的元素默认是按照key从小到大来排序。 // key类型一定要重载<运算符 map<string, int> mapTest; mapTest[ "aaa"] = 100; // int& operator[](const string& index); mapTest[ "eee"] = 500; mapTest[ "eee"] = 300; //[]方式,key重复,则被修改成最后一次插入的值。 mapTest.insert(map<string, int>::value_type( "bbb", 200)); mapTest.insert(map<string, int>::value_type( "bbb |