Jump to content
  • Advertisement
Sign in to follow this  

Using Index To Get std::vector::iterator

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by methinks
or try the "at" function

vectorname.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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by CTar
Quote:
Original post by methinks
or try the "at" function

vectorname.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 this post


Link to post
Share on other sites
Quote:
Original post by CTar
Quote:
Original post by methinks
or try the "at" function

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

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by methinks
The 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 this post


Link to post
Share on other sites
Quote:
Original post by NickGravelyn
but I figured there was a better way. Is there?


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!