Vector C++
Introduction
A vector is a separately included library. Do not be surprised if you have studied a younger programming language before, in which similar operations came «out of the box»
A vector contains a set of values of one type. That is, either all
int
or all
string
The size does not have to be given in advance. Memory is allocated as needed, in other words dynamically.
Every element of a vector is easy to access by its index.
You can study the theory, for example, in
Wikipedia
Example
#include <iostream>
#include <vector>
using namespace std;
int main() {
// 1. create a vector
vector <int> vec0;
cout << endl << "1. vector vec0 size is " << vec0.size();
if (vec0.empty())
{
cout << endl << "vector vec0 is empty" << endl;
}
else
{
cout << endl << "vector vec0 is not empty" << endl;
}
// 2. create vector with 5 elements each element is 4
vector <int> vec(5, 4);
cout << endl << "2. vector vec size is " << vec.size();
if (vec.empty())
{
cout << endl << "vector vec is empty" << endl;
}
else
{
cout << endl << "vector vec is not empty" << endl;
}
// 3. go through vector elements with a standard loop
cout << endl << "3. ";
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
// 4. go through vector elements in a shorter way
cout << "4. ";
for (auto item : vec)
{
cout << item << " ";
}
// 5. add new elements to the end
vec.push_back(7);
vec.push_back(8);
vec.push_back(9);
cout << endl << "5. new elements are added with push_back";
// 6. check that they are at the end
cout << endl << "6. ";
for (auto item : vec)
{
cout << item << " ";
}
// 7. remove elements from the end
vec.pop_back();
vec.pop_back();
// 8. check that elements are removed
cout << endl << "8. ";
for (auto item : vec)
{
cout << item << " ";
}
// 9. copy vector
vector <int> vec_two(vec);
cout << endl << "9. vec_two size is " << vec_two.size() << endl;
// 10. check vec_two content
cout << endl << "10. ";
for (auto item : vec_two)
{
cout << item << " ";
}
// 11. check if vectors are identical
cout << endl << "11. ";
if (vec == vec_two)
{
cout << "identical";
}
else {
cout << "not identical";
}
cout << endl;
return 0;
}
1. vector vec0 size is 0 vector vec0 is empty 2. vector vec size is 5 vector vec is not empty 3. 4 4 4 4 4 4. 4 4 4 4 4 5. new elements are added with push_back 6. 4 4 4 4 4 7 8 9 8. 4 4 4 4 4 7 9. vec_two size is 6 10. 4 4 4 4 4 7 11. identical
Print a vector of integers
Task
Create a vector of integers vi from 0 to 9 and print it on the screen.
Print the size of the vector vi.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> vi;
for (int i = 0; i < 10; i++)
{
vi.push_back(i);
}
for (auto item : vi)
{
cout << item << " ";
}
cout << endl << endl;
cout << "int vector vi has " << vi.size() << " elements. " << endl;
cout << endl;
return 0;
}
0 1 2 3 4 5 6 7 8 9 int vector vi has 10 elements.
Print the elements one by one
Change the values of several elements by hand
vi[5] = 3;
vi[6] = -1;
vi[1] = 99;
Print the changed vector on the screen in another way:
for (unsigned int i = 0; i < vi.size(); i++)
{
cout << vi[i] << " ";
}
cout << endl;
Count how many elements of the vector are equal to 3. To print the vector let us use a new way.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for sort and count
using namespace std;
int main()
{
vector<int> vi;
for (int i = 0; i < 10; i++)
{
vi.push_back(i);
}
vi[5] = 3;
vi[6] = -1;
vi[1] = 99;
for (auto i = begin(vi); i != end(vi); i++)
{
cout << *i << " ";
}
cout << endl;
int threes = count(begin(vi), end(vi), 3);
cout << "vector of ints has " << threes << " elements with value 3";
cout << endl;
return 0;
}
0 99 2 3 4 3 -1 7 8 9 vector of ints has 2 elements with value 3
Read three words from the keyboard into a vector, print them on the screen, sort them alphabetically and count how many letters o there are in the first word.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for sort and count
using namespace std;
int main()
{
vector<string> vs;
cout << "enter three words ";
for (int i = 0; i < 3; i++)
{
string s;
cin >> s;
vs.push_back(s);
}
for (auto item : vs)
{
cout << item << " ";
}
cout << endl;
sort(begin(vs), end(vs));
for (auto item : vs)
{
cout << item << " ";
}
cout << endl;
int os = count(begin(vs[0]), end(vs[0]), 'o');
cout << "first word has " << os << " letter o's ";
return 0;
}
enter three words heihei andreyolegovich topbicycle heihei andreyolegovich topbicycle andreyolegovich heihei topbicycle first word has 2 letter o's
The sum of numbers in a string
Task: a string of numbers separated by commas is given.
We need to:
print the numbers on the screen one by one.
calculate the sum of the numbers
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
string str = "12,1,214,3234";
string s;
vector <int> vec;
int x;
for (int i = 0; i < str.length(); i++)
{
if (str[i] != ',')
{
s = s + str[i];
}
else
{
cout << s << endl;
x = atoi(s.c_str());
s.clear();
vec.push_back(x);
}
}
cout << s << endl;
x = atoi(s.c_str());
s.clear();
vec.push_back(x);
int sum = 0;
for (auto item : vec)
{
sum = sum + item;
cout << item << " ";
}
cout << endl << "Sum of numbers in the string = " << sum << endl;
return 0;
}
12 1 214 3234 12 1 214 3234 Sum of numbers in the string = 3461
Create a two-dimensional vector
Task: create a two-dimensional vector 3 by 5 and fill it with random numbers from 0 to 9
#include <iostream>
#include <vector>
#include <cstdlib> // for rand()
using namespace std;
int main() {
vector<vector<int>> vec;
for (int i = 0; i < 3; i++)
{
vector <int> temp;
for (int j = 0; j < 5; j++)
{
temp.push_back(rand()%10);
}
vec.push_back(temp);
}
cout << endl;
for (int vecline = 0; vecline < 3; vecline++)
{
for (int vecelem = 0; vecelem < 5; vecelem++)
{
cout << vec[vecline][vecelem] << " ";
}
cout << endl;
}
return 0;
}
12 1 214 3234 12 1 214 3234 Sum of numbers in the string = 3461
Video
The code on this page was run with g++ 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Andrei Olegovich