9

The python collections.Counter object keeps track of the counts of objects.

>> 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}

Is there an analogous C++ object where I can easily keep track of the occurrence counts of a type? Maybe a map to string? Keep in mind that the above is just an example, and in C++ this would generalize to other types to count.

conchoecia
  • 491
  • 1
  • 8
  • 25
  • I think that's more interesting to see Counter use case on entire collection rather than working on it manually. is there any C++ equivalent for that? – Aviv Jun 19 '23 at 09:51

4 Answers4

14

You could use an std::map like:

#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;
    }
}

Output:

1:0
cat:2
dog:8
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
5

You can use std::unordered_map if you want constant on average lookup complexity (as you get using collections.Counter). std::map "usually implemented as red-black trees", so complexity for lookup is logarithmic in the size of the container. And we don't have red-black tree implementation in Python in built-in library.

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;
}
alexfv
  • 1,826
  • 2
  • 19
  • 22
3

You can use 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 is a C++ implementation of collections.Counter: https://gitlab.com/miyamoto128/cppcounter. It is written on top of unordered_map and easy to use.

May I add I am the author ;)

Ogami Itto
  • 31
  • 2
2

Python3 code:

import collections

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

C++ code:

#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;
}

Output:

Lizard:1
Cat:3
Dog:2
Puneeth G R
  • 111
  • 1
  • 8