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
請按任意鍵繼續. . .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM