c++ - 插入的最有效数据结构,然后按不同条件排序

标签 c++ sorting vector insert

我想知道从文本中读取不同单词并制作频率表(按出现次数递减排序)的最佳数据结构是什么。

我的想法是使用结构:

struct info {
    string word;
    int num;
};

考虑到这一点,我想知道我应该使用什么: vector 、集合、列表...? 我有两个 vector 实现:

1) 对 vector 进行排序并对单词进行线性搜索,如果单词不在 vector 中,我会在末尾添加元素。当我读完单词时,我按频率递减对 vector 进行排序。

2) 对 vector 进行排序并使用双分搜索,将元素添加到其相应的位置,如果是,则将 num 添加 1。然后我通过降低频率对 vector 进行排序。

你怎么看,做这种运动最好的方法是什么?

最佳答案

std::map<std::string, unsigned int> dictionary;

//where words is a list, vector of your words, replace this with reading from your text file word by word
for(const auto& word : words)
{
  dictionary[word]++;
}

//now the dictionary has your words in alphabetical order and the frequency (number of occurrences)
std::multimap<int, std::string> histogram;
for(const auto& elem : dictionary )
{
   histogram.insert(std::make_pair(elem.second(), elem.first()));
}

//print the histogram
for(const auto& elem : histogram)
{
  cout << elem.first() << " : " << elem.second() << endl;
}

关于c++ - 插入的最有效数据结构,然后按不同条件排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37098158/

相关文章:

c++ - istream 的 tellg/seekg 无法防止堆栈粉碎(g++)?

使用 CompareTo 的 VB.NET 排序顺序 - 结果不正确?

c++ - 在 C++ 中使用 vector 设置并集算法

C++按值而不是按位置删除 vector 元素?

c++ - 是否保证成员函数在创建任何对象之前准备就绪?

c++ - cv::DataType<> 与 cv::traits::Type<>

python - 多维 ndarray 的 argsort

c++ - std::vector = std::vector 时发生了什么?

C++ 类型是地址的抽象? - C++ 模板

python - 如何在 Python3 中使用两个条件对对象进行排序?