Dave's Brain

Browse - programming tips - convert vector index to an iterator

Date: 2003Aug12
Language: C++

Q.  How do I convert an integer index into a STL vector into
and iterator?  I need an iterator for insert().

A.  Do this:

#include <vector>

typedef std::vector<char *> MYARRAY;

void insert_with_index(MYARRAY &a, const int index, char *s)
{
	MYARRAY::iterator	it;

	it = &a[index]; // Convert the index to an iterator

	it = a.begin() + index; // Another way

	a.insert(it, s);
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.