简体   繁体   English

如何比较一个字符的字母顺序是高还是低?

[英]How can I compare if a char is higher or lower in alphabetical order than another?

Pretty much as the title. 和标题差不多。 I'm writing a linked list and I need a function to sort the list alphabetically, and I'm pretty stumped. 我正在写一个链表,我需要一个函数按字母顺序对列表进行排序,这让我很困惑。 Not sure how this has never come up before, but I have no idea how to do it other than to create my own function listing the entire alphabet and comparing positions of letters from scratch. 不确定以前从未出现过这种方法,但是除了创建自己的函数来列出整个字母并从头比较字母的位置之外,我不知道该怎么做。

Is there any easy way to do this? 有没有简单的方法可以做到这一点?

Edit for clarity: 为清楚起见进行编辑:

I've got a linear linked list of class objects, each class object has a char name, and I'm writing a function to compare the name of each object in the list, to find the highest object alphabetically, and then find the next object down alphabetically, etc, linking them together as I go. 我有一个线性链接的类对象列表,每个类对象都有一个字符名称,并且我正在编写一个函数,以比较列表中每个对象的名称,按字母顺序找到最高的对象,然后找到下一个反对按字母顺序排列,依此类推,将它们连接在一起。 I already have a function that does this for an int field, so I just need to rewrite it to compare inequalities between alphabetical characters where a is largest and z is smallest. 我已经有一个针对int字段执行此操作的函数,因此我只需要重写它即可比较最大a和最小z的字母字符之间的不等式。

In hindsight that was probably a lot more relevant than I thought. 事后看来,这可能比我想象的要重要得多。

I think a couple of the answers I've gotten already should work so I'll pop back and select a best answer once I've gotten it working. 我认为我已经获得的几个答案应该可以使用,因此一旦我可以使用,我将弹出并选择最佳答案。

I'm also working with g++ and unity. 我也在使用g ++和unity。

I think that in general case the best approach will be to use std::char_traits: 我认为一般情况下,最好的方法是使用std :: char_traits:

char a, b;
std::cin >> a >> b;
std::locale loc;
a = std::tolower(a, loc);
b = std::tolower(b, loc);

std::cout << std::char_traits::compare(&a, &b, 1u);

But in many common situations you can simply compare chars as you do it with other integer types. 但是在许多常见情况下,您可以像使用其他整数类型一样简单地比较字符。

 #include <stdio.h>
 #include <ctype.h>

 void main(void) {
  char a = 'X', b = 'M';
  printf("%i\n", a < b);
  printf("%i\n", b < a);

  printf("%i\n", 'a' < 'B'); 
  printf("%i\n", tolower('a') < tolower('B'));
 }

prints out: 打印出:

 0
 1
 0
 1

char s are still numbers, and can be compared as such. char仍然是数字,因此可以进行比较。 The upper case letters and lower case letters are all in order, with the upper case letters before the lower. 大写字母和小写字母均按顺序排列,大写字母位于小写字母之前。 (Such that 'Z' < 'a'.) See an ASCII table . (例如'Z'<'a'。)请参见ASCII表

My guess is that your list contains char* as data (it better contain std::string s as data). 我的猜测是您的列表包含char*作为数据(最好包含std::string作为数据)。 If the list is composed of the latter, you can simply sort using the overloaded std::string 's operator< , like 如果列表由后者组成,则可以简单地使用重载的std::stringoperator<排序,例如

return str1 < str2; // true if `str1` is lexicographically before `str2` 

If your list is made of C-like null-terminated strings, then you can sort them using std::strcmp like 如果您的列表是由类似C的以null终止的字符串组成的,则可以使用std::strcmp对其进行排序

return std::strcmp(s1, s2); 

or use the std::char_traits::compare (as mentioned by @Anton) like 或使用std::char_traits::compare (如@Anton所述),例如

return std::char_traits<char>::compare(s1, s2, std::min(std::strlen(s1), std::strlen(s2)));

or sort them via temporary std::string s (most expensive), like 或通过临时std::string排序它们(最昂贵),例如

return std::string(s1) < std::string(s2); // here s1 and s2 are C-strings

If your list simply contains characters, then, as mentioned in the comments, 如果您的列表仅包含字符,则如注释中所述,

return c1 < c2; // returns true whenever c1 is before c2 in the alphabet

If you don't care about uppercase/lowercase, then you can use std::toupper to transform the character into uppercase, then always compare the uppercase. 如果您不关心大写/小写,则可以使用std::toupper将字符转换为大写,然后始终比较大写。

As you can see from this ASCII table, all of the alphanumeric characters appear in the correct alphabetical order, regarding their actual values: 从此ASCII表中可以看到,所有字母数字字符都以正确的字母顺序出现,关于它们的实际值:

在此处输入图片说明

"Is there any easy way to do this?" “有没有简单的方法可以做到这一点?”

So yes, comparing the character values will provide to have them sorted in alphabetical order. 所以可以,比较字符值将使它们按字母顺序排序。

would something like below suffice ? 下面的东西就足够了吗? convert everything to upper first. 一切都先转换为上位。

class compareLessThanChar{
public:
    bool operator()(const char a, const char b)
    { return toupper(a) < toupper(b); }
}


std::multiset<char, compareLessThanChar> sortedContainer;

暂无
暂无

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

相关问题 如何比较高阶订单类型? - How do I compare higher order types? 如何生成比之前生成的随机数更高或更低的随机数? - How can I generate a random number which is, higher or lower than the random number generated before? 如果差异高于/低于“x”,则比较两个数字,做一些事情 - Compare two numbers if the difference is higher/lower than 'x', do something 如何使用 QSortFilterProxyModel 按字母顺序对列表项进行排序? - How i can sort a list item by alphabetical order with QSortFilterProxyModel? 为什么我可以将值大于127的int传递给char数组,但不是直接? - Why can I pass an int with a value higher than 127 into a char array, but not directly? 如何在 C++ 类中重载“小于”运算符以比较常量? - How can I overload a 'less than' operator in my C++ class in order to compare consts? 如何在具有较高和较低数字的数组中使搜索算法适应复杂性(3n / 2)-2? - How can I adapt a search algorithm in an array of higher and lower numbers to complexity (3n / 2) - 2? 如何在C ++中将类的实例与char *变量进行比较? - how can I compare an instance of a class with a char * variable in c++? 如何使用重载 == 运算符将 class 中的 char 数组与另一个 char 数组进行比较? - How do I compare a char array from a class with another char array using the overload == operator? string :: compare是否可靠,以确定字母顺序? - Is string::compare reliable to determine alphabetical order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM