简体   繁体   English

在字符串向量中对大小写单词进行排序

[英]Sorting upper and lower case words in vector of strings

I'm trying to alphabetize words in a vector of strings and my program is distinguishing between upper and lowercase letters, so uppercase words always appear first in the sorted list. 我试图按字母顺序将字符串向量中的单词按字母顺序排列,并且我的程序区分大写和小写字母,因此大写单词始终排在排序列表的首位。 I can think of potentially really cumbersome ways to make sure the upper case words go in their place, but is there a simple way to do this? 我可以想到一些可能非常麻烦的方法来确保大写单词在其位置上,但是有没有简单的方法可以做到这一点?

Here is my code: 这是我的代码:

for (int i = 0; i < str.size(); i++)
    {
        for (int j = 0; j < str.size(); j++) 
        {
            if (str.at(i) > str.at(j))
            {               
                temp = str.at(j);
                str.at(j) = str.at(i);
                str.at(i) = temp;
            }   
        }
    }

Also, this is for a programming assignment, so I am not allowed to use built in C++ functions to do this and I have to use a vector. 另外,这是用于编程的任务,因此不允许使用内置的C ++函数来执行此操作,而必须使用向量。

The trick to solving this is to write a replacement for this line 解决这个问题的技巧是为该行写一个替换

if (str.at(i) > str.at(j))

that performs case-insensitive comparison. 执行不区分大小写的比较。 Start by writing a signature for it: 首先为其签名:

bool greaterThanIgnoreCase(const string& left, const string& right) {
    ...
}

Now you can replace your if condition with a call to this new function: 现在,您可以将if条件替换为对此新函数的调用:

if (greaterThanIgnoreCase(str.at(i), str.at(j)))

Finally, you need to provide an implementation of greaterThanIgnoreCase function. 最后,您需要提供greaterThanIgnoreCase函数的实现。 This is the core of the problem, so you would need to do it yourself. 这是问题的核心,因此您需要自己做。 The trick to it is using toupper or tolower function on each character of strings left and right , and compare them one character at a time. 诀窍是使用touppertolower是字符串的每个字符功能leftright ,并在同一时间对它们进行比较的一个字符。 If you run out of characters in one of the strings, the one with some characters remaining should be considered greater. 如果其中一个字符串中的字符用完,则应考虑剩余一些字符。

If you have a vector of string s then you could/should use the STL library, which offers sort or stable_sort for a given comparison function (that can be a simple lambda expression, functor, function,...). 如果您具有string s的vector ,则可以/应该使用STL库,该库为给定的比较函数(可以是简单的lambda表达式,函子,函数等)提供sortstable_sort

Update: You're not allowed to use built-in types except vector. 更新:除vector外,不允许使用内置类型。 In this case you can develop easily a replicate of what STL does. 在这种情况下,您可以轻松开发STL所做的复制。

Define a sort function that receives a comparator (for a vector of strings). 定义一个接收比较器(用于字符串向量)的排序函数。

The only thing you need to do is to compare lowercased characters. 您唯一需要做的就是比较小写字符。 If you are not allowed to use any built-in functions, you can do it manually. 如果不允许使用任何内置功能,则可以手动进行。

Since lowercase characters are located in ASCII table starting from 97, and uppercase - starting from 65, you can simply add 32 to the uppercase char to get its lowercase equivalent. 由于小写字符位于ASCII表中,从97开始,而大写字母-从65开始,因此您只需在大写字符中加上32即可得到等效的小写字母。

char lowerCase(char c)
{
    if (c >= 'A' && c <= 'Z') // if char is uppercase
        return (char)(c + 32); // return its lowercase equivalent
    else
        return c;
}

Then, you can do the following in your if condition: 然后,您可以在if条件下执行以下操作:

if (lowerCase(str.at(i)) > lowerCase(str.at(j)))

Note that you shouldn use lowerCase only when you compare, but not when you assign. 请注意,仅在比较时才应使用lowerCase ,而在分配时则不应使用。

Here is the working IDEOne demo . 这是正在运行的IDEOne演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM