首页 新闻 会员 周边

toupper()的参数只能是整型?

0
悬赏园豆:5 [已解决问题] 解决于 2016-08-18 23:12
#include<iostream>
#include<string>
#include<vector>
#include<cctype>

int main()
{
    std::vector<std::string> str{ "some string" };

    for (auto i = str.begin(); i != str.end() && !i->empty(); i++)
    {
        *i = toupper(*i);
        std::cout << *i;
    }
    std::cout <<  std::endl;
    return 0;
}

代码如上,我试过把std::vector<std::string>改为std::string,那样的话可以解决toupper的问题,但i->empty()出现错误。嗯,还请各位多多指教。

C++
爱是用心码不要说话的主页 爱是用心码不要说话 | 初学一级 | 园豆:154
提问于:2016-07-27 22:14
< >
分享
最佳答案
0

auto i = str.begin() 这个指向的i是string类型,toupper这个函数无法处理string串的

改为std::string后,auto i = str.begin() 这是i是char类型了,所以对了

收获园豆:5
2012 | 高人七级 |园豆:21230 | 2016-07-28 08:58

那我如何用迭代器来把str的元素改为大写呢?

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-07-28 12:47

#include <algorithm>

#include <string>

std::string data = "Abc";

std::transform(data.begin(), data.end(), data.begin(), ::tolower);

 

std::string str = "HELLO, WORLD!";

boost::algorithm::to_lower(str);

 

http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case

2012 | 园豆:21230 (高人七级) | 2016-07-28 13:17

@2012: 不好意思我之前没有说清楚,我现在在学C++迭代器这一段,书上一个题目说要我用迭代器这个机制将str改为大写然后输出,然后我就遇到了那个问题,其实重点不是大写,是那个toupper()的参数我很纠结,或者说我还可以用什么代替那个toupper()?

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-07-28 23:46

std::string str = "some string" ;
for (std::string::iterator i= str.begin(); i != str.end(); i++)
{
*i = toupper(*i);
std::cout << *i;
}
std::cout << std::endl;
std::cout << str << std::endl;

2012 | 园豆:21230 (高人七级) | 2016-07-29 10:33

@2012: 这么久没有结贴...

最近突然想到了另外一个函数来代替empty()

isspace(),试了一下,没有错哈,【高兴脸】~~~

爱是用心码不要说话 | 园豆:154 (初学一级) | 2016-08-18 16:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册