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

Recommended Answers

All 7 Replies

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++;

>> 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.

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.

No worries.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.