Hi,

I'm trying to copy a value in a vector to a string. The vector is also of string type, I'm trying to copy through the iterator.

for(iter = inputStr.begin(); iter != inputStr.end(); iter++){
        string temp = inputStr.at(iter);
        int count = 0;

        for(int i = 0; i != temp.size(); i++){
            while(temp[i] != 'h')
                count++;
        }

        temp = temp.erase(0, count);
        inputStr.at(iter) = temp;
    }

Thanks

Edited 6 Years Ago by kenji: n/a

Your for loop is problematic. When i =0 it gets stuck since temp[0] !='h' is true.
Try

int count = 0;
while(temp[count] != 'h' && count<temp.size())
                count++;

Edited 6 Years Ago by jonsca: n/a

>> I'm trying to copy a value in a vector to a string. The vector is also of string type, I'm trying to copy through the iterator

So you are trying to copy the strings inside a vector into an array ? or into
another vector ? or into another string variable.

It works. At first I was a little skeptical...

What he's doing is getting a vector full of words that may have an 'h' in them, lopping off the letters before the first h and then writing the modified string back into the vector.

Hi,

I tried that but I'm still getting an error, well a couple of errors but from what I can gather the error is because of the way I'm trying to assign my vector value to my string variable.

string temp = inputStr.at(iter);

The error I get is this: error C2664: 'const std::basic_string<_Elem,_Traits,_Ax> &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'unsigned int'

string temp = *iter;

My simulation of your program wasn't structured properly.

Edited 6 Years Ago by jonsca: n/a

Doh! I knew I was making a silly mistake, haven't used the standard library in so long that its bitten me in the behind.

This question has already been answered. Start a new discussion instead.