Vectores C++ STL: Obtener iterador de índice?


Por lo tanto, escribí un montón de código que accede a los elementos en un vector stl por índice [], pero ahora necesito copiar solo un trozo del vector. Parece que vector.insert(pos, first, last) es la función que quiero... excepto que sólo tengo primero y último como ints. ¿Hay alguna forma agradable de obtener un iterador de estos valores?

Author: Andy, 2009-03-22

5 answers

Prueba esto:

vector<Type>::iterator nth = v.begin() + index;
 246
Author: dirkgently,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-22 18:42:49

Forma mencionada por @ dirkgently ( v.begin() + index ) agradable y rápido para vectores

Pero std::advance( v.begin(), index ) la forma más genérica y para iteradores de acceso aleatorio también funciona en tiempo constante.

EDITAR
diferencias en el uso:

std::vector<>::iterator it = ( v.begin() + index );

O

std::vector<>::iterator it = v.begin();
std::advance( it, index );

Añadido después de las notas @litb.

 75
Author: bayda,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-20 20:18:03

También; auto it = std::next(v.begin(), index);

Actualización: Necesita un compilador compatible con C++11x

 36
Author: Viktor Sehr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-03 08:49:01

O puede utilizar std::advance

vector<int>::iterator i = L.begin();
advance(i, 2);
 7
Author: TimW,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-13 13:52:01

Actutally std::vector está destinado a ser utilizado como pestaña C cuando sea necesario. (C++ estándar solicita que para la implementación de vectores, por lo que sé - reemplazo de matriz en Wikipedia) Por ejemplo, es perfectamente legal hacer lo siguiente, según mí:

int main()
{

void foo(const char *);

sdt::vector<char> vec;
vec.push_back('h');
vec.push_back('e');
vec.push_back('l');
vec.push_back('l');
vec.push_back('o');
vec.push_back('/0');

foo(&vec[0]);
}

Por supuesto, foo no debe copiar la dirección pasada como parámetro y almacenarla en algún lugar, o debe asegurarse en su programa de nunca enviar ningún elemento nuevo en vec, o solicitar cambiar su capacidad. O riesgo fallo de segmentación...

Por lo tanto, en su ejemplo conduce a

vector.insert(pos, &vec[first_index], &vec[last_index]);
 -3
Author: yves Baumes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-03-26 23:12:56