使用大写和小写字母对矢量进行排序

时间:2023-01-15 00:09:53

So I have a program that reads through a file and gets the largest words by using a std::map with an int as the key and a std::vector as value. I need to print out the largest words but have a problem with the sorting. Whereas the output should be Copyright, Publishin, Universal, blueprint I get :blueprint, Copyright, Universal, Publishin

所以我有一个程序可以读取文件并通过使用std :: map获取最大的单词,其中int作为键,std :: vector作为值。我需要打印出最大的单词,但排序有问题。而输出应该是版权,发布,通用,蓝图我得到:蓝图,版权,通用,Publishin

How can I fix this? Do vectors not sort uppercase AND lowercase? Here's what I have so far:

我怎样才能解决这个问题?矢量不排序大写和小写?这是我到目前为止所拥有的:

string temp;
stringstream ss(line);
while(ss >> temp) {
    int wordlength = temp.length();
    wordit = wordbycount.find(wordlength);
    if (wordit != wordbycount.end()) {
        vector<string> arrayofLongest = wordit->second; 
        std::vector<string>::iterator iterator1 = find(arrayofLongest.begin(), arrayofLongest.end(), temp);
        if (iterator1 == arrayofLongest.end()) {
            wordit->second.push_back(temp);
        }
    }
    else {
        wordbycount[wordlength] = temp;
    }
}

1 个解决方案

#1


0  

Do vectors not sort uppercase AND lowercase?

矢量不排序大写和小写?

No.

std::vectors doesn't sort (simply appending elements).

std :: vectors不排序(只是附加元素)。

std::vectors maintain the order of insertion.

std :: vectors维护插入顺序。

If you want an insert-and-ordering container, you should use std::set (or std::multiset).

如果需要插入和排序容器,则应使用std :: set(或std :: multiset)。

#1


0  

Do vectors not sort uppercase AND lowercase?

矢量不排序大写和小写?

No.

std::vectors doesn't sort (simply appending elements).

std :: vectors不排序(只是附加元素)。

std::vectors maintain the order of insertion.

std :: vectors维护插入顺序。

If you want an insert-and-ordering container, you should use std::set (or std::multiset).

如果需要插入和排序容器,则应使用std :: set(或std :: multiset)。