最近學習了關聯容器和疊代器, 在《C++ Primer》中, 有這樣一個題目 :

給定文本, 將其中的字元進行轉換, 轉換為對應的字元

儲存於 "exchange.txt" (實際檔案中沒有 "->") :

brb -> be right back

k -> okay

y -> why

r -> are

u -> you

pic -> picture

thk -> thanks!

l8r -> later

需要轉換的文本, 儲存於 "in.txt" :

where r u ? y don't u send me a pic ? k thk l8r

轉換結果, 輸出結果存儲於 "out.txt" :

where are you ? why don't you send me a picture ? okay thanks later

根據概述, 我們會用到如下 C++ 語法 :

  • 例外處理
  • fstream 與 fstream 的疊代器
  • deque
  • map

我們首先從 "exchange.txt" 檔案中讀取所有的轉換單詞到 map 中, 之後讀取所有 "in.txt" 檔案中的單詞到 deque, 最終利用疊代器在 deque 中進行遍歷, 並且在 map 中進行搜尋是否有對應的轉換, 有的話輸出轉換的單詞到存儲檔案, 否則按照原來的樣子輸出


以下是我所寫的實戰程式碼, 對於每一步都有詳細的輸出, 並且基本上每一步也都有非常詳盡的註解

大家可以作為參考. 不過最重要的就是自己實戰, 因為沒有實戰, 你永遠都只能是原地踏步

#include <fstream>
#include <map>
#include <deque>

/* Name Space for the programme. */
namespace {
    using std::string;
    typedef std::ifstream ifstream;
    typedef std::ofstream ofstream;
    using oIt = std::ostream_iterator<string>;      //The iterator for ostream is templated by string.
    using iIt = std::istream_iterator<string>;      //The iterator for istream is templated by string.
    using std::map;
    typedef map<string, string> stringDictionary;
    using std::deque;
    using std::out_of_range;
    typedef std::ios mode;
}
int main(int argc, char *argv[]) {
    ifstream in("in.txt", mode::in);        //Open in std::ios::in mode.
    iIt inIterator(in);
    iIt end;        //The end of istream_iterator.
    stringDictionary m;        //Using for saving string dictionary.
    while(inIterator != end) {
        string word;
        word = *inIterator++;       //Indexing word.
        m[word] = *inIterator++;        //Create the dictionary.
    }
    in.close();
    deque<string> q;        //Using for saving the sentences that needs to be exchanged.
    in.open("exchange.txt", mode::in);      //Open in std::ios::in mode.
    iIt exchangeIterator(in);
    /* Save the sentences to deque. */
    while(exchangeIterator != end) {
        q.push_back(*exchangeIterator++);
    }
    auto queueIterator {q.cbegin()};
    ofstream out("out.txt", mode::trunc & mode::out);       //Open in the mode of std::ios::trunc and std::ios::out.
    oIt outIterator(out, " ");      //Every printing would be added an empty character.
    auto queueEnd {q.cend()};
    while(queueIterator != queueEnd) {
        /* If cannot find the string in the dictionary, then print directly.
         * If can find the string in the dictionary, then print the exchange word. */
        try {
            /** "be_right_back" will be exchanged to "be right back". **/
            if(m.at(*queueIterator) == "be_right_back") {
                outIterator = "be right back";
                ++queueIterator;
                continue;
            }
            outIterator = m.at(*queueIterator);
        }catch(out_of_range &e) {
            /** Catch the exception. **/
            outIterator = *queueIterator;
        }
        ++queueIterator;
    }
}

最終的檔案內容 :

【C++ 實戰】關聯容器與資料流疊代器-Jonny'Blog