C ++ STL | Crear e inicializar un vector : Aquí, vamos a aprender cómo crear un vector y cómo inicializar utilizando diferentes formas en STL C ++?
¿Cuál es el vector?
Vector es un contenedor en STL C ++, que se utiliza para representar la matriz y su tamaño se puede cambiar.
Leer más: C ++ STL vector
siguientes son las diferentes formas mediante el uso de ellos podemos crear e inicializar un vector de STL C ++,
1) Crear un vector vacío y initialize empujando los valores
//Create an empty vector and initialize by pushing values
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1;
//pushing the elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the vector elements
//creating iterator to access the elements
vector<int>::iterator it;
cout << "Vector v1 elements are: ";
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
salida
Vector v1 elements are: 10 20 30 40 50
2) crear un vector mediante la especificación de los elementos de tamaño e inicializar con un valor class
//Create a vector by specifying the size and
//initialize elements with a default value
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1(5, 10);
//printing the vector elements
//creating iterator to access the elements
vector<int>::iterator it;
cout << "Vector v1 elements are: ";
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
salida
Vector v1 elements are: 10 10 10 10 10
3) Crear un vector y inicializarlo como un array
//Create a vector and initialize it like an array
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50 };
//printing the vector elements
//creating iterator to access the elements
vector<int>::iterator it;
cout << "Vector v1 elements are: ";
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
salida
Vector v1 elements are: 10 20 30 40 50
4) crear un vector y inicializarlo de una matriz
//Create a vector and initialize it from an array
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//array
int arr[] = { 10, 20, 30, 40, 50 };
//vector declaration from the array
//array size
int size = sizeof(arr) / sizeof(arr[0]);
vector<int> v1(arr, arr + size);
//printing the vector elements
//creating iterator to access the elements
vector<int>::iterator it;
cout << "Vector v1 elements are: ";
for (it = v1.begin(); it != v1.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
salida
Vector v1 elements are: 10 20 30 40 50
5) crear un vector e inicializar desde otro vector
//Create a vector and initialize it from another vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector
vector<int> v1{ 10, 20, 30, 40, 50 };
//vector declaration from the vector
vector<int> v2(v1.begin(), v1.end());
//printing the vector elements
//creating iterator to access the elements
vector<int>::iterator it;
cout << "Vector v2 elements are: ";
for (it = v2.begin(); it != v2.end(); it++)
cout << *it << " ";
cout << endl;
return 0;
}
salida
Vector v2 elements are: 10 20 30 40 50