[英]problems using STL std::transform from cygwin g++
我在cygwin上運行g ++(gcc版本3.4.4)。
我無法獲得這小段代碼進行編譯。 我包括了適當的標題。
int main(){
std::string temp("asgfsgfafgwwffw");
std::transform(temp.begin(),
temp.end(),
temp.begin(),
std::toupper);
std::cout << "result:" << temp << std::endl;
return 0;
}
使用vector等STL容器時,我沒有任何問題。 是否有人對此情況有任何建議或見解? 謝謝。
#include <cctype> // for toupper #include <string> #include <algorithm> using namespace std; void main() { string s="hello"; transform(s.begin(), s.end(), s.begin(), toupper); }
las,上面的程序將無法編譯,因為名稱“ toupper”不明確。 它可以指的是:
int std::toupper(int); // from <cctype>
要么
template <class chart> charT std::toupper(charT, const locale&);// from <locale>
使用顯式強制轉換來解決歧義:
std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
這將指示編譯器選擇正確的toupper()。
可以歸結為以下代碼:
std::transform(temp.begin(),temp.end(),temp.begin(),static_cast<int (*)(int)>(std::toupper));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.