C++ set增删改查遍历


int main()
{
    set<int> test;
    //插入
    test.insert(2);
    test.insert(1);
    test.insert(4);
    test.insert(5);
    test.insert(3);
    //遍历
    for (auto i : test)
        cout << i << endl;
    cout << endl;
    auto iter = test.rbegin();//最大的N个数
    for (int i = 0; i < 3;i++)
        cout << *iter++ << endl;//双向迭代器不支持随机访问
    //删除    
    test.erase(4);//特定元素
    auto it = test.begin();//最小的N个数
    for (int i = 0; i < 2;i++)
        it=test.erase(it);
    //查找
    if (test.find(5) != test.end())
        cout << "find it" << endl;

    return 0;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM