Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created June 20, 2019 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5cd3c44a13e5e40807b161de6740e52e to your computer and use it in GitHub Desktop.
Save parzibyte/5cd3c44a13e5e40807b161de6740e52e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
int main() {
std::vector<int> numeros;
// Se agregan al final con push_back
numeros.push_back(50);
numeros.push_back(70);
numeros.push_back(80);
// Imprimirlos para ver contenido
std::cout << "Imprimiendo" << std::endl;
for (std::size_t i = 0; i < numeros.size(); i++) {
std::cout << numeros[i] << std::endl;
}
// Ahora con insert.
// Insertar al inicio
numeros.insert(numeros.begin(), 100);
// Insertar en la posición 1
numeros.insert(numeros.begin() + 1, 500);
// Insertar en la penúltima posición
numeros.insert(numeros.end() - 1, 1000);
// Imprimirlos para ver contenido
std::cout << "Imprimiendo" << std::endl;
for (std::size_t i = 0; i < numeros.size(); i++) {
std::cout << numeros[i] << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment