C++ 最有效的数据结构,用于插入,然后使用不同的条件进行排序

C++ 最有效的数据结构,用于插入,然后使用不同的条件进行排序,c++,sorting,vector,insert,C++,Sorting,Vector,Insert,我想知道什么是最好的数据结构,可以从文本中读取不同的单词,并按照减少的出现次数进行排序,生成频率表 我的想法是使用结构: struct info { string word; int num; }; 考虑到这一点,我想知道我应该使用什么:向量、集合、列表。。。? 我有两个使用vector的实现: 1) 对向量进行未排序并对单词进行线性搜索,如果单词不在向量处,我会在末尾添加元素。当我读完这些单词时,我通过降低频率对向量进行排序 2) 对向量进行排序并使用二分法搜索,将元素添加到

我想知道什么是最好的数据结构,可以从文本中读取不同的单词,并按照减少的出现次数进行排序,生成频率表

我的想法是使用结构:

struct info {
    string word;
    int num;
};
考虑到这一点,我想知道我应该使用什么:向量、集合、列表。。。? 我有两个使用vector的实现:

1) 对向量进行未排序并对单词进行线性搜索,如果单词不在向量处,我会在末尾添加元素。当我读完这些单词时,我通过降低频率对向量进行排序

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

你认为,做这种练习最好的方法是什么;
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;
}
//其中单词是一个列表,是单词的向量,用逐字阅读文本文件来代替它 for(const auto和word:words) { 字典[字]+; } //现在,字典已按字母顺序和频率(出现次数)列出您的单词 多重映射直方图; for(常量自动和元素:字典) { 插入(std::make_pair(elem.second(),elem.first()); } //打印直方图 用于(常量自动和元素:直方图) { cout
std::map字典;
//其中单词是一个列表,是单词的向量,用逐字阅读文本文件来代替它
for(const auto和word:words)
{
字典[字]+;
}
//现在,字典已按字母顺序和频率(出现次数)列出您的单词
多重映射直方图;
for(常量自动和元素:字典)
{
插入(std::make_pair(elem.second(),elem.first());
}
//打印直方图
用于(常量自动和元素:直方图)
{
cout如评论中所述(抱歉,键入太难,无法获得学分),您可以使用
std::map
。maps元素被排序,您可以省去“手动”排序的额外工作量。如果您需要两种不同的排序方式,您可以使用两个maps或其他容器并对其排序两次。例如,使用向量:

#include <string>
#include <vector>
#include <algorithm>

struct info {
    std::string word;
    int num;
};

bool sortViaNum(const info& a,const info& b)  { return a.num > b.num; }
bool sortViaWord(const info& a,const info& b) { return a.word > b.word; }

int main() {

    std::vector<info> vect;
    // fill the vector
    std::sort(vect.begin(),vect.end(),sortViaNum);
    std::sort(vect.begin(),vect.end(),sortViaWord);
    return 0;
}
#包括
#包括
#包括
结构信息{
字符串字;
int-num;
};
boolsortvianum(constinfo&a,constinfo&b){返回a.num>b.num;}
bool-sortViaWord(const-info&a,const-info&b){返回a.word>b.word;}
int main(){
std::vector-vect;
//填充向量
排序(vect.begin()、vect.end()、sortViaNum);
排序(vect.begin()、vect.end()、sortViaWord);
返回0;
}
如评论中所述(抱歉,键入太难,无法获得学分),您可以使用
std::map
。maps元素被排序,您可以省去“手动”排序的额外工作量。如果您需要两种不同的排序方式,您可以使用两个maps或其他容器,并将其排序两次。例如,使用向量:

#include <string>
#include <vector>
#include <algorithm>

struct info {
    std::string word;
    int num;
};

bool sortViaNum(const info& a,const info& b)  { return a.num > b.num; }
bool sortViaWord(const info& a,const info& b) { return a.word > b.word; }

int main() {

    std::vector<info> vect;
    // fill the vector
    std::sort(vect.begin(),vect.end(),sortViaNum);
    std::sort(vect.begin(),vect.end(),sortViaWord);
    return 0;
}
#包括
#包括
#包括
结构信息{
字符串字;
int-num;
};
boolsortvianum(constinfo&a,constinfo&b){返回a.num>b.num;}
bool-sortViaWord(const-info&a,const-info&b){返回a.word>b.word;}
int main(){
std::vector-vect;
//填充向量
排序(vect.begin()、vect.end()、sortViaNum);
排序(vect.begin()、vect.end()、sortViaWord);
返回0;
}

您可以使用
std::map
来计算特定的单词。我的想法是我必须使用两种类型的排序:首先使用字母顺序,然后按频率排序。您可以使用
std::map
来计算特定的单词。我的想法是我必须使用两种类型的排序:首先使用字母顺序,然后使用排序by频率。