c++ string compare

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string str1("hello");
  string str2("hello");
  string str3("world");

  if (str1 != str2)
  {
    cout << "str1 不等于 str2" << endl;
  }

  if (str1 == str2)
  {
    cout << "str1 等于 str2" << endl;
  }

  if (str1>str3)
  {
    cout << "str1 大于 str3" << endl;
  }
  else
  {
    cout << "str1不大于str3" << endl;
  }

  if (str1.compare(str3)>0)
  {
    cout << "str1 大于 str3" << endl;
  }

  if (str1.compare(str3)<0)
  {
    cout << "str1 小于 str3" << endl;
  }

  if (str1.compare(str3)==0)
  {
    cout << "str1 等于 str3" << endl;
  }

  string str4(str3,2,3);
  cout << str4 << endl;
  if (str1.compare(str4)>0)
  {
    cout << "str1 大于 str4" << endl;
  }
  else
  {
    cout << "str1 不等于于 str4" << endl;
  }

  if (str1.compare(2,2,str2)>0)
  {
    cout << "str1 大于 str2" << endl;
  }

  system("pause");
  return 0;
}

=================================

str1 等于 str2
str1不大于str3
str1 小于 str3
rld
str1 不等于于 str4
str1 大于 str2
请按任意键继续. . .

c++ string compare

 

原文链接: https://www.cnblogs.com/herd/p/10995961.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ string compare

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296495

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午5:55
下一篇 2023年2月15日 下午5:55

相关推荐