C++中与Python collections.Counter相当的是什么?

9
Python的collections.Counter对象用于跟踪对象的计数。
>> from collections import Counter
>> myC = Counter()
>> myC.update("cat")
>> myC.update("cat")
>> myC["dogs"] = 8
>> myC["lizards"] = 0
>> print(myC)
{"cat": 2, "dogs": 8, "lizards": 0}

是否有类似的C++对象可以轻松地跟踪类型的发生次数?也许是一个mapstring?请记住,上面只是一个示例,在C++中,这将推广到其他类型进行计数。


我认为在整个集合上查看计数器使用情况比手动处理更有趣。有没有C++的等价物呢? - Aviv
4个回答

14

您可以使用std::map,例如:

#include <iostream>
#include <map>

int main()
{
    std::map<std::string,int> counter;
    counter["dog"] = 8;
    counter["cat"]++;
    counter["cat"]++;
    counter["1"] = 0;

    for (auto pair : counter) {
        cout << pair.first << ":" << pair.second << std::endl;
    }
}

输出:

1:0
cat:2
dog:8

5

如果你想要与使用 collections.Counter 相同的平均常数查找复杂度,可以使用 std::unordered_map。但是,std::map 通常被实现为红黑树,因此查找复杂度对容器大小取对数。而在Python内置库中没有红黑树实现。

std::unordered_map<std::string,int> counter;
counter["dog"] = 8;
counter["cat"]++;
counter["cat"]++;
counter["1"] = 0;

for (auto pair : counter) {
    cout << pair.first << ":" << pair.second << std::endl;
}

3
您可以使用 CppCounter:
#include <iostream>
#include "counter.hpp"

int main() {
    collection::Counter<std::string> counter;
    ++counter["cat"];
    ++counter["cat"];
    counter["dogs"] = 8;
    counter["lizards"] = 0;

    std::cout << "{ ";
    for (const auto& it: counter) {
        std::cout << "\"" << it.first << "\":" << it.second.value() << " ";

    }
    std::cout << "}" << std::endl;
}

CppCounter是一个C++实现的collections.Counter: https://gitlab.com/miyamoto128/cppcounter 。它是基于unordered_map编写的,易于使用。顺便说一句,我是作者;)

请说明链接与问题的关系,并描述其内容。 - M-Chen-3
1
我修改了我的评论并使用CppCounter添加了代码,以回答这个问题。 - Ogami Itto

2

Python3代码:

import collections

stringlist = ["Cat","Cat","Cat","Dog","Dog","Lizard"]
counterinstance = collections.Counter(stringlist)
for key,value in counterinstance.items():
    print(key,":",value)

C++ 代码:

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main()
{
   
    unordered_map <string,int> counter;
    vector<string> stringVector {"Cat","Cat","Cat","Dog","Dog","Lizard"};

    for (auto stringval: stringVector)
    {
        if (counter.find(stringval) == counter.end()) // if key is NOT present already
        {
            counter[stringval] = 1; // initialize the key with value 1
        }
        else
        {
            counter[stringval]++; // key is already present, increment the value by 1
        }
    }
    for (auto keyvaluepair : counter)
    {
        // .first to access key, .second to access value
        cout << keyvaluepair.first << ":" << keyvaluepair.second << endl; 
    }
    return 0;
}

输出:

Lizard:1
Cat:3
Dog:2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接