stl中map用法的总结

#include
#include
#include
#include
using namespace std;
int main() {
    map m;//定义map

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

    cout << m[1]< p1;//定义键值对
    p1 = make_pair(2, 7);

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

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

    cout << m[2]<::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用法的总结)