C++ map_插入数据

map是STL的一个关联容器,它提供一对一的hash。

第一个可以称为关键字(key),每个关键字只能在map中出现一次;第二个可能称为该关键字的值(value); 

由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的

map以模板(泛型)方式实现,可以存储任意类型的数据,包括使用者自定义的数据类型

需要头文件   #include <map>  

#include <iostream>
#include<string>
#include <map>

int main() {
std::map<int, std::string> mapStudent; //创建map对象
mapStudent.insert(std::pair<int, std::string>(1, "student_one")); //用pair方式插入数据
mapStudent.insert(std::pair<int, std::string>(2, "student_two"));
mapStudent.insert(std::pair<int, std::string>(3, "student_three"));

mapStudent.insert(std::map<int, std::string>::value_type(4, "student_4")); //用value_type方式插入数据
mapStudent.insert(std::map<int, std::string>::value_type(5, "student_5"));

mapStudent.insert(std::map<int, std::string>::value_type(1, "student_1"));//插入失败
mapStudent.insert(std::pair<int, std::string>(1, "student_1")); //插入失败
mapStudent[1] = "student_1"; //修改value的值
//失败原因:用insert函数插入数据,涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是不能再插入数据的
//用数组方式可以覆盖以前该关键字对应的值---用来修改值


mapStudent[6] = "student_6"; //数组方式插入数据
mapStudent[7] = "student_7";


std::map<int, std::string>::iterator iter; //迭代器
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
std::cout << iter->first << " " << iter->second << std::endl;//iter->first 返回第一个key的值;iter->second 返回第二个value的值




return 0;
}
std::map<int, std::string> mapStudent; 
std::pair<std::map<int, std::string>::iterator, bool> IP; //用insert方式插入数据时的返回值类型对象
//IP中的第一个数据就是迭代器,第二个数据表示插入是否成功:成功返回真

IP=mapStudent.insert(std::pair<int, std::string>(1, "student_one"));
std::cout <<IP.second << std::endl;
IP = mapStudent.insert(std::pair<int, std::string>(1, "student_1"));
std::cout << IP.second << std::endl;
IP=mapStudent.insert(std::map<int, std::string>::value_type(2, "student_11"));
std::cout << IP.second << std::endl;
std::map<int, std::string> mapStudent; 
mapStudent.insert(std::pair<int, std::string>(1, "student_one"));
mapStudent.insert(std::pair<int, std::string>(2, "student_1"));
mapStudent.insert(std::map<int, std::string>::value_type(3, "student_11"));

int nSize = mapStudent.size(); //返回数据总数

std::map<int, std::string>::reverse_iterator iter; //反向迭代器
for (iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
std::cout << iter->first << " " << iter->second << std::endl;

std::string str = mapStudent[1]; //返回指定key所对应的值
std::map<int, std::string> mapStudent; 
mapStudent.insert(std::pair<int, std::string>(1, "student_one"));
mapStudent.insert(std::pair<int, std::string>(2, "student_1"));
mapStudent.insert(std::map<int, std::string>::value_type(3, "student_11"));

std::map<int, std::string>::iterator iter;

bool b = mapStudent.count(2); //返回指定key是否出现
//指定key已经存在返回真
//参数:指定key

iter = mapStudent.find(1); //根据key进行查找
/*
指定key已经存在,返回相应的迭代器
指定key不存在,返回值与end函数相同
*/

if (iter != mapStudent.end()) {
std::cout << iter->first << " " << iter->second << std::endl;
}
else {
std::cout << "key不存在" << std::endl;
}
std::map<int, std::string> mapStudent; 
mapStudent.insert(std::pair<int, std::string>(1, "student_1"));
mapStudent.insert(std::pair<int, std::string>(2, "student_2"));
mapStudent.insert(std::map<int, std::string>::value_type(3, "student_3"));
mapStudent.insert(std::pair<int, std::string>(4, "student_4"));
mapStudent.insert(std::pair<int, std::string>(5, "student_5"));
mapStudent.insert(std::pair<int, std::string>(6, "student_6"));
mapStudent.insert(std::pair<int, std::string>(7, "student_7"));
mapStudent.insert(std::pair<int, std::string>(8, "student_8"));
mapStudent.insert(std::pair<int, std::string>(9, "student_9"));

std::map<int, std::string>::iterator iter;
std::map<int, std::string>::iterator iter1;
iter = mapStudent.find(2);

mapStudent.erase(iter); //删除指定迭代器的数据
int n = mapStudent.erase(3);//删除指定key的数据,如果删除了会返回1,否则返回0

iter = mapStudent.find(5);
iter1 = mapStudent.find(8);

mapStudent.erase(iter, iter1); //用迭代器,成片的删除
//删除区间是一个前闭后开的集合


for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
std::cout << iter->first << " " << iter->second << std::endl;
std::map<int, std::string> mapStudent; 
mapStudent.insert(std::pair<int, std::string>(1, "student_1"));
mapStudent.insert(std::pair<int, std::string>(2, "student_2"));

std::map<int, std::string> mapStudent1;
mapStudent1.insert(std::pair<int, std::string>(3, "student_3"));
mapStudent1.insert(std::pair<int, std::string>(4, "student_4"));
mapStudent1.swap(mapStudent); //交换两个map对象的所有内容

STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int 型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题 

std::map<int, std::string> mapStudent; 
mapStudent.insert(std::pair<int, std::string>(1, "student_1"));
mapStudent.insert(std::pair<int, std::string>(2, "student_2"));
mapStudent.insert(std::pair<int, std::string>(3, "student_3"));
mapStudent.insert(std::pair<int, std::string>(5, "student_5"));
mapStudent.insert(std::pair<int, std::string>(6, "student_6"));

//mapStudent.clear(); //删除所有元素
std::map<int, std::string>::iterator iter;
bool b = mapStudent.empty(); //判断是否为空,空返回真
iter = mapStudent.lower_bound(4); //返回key键值>=给定元素的第一个位置
iter = mapStudent.upper_bound(3); //返回key键值>给定元素的第一个位置

int n = mapStudent.max_size(); //返回可以容纳的最大元素个数

std::cout << iter->second << std::endl;