简体   繁体   English

如何比较两个大小不同的字符串

[英]How to compare two strings that are different sizes

Lets just say string A = "ABCDEF" , string B = "ABC" 假设字符串A = "ABCDEF" ,字符串B = "ABC"

Is there a way to compare both these strings character by character? 有没有办法逐个字符地比较这两个字符串?

For example: Lets say you wanted to iterate through each string, 例如:假设您要遍历每个字符串,

 for(size of strings)
 if(A.at(i) == B.at(i)
 {
   do something
 }
 else
 {
   do something else
 }

You couldn't do it in a for loop since they're different sizes, any other suggestions? 您无法在for循环中执行此操作,因为它们的大小不同,还有其他建议吗?

You couldn't do it in a for loop since they're different sizes, 您无法在for循环中执行此操作,因为它们的大小不同,

You absolutely can do it in a loop. 您绝对可以循环执行。 You can use the following algorithm: 您可以使用以下算法:

  • Compare lengths of the strings 比较字符串的长度
  • Store the shorter length in n 将较短的长度存储在n
  • Do n iterations in loop 循环执行n次迭代
  • Decide what you want to do with rest of the longer string 确定要对其余长字符串进行的处理

You could also take a look at something like std::lexogrphical_compare . 您也可以看一下类似std::lexogrphical_compare东西。 It is used to sort words as they would be sorted in the dictionary. 它用于对单词进行排序,就像在字典中对单词进行排序一样。 If you want to ensure that characters themselves are sorted in a different order, you can provide a function that does that comparison. 如果要确保字符本身以不同的顺序排序,则可以提供一个进行比较的函数。 For example, let's imagine that you want a letter 'c' to actually be sorted as a 'k'. 例如,假设您希望字母'c'实际上被排序为'k'。 You can accomplish that in the following manner: 您可以通过以下方式完成此操作:

bool comp(char c1, char c2)
{
  c1 = std::tolower(c1);
  c2 = std::tolower(c2);
  c1 = (c1=='c') ? 'k' : c1;
  c2 = (c2=='c') ? 'k' : c2;
  return c1 < c2;
}

// main.cpp
std::string str1 = "string c";
std::string str2 = "string d";
std::string str3 = "string k";

std::cout << std::boolalpha;
std::cout << str1 << ( std::lexographical_compare(str1, str2, comp) ? "<" : ">=" ) << str2 << std::endl;
std::cout << str1 << ( std::lexographical_compare(str1, str3, comp) ? "<" : ">=" ) << str3 << std::endl;
std::cout << str2 << ( std::lexographical_compare(str2, str3, comp) ? "<" : ">=" ) << str3 << std::endl;

if you use std::string, for comparison you can either use the compare function or use relational operators, provided with this class. 如果使用std :: string,则可以使用该类提供的compare函数或关系运算符compare

std::string str1 ("green apple");
std::string str2 ("red apple");

if (str1.compare(str2) != 0)
  std::cout << str1 << " is not " << str2 << '\n';

or 要么

if (str1 != str2) std::cout << "str1 and str2 are not equal\n";

all other relational operators, ie, == < > <= >= are also available. 所有其他关系运算符,即== <> <=> =也都可用。 It looks like in your case you only need == of != . 在您的情况下,您只需要== !=

if you insist on comparing strings char by char, than you can do it in a loop in several ways. 如果您坚持要逐个字符比较字符串,那么可以通过几种方式循环进行。 For example, 例如,

bool equal = true;
if (str1.length() != str2.length())
     equal = false;
else 
     for (int i = 0; equal && i < str1.length(); i++) 
        if (str1.at(i) != str2.at(i) 
           equal = false;

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

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