iterator loop thru string vector

I am trying to use to iterator to loop thru each element in a string vector, then loop thru each char in the string and change it to upper case using header cctype function toupper(). I cant understand how to loop thru the char... This is how I am doing it, what am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
#include <vector>
#include <string>
#include <cctype>


using namespace std;
vector<string> num{"user", "test"};
vector<string>::iterator lp;
vector<char>::iterator lp2;
int main()
{
    for(lp = num.begin(); lp != num.end(); ++lp) //loop thru each element
        for(lp2 = lp->begin(); lp2 != lp->end();++lp2)//loop thru each char in element
        lp2 = toupper(lp2);
        cout << *lp << endl;
return 0;
}


I get this error in compiler:

 
C:\Users\Pata The Great\Documents\c++\Testing Struct\main.cpp|14|error: no match for 'operator=' in 'lp2 = lp.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-><std::basic_string<char>*, std::vector<std::basic_string<char> > >()->std::basic_string<_CharT, _Traits, _Alloc>::begin<char, std::char_traits<char>, std::allocator<char> >()'|
Last edited on
Type of lp2 is not iterator to vector of char. Try string. Or auto, if your compiler supports C++11 sufficiently.
ok I understand now after playing a bit it should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
#include <vector>
#include <string>
#include <cctype>


using namespace std;
vector<string> num{"user", "test"};
vector<string>::iterator lp;
string::iterator lp2;
int main()
{
    for(lp = num.begin(); lp != num.end(); ++lp) {//loop thru each element
        for(lp2 = lp->begin(); lp2 != lp->end();++lp2)//loop thru each char in element
       * lp2 = toupper(*lp2);
        cout << *lp << endl;}
return 0;
}


Code works as written, thank you so much for your help!!
Last edited on
Topic archived. No new replies allowed.