C++ unordered_set運用實例


C++ unordered_set運用實例

#include <unordered_set>
#include <numeric>
#include "print.hpp"

using namespace std;

int main()
{
    unordered_set<int> u_set1 = {1,2,3,5,7,11,13,17,19,23,29,31,37,41};
    PRINT_ELEMENTS(u_set1);

    u_set1.insert({-7,17,33,-11,17,19,23});
    PRINT_ELEMENTS(u_set1);


    u_set1.erase(33);
    PRINT_ELEMENTS(u_set1);

    u_set1.insert(accumulate(u_set1.begin(),u_set1.end(),0));
    PRINT_ELEMENTS(u_set1);
    
    unordered_set<int>::iterator pos1;
    for (pos1 = u_set1.begin();pos1 != u_set1.end();)
    {
        if (*pos1 <0)
        {
            pos1 = u_set1.erase(pos1);
        }
        else
        {
            ++pos1;
        }
    }

    PRINT_ELEMENTS(u_set1);

    system("pause");
    return 0;
}

17 1 2 19 11 3 13 5 7 23 29 31 37 41
17 1 2 19 11 3 13 5 7 23 29 31 37 41 -7 33 -11
17 1 2 19 11 3 13 5 7 23 29 31 37 41 -7 -11
17 1 2 19 11 3 13 5 7 23 221 29 31 37 41 -7 -11
17 1 2 19 11 3 13 5 7 23 221 29 31 37 41
請按任意鍵繼續. . .

 

代碼參考:C++標准庫(第2版)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM