小编Kar*_*ham的帖子

如何减少 C++ 中多重集中元素的计数?

我在 C++ 中使用多重集,我相信它存储一个元素及其插入时的相应计数。

在这里,当我想删除一个元素时,我只想将集合中该元素的计数减1,直到它大于0。

C++ 代码示例:

    multiset<int>mset;
    mset.insert(2);
    mset.insert(2);
    printf("%d ",mset.count(2)); //this returns 2
    // here I need an O(1) constant time function (in-built or whatever )
    // to decrease the count of 2 in the set without deleting it 
    // Remember constant time only

      -> Function and its specifications

    printf("%d ",mset.count(2)); // it should print 1 now .
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以实现这一点,或者我应该删除它并按所需的(count-1)次插入元素 2 ?

c++ c++11

4
推荐指数
1
解决办法
2160
查看次数

标签 统计

c++ ×1

c++11 ×1