程式語言教學誌
首頁 書籍與課程 遊戲 App 教材目錄 如何自學 C C++ C# Java Python EPUB 舊站

C++ 入門指南 4.01

練習 24.4 參考程式 - 練習標準程式庫的 string

電子書購買頁面
Google Play - C++ 入門指南
Readmoo - C++ 入門指南
樂天 kobo - C++ 入門指南

// 引入標準程式庫中相關的輸入、輸出程式
#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++入門指南單元二十四的練習
   作者:張凱慶 */
回到練習題目

如果覺得這篇文章不錯~~

上一頁 練習 24.3 參考程式 - 練習標準程式庫的 string
回 C++ 入門指南 4.01 目錄
下一頁 練習 24.5 參考程式 - 練習標準程式庫的 string
回 C++ 教材
回程式語言教材首頁
PM 5:24:47

Facebook Youtube GitHub 微博