C++ 入門指南 4.01
練習 24.4 參考程式 - 練習標準程式庫的 string
// 引入標準程式庫中相關的輸入、輸出程式
#include <iostream>
// 引入標準程式庫中相關的 string
#include <string>
// cout 為 std 中的輸出物件
using std::cout;
// endl 為 std 中的斷行符號
using std::endl;
// string 為 std 中字串型態
using std::string;
int main(void) {
// 設定字串
string s1 = "Free your mind.";
string s2 = "Free your hands.";
// 比較完整字串
cout << s1.compare(s2) << endl;
// 比較子字串
cout << s1.compare(0, 4, s2, 0, 4) << endl;
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise2404.cxx
編譯:g++ exercise2404.cxx
執行:./a.out
功能:C++入門指南單元二十四的練習
作者:張凱慶 */
回到練習題目