微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 C++ 中将向量中的所有元素转换为大写

如何解决如何在 C++ 中将向量中的所有元素转换为大写

我想在一个向量中显示所有元素。我的代码显示所有元素,但仅将最后一个转换为大写。

#include <iostream>
#include <vector>
#include <string>
using namespace std;


int main()
{
    string words;
    string it;
    vector<string>list_words;

    while (cin>>words)
        list_words.push_back(words);
            for (auto it = words.begin(); it != words.end() && !isspace(*it); ++it)
                *it = toupper(*it);
                cout<<list_words.size()<<endl;
                cout<<words<<endl;
                



    return 0;
}

解决方法

我再次检查了如何索引每个“单词”。

#include <iostream>
#include <string>
#include<vector>
using std::cin;
using std::cout;
using std::endl;
using namespace std;

int main()
{
    string word;
    char index;
    vector<string>g_words;
        while (cin>> word)
            g_words.push_back(word);
                for (auto &word : g_words)
                    for (decltype (word.size()) index = 0;
                        index != word.size() && !isspace(word[index]); ++index)
                            word[index] = toupper(word[index]);
                                for ( auto word : g_words)
                                    cout<<word<<endl;

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。