怎么做帖子网站,微信小程序注册要钱吗,网站界面修改,微信小程序双人游戏情侣介绍
map是一种键值对容器#xff0c;第一个数值为关键字#xff08;key#xff09;#xff0c;第二个数值为该元素对应的出现的次数。如果是map#xff0c;key只会出现一次#xff0c;如果是unordered_map#xff0c;无此限制。此外#xff0c;map会对元素进行排序第一个数值为关键字key第二个数值为该元素对应的出现的次数。如果是mapkey只会出现一次如果是unordered_map无此限制。此外map会对元素进行排序unordered_map是无序的。
Map的使用
引入头文件 #includemap #includeunordered_mapmap对象是一个模板类需要关键字和存储对象两个模板参数 std::mapint,std::string person可以对模板进行类型定义 使其使用更加方便 typedef std::mapint,std::string MAP_INT_STRING; MAP_INT_STRING person;
map添加数据
准备操作 std::mapint,std::string map_element;使用pair map_element.insert(std::pairint,std::string(1,Jim));使用value_type map_element.insert(std::mapint,std::string::value_type(1,Jim));使用emplace map_element.emplace(1,Jim);使用数组
map数据的遍历
前向迭代器for (auto i m2.cbegin(); i ! m2.cend(); i) {std::cout i-first i-second std::endl;}
反向迭代器 使用rbegin和rend替代上面的cbegin 和 cend数组形式 谨慎使用
map中元素的查找
find() 函数返回一个迭代器指向键值为key的元素如果没有找到就返回指向map尾部的迭代器mapint ,string ::iterator l_it;; l_it maplive.find(112);if(l_it maplive.end())coutwe do not find 112endl;else coutwo find 112endl;
map中元素的删除
iterator eraseiterator it) ;//通过一个条目对象删除
iterator eraseiterator firstiterator last //删除一个范围
size_type erase(const Keykey); //通过关键字删除
clear()//就相当于enumMap.erase(enumMap.begin(),enumMap.end());参考链接
C map用法C 打印 vector 的几种方法