Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c++11 - how to convert to uppcase all the elements in a vector in C++

I want to display all the elements in a vector. My code displays all elements but converts to uppercase just the last one.

#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;
}
question from:https://stackoverflow.com/questions/66054755/how-to-convert-to-uppcase-all-the-elements-in-a-vector-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I checked again how to index each "word".

#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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...