Hi. I am trying to access and use the values stored in my vector (v) as the parameters for my substr function by using a vector iterator (I) but it seams like i can't use *I as parameters for my substr. Is there a way I can get *I to print the values of my substring from my vector? Any help will be appreciated. Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13
void ClassName::classFunctionName(const string& s, vector<int>& v)
{
vector<int>::iterator startPosition;
vector<int>::iterator length;
//startPosition = v.begin();
length = v.begin + 1; // trying to increment the iterator to access the next element
for(startPosition = v.begin(); startPosition < v.end(); startPosition++)
cout << s.substr(v.at( *startPosition), v.at( *length));
}
}
.at() doesn't take iterator parameters. You just pass it the numbers. And substr just takes numbers as well.
e.g.:
s.substr(2, 5); //get characters from 2-7
Also, .at() will return the element at that point, not where the element is.
If you have the "end" iterator (I'm assuming that is what length is), you can get a substr from the start to the end by just making a new string and append the data from the start till the end using a for loop.