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

代码参考:C++标准库(第2版)

猜你喜欢

转载自www.cnblogs.com/herd/p/12071845.html