关联容器支持高效的关键字查找和访问。
map:
关联数组:保存关键字key - 值value对。按关键字有序保存。内部是红黑树结构。
对于迭代器来说,可以修改value,而不能修改key。
map数据插入:
#include <iostream> #include <map> #include <string> using namespace std; int main() { map<int, string> mapStudent; //直接用下标方式插入数据 mapStudent[1] = "student_one"; //用insert函数插入pair数据 mapStudent.insert(pair<int, string>(2, "student_two")); //用insert函数插入value_type数据 mapStudent.insert(map<int, string>::value_type (3, "student_three")); map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){ cout<<iter->first<<' '<<iter->second<<endl; } return 0; }
注意:当map中已有某个关键字时,再使用insert是无法插入数据的,但用下标方式就可以覆盖该关键字之前的值。
C++11
原文链接: https://www.cnblogs.com/chongjz/p/12796518.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/345029
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!