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