简体   繁体   English

如何在 C++ 中比较字符串数组

[英]How to compare an array of strings in C++

I am trying to compare two strings in my program so that I can arrange them in dictionary order.我试图在我的程序中比较两个字符串,以便我可以按字典顺序排列它们。 I am not allowed to declare an array like this: string arr[10] , instead I have to declare it like this: char arr[10][20] , where the [10] denotes the number of strings and the [20] denotes the size of each string.我不允许像这样声明一个数组: string arr[10] ,而是我必须像这样声明它: char arr[10][20] ,其中[10]表示字符串的数量和[20]表示每个字符串的大小。

To compare two strings I tried using something like: arr[5]>arr[7] , but it didn't work.为了比较两个字符串,我尝试使用类似的东西: arr[5]>arr[7] ,但没有用。 Any ideas?有任何想法吗?

https://www.techonthenet.com/c_language/standard_library_functions/string_h/strcmp.php https://www.techonthenet.com/c_language/standard_library_functions/string_h/strcmp.php

strcmp() should do the trick strcmp() 应该可以解决问题

Since you are limited to char arrays instead of string literals look at C documentation instead of C++ documentation.由于您仅限于字符数组而不是字符串文字,请查看 C 文档而不是 C++ 文档。 I assume you are a student as I had the same limitations.我假设你是一名学生,因为我有同样的限制。

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

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