Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力

初始化:

#include "map"   //引入头文件

// 定义一个map对象
map<int, string> mapStudent;
 
// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
 
// 第二种 用insert函数插入value_type数据
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
 
// 第三种 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";

插入:

// 如果已经存在键值200,则会作赋值修改操作,如果没有则插入
	_map[200] = "booomm";
//通过insert插入
	_map.insert(std::pair<int, std::string>(4, "33333"));

取值:

用at和[]:

//Map中元素取值主要有at和[]两种操作,at会作下标检查,而[]不会。
	std::cout<< _map.at(100).c_str()<< std::endl;//使用at会进行关键字检查,因为没有100因此该语句会报错
	std::cout << _map.at(4).c_str() << std::endl;//因为已经有4了,不会报错
	
	std::cout << _map[300].c_str() << std::endl;//ID_Name中没有关键字200,使用[]取值会导致插入,因此不会报错,但打印结果为空

map的大小

int nSize = mapStudent.size();

是否存在某个元素

count()         返回指定元素出现的次数, (帮助评论区理解: 因为key值不会重复,所以只能是1 or 0)

删除

删除键为bfff指向的元素
 
cmap.erase("bfff");
 
 
删除迭代器 key所指向的元素
map<string,int>::iterator key = cmap.find("mykey");
  if(key!=cmap.end())
 {
	cmap.erase(key);
 }
 
删除所有元素
cmap.erase(cmap.begin(),cmap.end());

https://blog.csdn.net/zvall/article/details/52267007

遍历

#include <iostream>
#include <map>

using namespace std;

int main() {
    map<int, int> _map;
    _map[0] = 1;
    _map[1] = 2;
    _map[10] = 10;

    map<int, int>::iterator iter;
    iter = _map.begin();
    while(iter != _map.end()) {
        cout << iter->first << " : " << iter->second << endl;
        iter++;
    }

    // 也可以使用for循环遍历
    /*
    for(iter = _map.begin(); iter != _map.end(); iter++) {
        cout << iter->first << " : " << iter->second << endl;
    }
    */
    return 0;
}

https://blog.csdn.net/u010429424/article/details/75332700

https://blog.csdn.net/sevenjoin/article/details/81943864

https://blog.csdn.net/weixin_34218579/article/details/93655976

stl 字符串std::string作为std::map主键key的实例

#include <map>
#include <string>
#include <algorithm>
using namespace std;

class map_value_finder
{
public:
    map_value_finder(const std::string &cmp_string):m_s_cmp_string(cmp_string){}
    bool operator ()(const std::map<std::string, std::string>::value_type &pair)
    {
        return pair.second == m_s_cmp_string;
    }
private:
    const std::string &m_s_cmp_string;
};

int main()
{
    std::map<std::string, std::string> my_map;
    my_map.insert(std::make_pair("10", "china"));
    my_map.insert(std::make_pair("20", "usa"));
    my_map.insert(std::make_pair("30", "english"));
    my_map.insert(std::make_pair("40", "hongkong"));

    //通过key查找
    std::map<string, std::string>::iterator it = my_map.find("10");
    if (it == my_map.end())
       printf("not found\n");       
    else
        printf("found key:%s value:%s\n", it->first.c_str(), it->second.c_str());

    //通过value查找
    it = std::find_if(my_map.begin(), my_map.end(), map_value_finder("english"));
    if (it == my_map.end())
       printf("not found\n");       
    else
        printf("found key:%s value:%s\n", it->first.c_str(), it->second.c_str());
       
    return 0;
}

https://m.tsingfun.com/it/cpp/1434.html

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐