map使用

// mapTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include <iostream> using namespace std; /* template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map; map映射表 key->value对 key是唯一的,不可能存入key相同的键值对 在map内部所有的数据都是有序的 */ /* 关联容器要求其元素是有序的 排序准则必须定义一种严格的弱顺序:小于和等于可传递 */ /* 比较函数 */ bool comFun(const int a, const int b); bool comFun(const int a, const int b) { return true; } int _tmain(int argc, _TCHAR* argv[]) { map<int, int> haha; /* T& operator[] ( const key_type& x ); 如果该key不存在则添加该key(the element is constructed using its default constructor) */ haha[0] = 5; haha[21] = 10; cout << haha[67] << endl; map<int, int> haha0; haha0.insert(pair<int, int>(2, 9)); haha0.insert(pair<int, int>(1, 9)); haha0.insert(pair<int, int>(6, 9)); haha0.insert(pair<int, int>(7, 10)); haha0.insert(pair<int, int>(9, 9)); /* 不管key存不存在,返回值里面的迭代器指向该key对应的键值对 如果该key是新添加的,返回值里面的bool变量为true,否则为false 如果该key存在,对应的值保持不变 */ pair<map<int, int>::iterator, bool> yes = haha0.insert(pair<int, int>(7, 15)); map<int, int>::iterator ite = haha0.begin(); for (; ite != haha0.end(); ite++) { cout << ite->first << "=" << ite->second << endl; } cout << "------------------------------------------------------" << endl; /* map内部的数据是有序的,所以插入键值对时,插入的位置是有依据的 根据传入的比较函数或者比较对象,比较结果为true的键值对将存放在“前面”(begin,end顺序) 注意:由于我定义的比较函数不符合严格的弱顺序准则,下面的这段代码在debug模式是会产生断言错误(代码这样做是为了遵守STL设计准则) 如果是在release模式下,这段代码是没有问题的,因为每次都是返回true,那么添加的键值对的顺序就是添加的先后顺序反向了 */ map<int, int, bool (*)(const int a, const int b)> haha1(comFun); haha1.insert(pair<int, int>(2, 9)); haha1.insert(pair<int, int>(1, 9)); haha1.insert(pair<int, int>(6, 9)); haha1.insert(pair<int, int>(7, 10)); haha1.insert(pair<int, int>(9, 9)); map<int, int, bool (*)(const int a, const int b)>::iterator ite1 = haha1.begin(); for (; ite1 != haha1.end(); ite1++) { cout << ite1->first << "=" << ite1->second << endl; } /* 几个重要的函数功能演示 */ /* 值比较函数(对象) */ map<int, int> haha2; map<int, int>::key_compare mycomp; mycomp = haha2.key_comp(); cout << mycomp(11, 12) << endl; map<int, int, bool (*)(const int a, const int b)>::key_compare mycomp1; mycomp1 = haha1.key_comp(); cout << mycomp1(13, 11) << endl; /* 键值对比较函数(对象) */ map<int, int> haha3; haha3.insert(pair<int, int>(111, 34)); haha3.insert(pair<int, int>(120, 55)); haha3.insert(pair<int, int>(130, 11)); map<int, int>::value_compare mycomp3(haha3.key_comp()); cout << mycomp3(*(haha3.begin()), *(haha3.begin()++)) << endl; cout << (haha3.value_comp())(*(haha3.begin()), *(haha3.begin()++)) << endl; /* lower_bound Returns an iterator pointing to the first element in the container whose key does not compare less than x(>=x) upper_bound Returns an iterator pointing to the first element in the container whose key compares greater than x(>x) equal_range(三种情况) 1.Returns the bounds of a range that includes all the elements in the container with a key that compares equal to x 2.nearest key greater than x 3.map::end if x is greater than all the elements in the container */ return 0; }

你可能感兴趣的:(map使用)