1:使用“》”、“!=”、“>=”等比較運算符可以比較兩個字符串的內容。比較的方法是將兩個string字符串從頭開始比較每一個字符,直到出現兩者不一致。比較這兩個不相同的字符的字面值,得出相應的結果。代碼如下:

// 6.21.cpp : 定義控制台應用程序的入口點。 // #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main(int argc, _TCHAR* argv[]) { string s1; string s2; cout<<"請輸入兩個字符串"<<endl; cin>>s1; cin>>s2; if(s1 == s2) { cout<<"兩個字符串相等"<<endl; } else if(s1>s2) { cout<<"第一個字符大於第二個字符串 "<<endl; } else { cout<<"第二個字符大於第二個字符串 "<<endl; } return 0; }
運行結果: