看板 EE_DSnP 關於我們 聯絡資訊
用個function object把toupper()包起來。 #include <iostream> #include <string> #include <cctype> #include <algorithm> using namespace std; struct MyToUpper { char operator()(char ch) const { return toupper(ch); } }; int main() { string s = "Clare"; // toUpper transform(s.begin(), s.end(), s.begin(), MyToUpper()); cout << s << endl; } --- 我猜<functional>裡應該有什麼東西可以把function直接包成function object 而不用手動寫一個,不過懶得查..... ※ 引述《wafu (wafu)》之銘言: : 請問一下, : 如果我想要一次對整個string作to upper, : 應該怎麼作? : 我找到的做法是 : http://www.cnblogs.com/oomusou/archive/2009/02/16/529409.html : 可是在我include完 : #include <algorithm> : #include <cctype> : 之後 : make時還是出現這樣的錯誤 : cmdParser.cpp:164: 錯誤: 對 transform(__gnu_cxx::__normal_iterator<char*, : std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, : __gnu_cxx::__normal_iterator<char*, std::basic_string<char, : std::char_traits<char>, std::allocator<char> > >, : __gnu_cxx::__normal_iterator<char*, std::basic_string<char, : std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded : function type>) 的呼叫沒有匹配的函式 : 不知道哪裡出問題了?? : 謝謝大家!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.171.138 ※ 編輯: timrau 來自: 140.112.171.138 (04/13 10:28)