如何将字符串更改为大写?

时间:2014-01-26 12:54:17

标签: c++

我正在尝试从cin获取一系列单词并将值存储在a中 向量。阅读完所有单词后,处理向量并进行更改 每个单词都是大写的。

#include<iostream>
#include<string>
#include<cctype>
#include<vector>

using namespace std;

int main() {
    string line;
    vector<string> words;
    while( getline(cin, line) ) {
        words.push_back(line);
    }
    for (auto &c : words) {
        c = toupper(c);
        cout << c << endl;
    }
}

我在c = toupper(c)收到错误。请帮忙。

||=== Build: Debug in test (compiler: Cygwin GNU GCC Compiler) ===|
C:\Users\Ajay\Desktop\test\test\main.cpp||In function ‘int main()’:|
C:\Users\Ajay\Desktop\test\test\main.cpp|15|error: no matching function for call to ‘toupper(std::basic_string<char>&)’|
C:\Users\Ajay\Desktop\test\test\main.cpp|15|note: candidates are:|
\usr\include\ctype.h|20|note: int toupper(int)|
\usr\include\ctype.h|20|note:   no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘int’|
\usr\lib\gcc\i686-pc-cygwin\4.8.2\include\c++\bits\locale_facets.h|2596|note: template<class _CharT> _CharT std::toupper(_CharT, const std::locale&)|
\usr\lib\gcc\i686-pc-cygwin\4.8.2\include\c++\bits\locale_facets.h|2596|note:   template argument deduction/substitution failed:|
C:\Users\Ajay\Desktop\test\test\main.cpp|15|note:   candidate expects 2 arguments, 1 provided|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

1 个答案:

答案 0 :(得分:2)

    for(auto &word : words){
        for (auto &c : word) {
            c = ::toupper(c);
            cout << c << endl;
        }
    }