计数整数的频率 [英] counting frequency of integers take together
问题描述
我需要在二进制文件中统计不同整数的频率,我该如何做?我不想转换为字符串,因为这将减慢我的程序下来..我想...
I need to count the frequencies of different integers together in a binary file, how can I do this? I do not wish to convert to string, because that would slow down my program down.. I think...
vector<uint32_t> buf(2);
map<uint32_t, uint32_t> mymap;
if(file.is_open())
{
while (file.read(reinterpret_cast<char*>(&buf[0]), sizeof(uint32_t)*numcols))
{
for(size_t i = 0; i < numcols; ++i)
{
mymap[buf[i]]++; // **---> I need help here**
}
}
}
file.close();
如何制作地图的键,以便它总是一起计数那些整数
How can I make the key to the map so that it always counts those integers together
是的..我连续看到整数对的次数,例如多少次(1,2),(8,14)或(7,3)。
Yep.. how many times I see integer pairs consecutively, like how many times (1,2), or (8, 14), or (7,3).
1 2
1 2
7 3
8 14
8 14
8 14
1 2 --> 2 times
7 3 --> 1 time
8 14 --> 3 times
numcols == 2正确。
numcols == 2 correct.
推荐答案
一个选项可能是使用映射
使用 pair< uint32_t,uint32_t>
s作为键。这样,你就可以从 uint32_t
的显式映射到它们出现的频率。
One option might be to have the map
use pair<uint32_t, uint32_t>
s as keys. That way you're explicitly mapping from pairs of uint32_t
s to the frequency with which they appear.
这篇关于计数整数的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!