C++ map增删改查遍历

int main()
{
    typedef map<int, string> myMap;
    myMap test;
    //插入
    test.insert(pair<int, string>(1, "a"));
    test.insert(pair<int, string>(2, "b"));
    test.insert(pair<int, string>(3, "c"));
    test.insert(pair<int, string>(4, "d"));
    test.insert(pair<int, string>(5, "e"));

    //遍历(二叉搜索树的中序遍历,按照key值递增顺序)
    cout << "遍历" << endl;
    for (auto &i : test)
        cout << i.second << endl;
    cout << endl;
    auto iter = test.rbegin();//最大的N个数
    for (int i = 0; i < 3; i++)
        cout << iter++->second << endl;
    //查找
    cout << "查找" << endl;
    auto it = test.find(1);
    if (it != test.end())
        cout << it->second << endl;
    cout << test.count(1) << endl;
    //删除
    cout << "删除" << endl;
    if (test.erase(3))
        cout << "delete success" << endl;
    for (auto &i : test)
        cout << i.second << endl;

    return 0;
}

 

posted @ 2018-08-23 21:49  aote369  阅读(1354)  评论(0编辑  收藏  举报