1

I'm working on right now with string in vector . And i got my self in dead end . I've manipulate with vector<int> elements, and understand how to work with them ! I know how to work with string ! But i can't get thru the part where i need to change string element value in vector .I mean i don't know what to do in loop with "do something". So to be short i give the task on witch i work right now.

Read a sequence of words from cin and store values in vector.After you've read all words,process the vector and change each word to uppercase

Here what I've got so far

int main ()  
{
    vector<string> words;    //Container for all input word
    string inp;              //inp variable will process all input 

    while (cin>>inp)         //read
       words.push_back(inp); //Storing words 

    //Processing vector to make all word Uppercase
    for (int i = 0; i <words.size(); ++i)
     //do something

         words[i]=toupper(i);
    for (auto &e : words)    //for each element in vector 
     //do something

     cout<<e;

    keep_window_open("~");
    return 0;
}  

This first for statement is not right i try access vector elements and change words to upper but it did't work for me its just sample
I've try many ways to access the vector element but when trying to use string member functions toupper() on vector i'm getting messy with code and logical mistakes!
Thanks For your time . Sorry for mistakes i made in spelling words

6
  • 1
    toupper only works on single characters. You might try combining it with std::transform. Commented Jun 8, 2013 at 23:43
  • toupper work with whole string in rangebase for statement. Commented Jun 8, 2013 at 23:45
  • 1
    Applying toupper to the index surely won't give anything useful. Commented Jun 8, 2013 at 23:45
  • possible duplicate of Convert a String In C++ To Upper Case Commented Jun 8, 2013 at 23:46
  • @StuartGolodetz Didnt find anything usefull out there ! Commented Jun 8, 2013 at 23:50

3 Answers 3

4

Try this:

for (auto& word: words)
  for (auto& letter: word)
    letter = std::toupper(letter);
Sign up to request clarification or add additional context in comments.

2 Comments

THANK YOU ! that what i need . OMG itslook so simple
There's no need to manually iterate through each char when you have ranged loops - By far this is the best solution!
2

This can be fixed by using the std::transform Standard algorithm to iterate through the characters of the words. You can also use std::for_each instead of the manual loop.

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

int main()  
{
    std::vector<std::string> words;
    std::string inp;

    while (std::cin >> inp)
       words.push_back(inp);

    std::for_each(words.begin(), words.end(), [] (std::string& word)
    {
        std::transform(
            word.begin(),
            word.end(), 
            word.begin(), (int (&)(int)) std::toupper
        );
    })

    for (auto &e : words)
        std::cout << e << std::endl;
}

And here is a demo.

5 Comments

Not working out ! std has no member toupper ! and also got 8 errors in that code all of them about transform function!
@AlexGreat Include the header <cctype> and use my latest update.
`Error 3 error C2440: '=' : cannot convert from 'int' to 'std::basic_string<_Elem,_Traits,_Alloc>'
Error 2 error C2664: 'int (int)' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'int'
@AlexGreat Okay, please use my newest update. I provided the entire new program.
0

You can do this in the first for loop:

string w = words.at(i);
std::transform(w.begin(), w.end(), w.begin(), ::toupper);

1 Comment

it helps ! but it uppercase first index element. I need uppercase all chars in vector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.