看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《MrPanda (不人氣揪團師)》之銘言: : 標題: [問題] 找出句子中最常的單字(C++) : 時間: Sun Dec 10 13:03:18 2017 : ~ : 餵入的資料(Input): : : 條件 : 1. 遇到符號'.'為結數字元 : 2. 以空白鍵當作做為區隔單字識別字元 : 3. 長度一樣則輸出第一個 : 輸入測試字串 : I am a normal ptt user like everybody. : Hello world. : : 推 loveflames: 建議你把cppreference的list initialization看個一輪 12/12 15:00 : → loveflames: 問題通通迎刃而解 12/12 15:00 : → galic: 樓上示範一下如何在讀完你說的參考資料以後 寫出"迎刃而解" 12/12 17:34 : → galic: 的程式碼 12/12 17:34 路過獻醜一下XD ===================== #include <iostream> #include <string> #include <list> int main() { std::list<std::string> l; std::string str; do { std::cin >> str; if (str[str.length()-1] == '.') { std::string str2 = str.substr(0, str.length()-1); l.push_back(str2); } else { l.push_back(str); } } while (str[str.length()-1] != '.'); l.sort([](std::string &lhs, std::string &rhs) { return (lhs.length() > rhs.length()) ? true : false; }); // 輸出最長單字(在最前面) std::cout << std::endl << l.front() << std::endl; return 0; } =========================== 初來乍到的,規矩多有不熟悉^^" https://ideone.com/eKv1BS -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.124.20 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1513072065.A.548.html
peterwu4: 好像不是在講list,不過用container蠻好做的 12/12 18:02
※ 編輯: peterwu4 (36.224.124.20), 12/13/2017 07:41:15
s25g5d4: 從頭掃一次就能抓到最長的單字,何必用 sort? 12/13 20:22
s25g5d4: 我認真看了一下,推文說的是 list initialization, 你回 12/13 20:31
s25g5d4: 這個完全是不一樣的東西啊,這是 STL container 耶 12/13 20:31
peterwu4: 是啊,搞錯了 12/14 00:51
TitanEric: list的sort超花時間的 不過這裡也不用sort就對了 12/14 12:25
dustlike: 這個記憶體搬移量看得我全身都不舒服惹 12/15 15:21
peterwu4: 可以去cplusplus或是cppreference看一下list這個contai- 12/19 11:30
peterwu4: ner的特性,它和vector實現的方式是不一樣的~ 12/19 11:30
loveflames: vector可以直接當陣列用 12/20 09:47