# STL vector iterator assignment based on index

## Recommended Posts

id like to assign a std::vector::iterator based on user input that corresponds with the integer value of the index of another vector element. so...
class Blah
{
...
};

std::vector<Blah> blah;
int userInput = 0;
std::vector<Blah>::iterator itrBlah = blah.begin();

...

itrBlah = blah[userInput];

obviously that doesnt work. i get an error similar to
binary '=' : no operator found which takes a right-hand operand of type 'Blah' (or there is no acceptable conversion)
ive been googling and reading for a while, but i cant seem to sort out pointer math and apply it to this iterator situation. help please? ha. =)

##### Share on other sites
itrBlah = blah.begin() + userInput;

##### Share on other sites
ah, thank you. thats working, of course, but now the accessor function i use to get the relative index value is causing an access violation and i have no idea why. i'll pick this up tomorrow.

##### Share on other sites
Is the index within bounds (i.e. >= 0, and < the .size() of the vector)?

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627636
• Total Posts
2978331

• 10
• 12
• 22
• 13
• 33