美文网首页
stl中map用法的总结

stl中map用法的总结

作者: 小雨启明 | 来源:发表于2018-09-16 09:29 被阅读0次
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main() {
    map<int, int> m;//定义map

    m[1] = 1;
    m[1] = 2;//第一种插入方式 使用下标插入
             //key不存在时创建 存在时更新

    cout << m[1]<<endl;

    pair<int, int> p1;//定义键值对
    p1 = make_pair(2, 7);

    pair<int, int> p2(2, 4);//直接构造键值对

    m.insert(p1);
    m.insert(p2);//第二种插入方式 使用insert接口
                 //key不存在时创建 存在时放弃

    cout << m[2]<<endl;

    map<int, int>::iterator itr;
    for (itr = m.begin(); itr != m.end(); itr++) {  //遍历
        cout << itr->first << " " << itr->second << endl;
    }

    if ((itr = m.find(3)) != m.end()) {
        cout << "finded" << endl;
    }
    else {
        cout << "no found";
    }
    return 0;
}


相关文章

网友评论

      本文标题:stl中map用法的总结

      本文链接:https://www.haomeiwen.com/subject/tiuxnftx.html