首页 > 编程语言 > 详细

C++ unordered_set运用实例

时间:2019-12-20 12:30:55      阅读:182      评论:0      收藏:0      [点我收藏+]

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版)

C++ unordered_set运用实例

原文:https://www.cnblogs.com/herd/p/12071845.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!