# Using Index To Get std::vector::iterator

This topic is 4499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a technique for getting just an iterator for a certain spot in a vector simply using an index number? I know I can just use the [] operator to get the value, but I have an iterator that I need to be pointed at it. I have a crude solution using this:
void GUILayout::WidgetAtIndex(UINT index)
{
currentWidget = widgets.begin();
for(UINT i = 0; i < index; i++)
currentWidget++;
}
but I figured there was a better way. Is there?

##### Share on other sites
myvector.begin() + yourindex;

##### Share on other sites
or try the "at" function

vectorname.at(index)

vector<char> list;list.push_back("a");list.push_back("b");list.push_back("c");output(list.at(0)); //output "a"output(list.at(1)); //output "b"output(list.at(2)); //output "c"

Of course you would probably just put the above code into a for loop...

##### Share on other sites
Quote:
 Original post by methinksor try the "at" functionvectorname.at(index)*** Source Snippet Removed ***Of course you would probably just put the above code into a for loop...

What would that help? He needs an iterator.

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by methinksor try the "at" functionvectorname.at(index)*** Source Snippet Removed ***Of course you would probably just put the above code into a for loop...

What would that help? He needs an iterator.

Well it wouldn't even work... he's attempting to put constant char arrays into a vector of chars... :P

##### Share on other sites
Quote:
Original post by CTar
Quote:
 Original post by methinksor try the "at" functionvectorname.at(index)*** Source Snippet Removed ***Of course you would probably just put the above code into a for loop...

What would that help? He needs an iterator.

The at() allows you to use an integer as iterator, so that you use the vector the same way you would use an array. For example

for(int i = 0; i < vectorname.size(); i++){    output("vectorname.at(i)");}

as for the example I put up top, it probably wasn't the best... = P

 Ok, I see now what you mean... I guess I misunderstood the original question

##### Share on other sites
I don't think you understand the definition of iterator in a C++ context. at() does not return an iterator, it returns a reference or a const_reference. It cannot be used to specify an insertion point via insert() or an erasure point for erase(), nor can the return value at() be used with any of the functions in the <algorithm> header to operate on the contents of the vector.

##### Share on other sites
Quote:
 Original post by methinksThe at() allows you to use an integer as iterator, so that you use the vector the same way you would use an array.
I think you still may be missing the point of the original post. Aside possibly from bounds checking behavior, at() is more or less equivalent to operator[](), and the OP has already said that:
Quote:
 I know I can just use the [] operator to get the value...
He doesn't just want the value, he specifically wants an iterator pointing to the element in question, which the solution posted earlier will give.

##### Share on other sites
Like I said, I misunderstood the original question

##### Share on other sites
Quote:
 Original post by NickGravelynbut I figured there was a better way. Is there?

void GUILayout::WidgetAtIndex(UINT index){    currentWidget = widgets.begin();    std::advance(currentWidget, index);}

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632955
• Total Posts
3009450
• ### Who's Online (See full list)

There are no registered users currently online

×