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