Re: [問題] 關於uva上轉uppercase的問題

看板C_and_CPP (C/C++)作者 (朴髮箍)時間15年前 (2011/06/15 16:58), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串2/2 (看更多)
※ 引述《fftsquall (just only you know)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : uva online charge : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : #include<iostream> : #include<fstream> : #include<string> : #include<vector> : #include<cctype> : #include<algorithm> : 問題(Question): : 我在寫10010的時候 : 使用了 : std::transform(targetString.begin(), targetString.end(), targetString.begin(), std::toupper); : 但是會有compilation error : code.cpp:85: error: no matching function for call to 'transform(__gnu_cxx::__normal_iterator, : 但是如果把 std::toupper 換成 ::toupper就會AC : 想請問為什麼會這樣 : 餵入的資料(Input): : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : 補充說明(Supplement): 因為 std::transform() 不要求傳進來的 UnaryOperator/Binary- Operation 必須為可配接的(Adaptable), 所以 ::toupper 即可通 過編譯. - 你用的 std::toupper() 事實上是在 <locale> 底下的這個版本: template <class charT> charT toupper( charT c, const locale& loc ); 它是一個 function template而且參數有兩個. 為了符合你的需求 , 必須使用 std::bind2nd() 把第二個引數給固定好, 所以變成這 樣: std::bind2nd( std::toupper<char>, std::locale() ); 但是 std::bind2nd() 的第一引數必須是可配接的, 所以要改成這 樣才能真正通過編譯. std::bind2nd( std::ptr_fun(std::toupper<char>), std::locale() ); - std::ptr_fun 是可以將一般函式變為可配接物件的 convenient function. -- ▂▂ ▄▂ T.T.L Listen 2 http://ppt.cc/jIUk ˇ ˇˇ ˇ 說什麼結束 ▃▃ http://ppt.cc/zQtB ψ髮箍 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.121.197.115

06/15 22:02, , 1F
~"~一堆眉角
06/15 22:02, 1F

06/15 22:30, , 2F
@@?
06/15 22:30, 2F

06/15 22:56, , 3F

06/15 22:57, , 4F
謝謝謝解答 不過我template懂太少
06/15 22:57, 4F

06/15 22:59, , 5F
沒有很了解你的意思 是誤用到其他namespace的upper?
06/15 22:59, 5F
有 std::toupper(), 但使用方法跟 ::toupper() 不同, 因為是模 板的關係, 給 std::transform() 參考必須指定一個函式實體 std::toupper<char> 再來你想呼叫的是使用 UnaryOperator版本的演算法, 所以在引數 個數上要做一個轉換. ※ 編輯: loveme00835 來自: 140.121.197.115 (06/16 05:46)
文章代碼(AID): #1D-7Gf9l (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1D-7Gf9l (C_and_CPP)