如何求一对向量中元素的个数

How to find count of element in a vector of pair

本文关键字:元素 何求一 向量      更新时间:2023-10-16

我有一对向量如下

vector<pair<string, vector<string>> v;

我的数据集像

'10', vector1
'10', vector2
'10', vector3
'20', vector4
'20', vector5

我希望输出为count of 10 - 3和count of 20 - 2。我可以知道如何对向量进行计数算法吗?

I tried with

std::count(v.begin(), v.end(), iterator->first)

制作一个简单的直方图:

std::map<std::string, unsigned int> h;
for (auto const & x : v)
{
    ++h[x.first];
}
for (auto const & p : h)
{
    std::cout << p.first << " occurs " << p.second << " times.n";
}