Vector in C++ is a sequence container representing arrays that can change in size (dynamic allocation)
- Vector uses dynamic allocation in which it allocates additional size with more element insertion, whereas Array is a fixed size container which the size can not be reset once initialized
- Vector occupies much more memory in exchange while the Array is a memory efficient data structure
- Vector is from the C++ template class while the Array is the lower-level data structure within C++
- When data are need to be saved dynamically (i.e. - when the amount of data is not fixed)
- When insertion / removal of data in the middle of the structure is not frequent
- When the amount of data needed to be saved is minimal
- When randomly accessing the index of the data
Code
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
for (vector<int>::size_type i = 0; i < v.size(); ++i)
cout << v[i] << endl;
return 0;
}
Output
10
20
30
40
50
Function push_back() adds a new element at the end of the vector.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
for (vector<int>::size_type i = 0; i < v.size(); ++i)
cout << v[i] << endl;
cout << endl;
v.pop_back(); // delete last element 50
v.pop_back(); // delete last element 40
for (vector<int>::size_type i = 0; i < v.size(); ++i)
cout << v[i] << endl;
return 0;
}
10
20
30
40
50
10
20
30
Function pop_back() removes the last element in the vector
Vector is convenienet to access the target element using the index.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
cout << v[0] << endl;
cout << v[1] << endl;
cout << v[2] << endl;
return 0;
}
10 // 0th element of vector "v";
20 // 1st element of vector "v";
30 // 2nd element of vector "v";
As the picture shown above, when performing the middle deletion of element "D" of the third index, entire elements of the vector must be adjusted hence hindering the efficiency.
Assuming there is a vector size of 10,000 consisted of random characters.
If wish to find unique character "K", coincidentally located at the last index of the vector, then the program will perform 10,000 searches to generate the result.
#inlcude <vector> // imports vector class from C++ std
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v; // declare int-type vector name "v"
vector<int> v(10); // vector of ten elements of 0
vector<int> v(10, 5); // vector of ten elements of 5
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
v.insert(1, 15); // insert element 15 at index 1
v.pop_back();
v.pop_back();
v.pop_back();
return 0;
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
v.insert(1, 15); // insert element 15 at index 1
v.pop_back();
v.pop_back();
v.pop_back();
cout << v.size() << endl; // vector "v" has a size of 3
}
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
v.insert(1, 15); // insert element 15 at index 1
v.pop_back();
v.pop_back();
v.pop_back();
cout << v.empty() << endl; // whether the vector is empty or not
}
If you're in search of the ultimate relaxation experience and want to invest in a massage chair that delivers unparalleled comfort, quality, and functionality, I wholeheartedly recommend Massage Chair Genius. visit, click for more info, Use this website, click to read more, find more info.