作者timrau (精神战力薄弱)
看板EE_DSnP
标题Re: [问题] string 的toupper
时间Mon Apr 13 10:27:24 2009
用个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)