Dave's Brain

Browse - Programming Tips - How do I convert an integer index into a STL vector into

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); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.