C++ 入門指南 4.01
練習 24.7 參考程式 - 練習標準程式庫的 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 s = "There is no spoon.";
// 尋找是否有 "is" 子字串
string::size_type n = s.find("is");
if (n != std::string::npos) {
cout << n << endl;
}
else {
cout << -1 << endl;
}
// 尋找索引值 8 之後是否有 "is" 子字串
n = s.find("is", 8);
if (n != std::string::npos) {
cout << n << endl;
}
else {
cout << -1 << endl;
}
// 最後回傳 0 給作業系統
return 0;
}
/* 《程式語言教學誌》的範例程式
http://kaiching.org/
檔名:exercise2407.cxx
編譯:g++ exercise2407.cxx
執行:./a.out
功能:C++入門指南單元二十四的練習
作者:張凱慶 */
回到練習題目