GuoXin Li's Blog

std::vector in c++

字数统计: 1k阅读时长: 6 min
2020/09/23 Share

std::vector in C++

Declaration of std::vector

1
std::vector<int> marks;

initialization of std::vector

1
std::vector<int> marks = {50, 45, 47, 65, 80};
1
2
std::vector<int> marks;
marks = {50, 45, 47, 65, 80};

Length of std::vector

marks.size() — function

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>

int main()
{
std::vector<int> marks = {50, 45, 47, 65, 80};
marks = {50, 47, 60};
std::cout << "length of array : " << marks.size() << std::endl;
return 0;
}

Pass a std::vector to function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;

void printVector( const vector<int> &v ){
for(int i = 0; i < v.size(); i++){
cout << "v[" << i << "] = " << v[i] << endl;
// v[2] = 4;
}
}

int main() {
std::cout << "Hello, World!" << std::endl;
vector<int> v = {1,2,3,4,5,6,7};
printVector(v);
return 0;
}

noted from: https://www.codesdope.com/cpp-stdvector/

void printVector(const std::vector\ &n) - const is used here to prevent the compiler from making a copy of the vector and this enhances the performance. The passed vector will be n in this function as &n is the parameter of the function ‘printArray’.

Screen Shot 2020-09-23 at 23.11.10

front & back

1
2
3
4
//usage of front;
marks.front();
//usage of back;
marks.back();

empty

1
marks.empty() //if true, return 1; else return 0;

resize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v1 = {5, 6};
v1.resize(5);
for(int i = 0; i < v1.size() ; i++)
{
cout << v1[i] << endl;
}
return 0;
}

1
2
3
4
5
5
6
0
0
0

assign

assigns new contents to the vector and replaces its current contents all.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v;
v.assign(7, 40); // 7 elements each of value 40
cout << v.size() << endl;
return 0;
}

push_back

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v = {4, 5, 6, 7, 8};
v.push_back(47);

//printing values of v
cout << "elements of v" << endl;
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}

return 0;
}

pop_back

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v = {4, 5, 6, 7, 8};
v.pop_back();

//printing values of v
cout << "elements of v" << endl;
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}

return 0;
}

reserve

This function increases the capacity of the vector if the desired number of elements is greater than the capacity of the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v1;
for(int i = 0; i < 50; i++)
{
v1.push_back(1);
}
v1.reserve(100);
cout << "capacity : " << v1.capacity() << endl;
return 0;
}

1
capacity:100

erase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
vector<int> v1 = {4, 5, 6, 7, 8};
vector<int> v2 = {1, 2, 3, 4, 5};

v1.erase(v1.begin()+4); // removing a single element at position 4
v2.erase(v2.begin()+1, v2.begin()+3); // removing range of elements from position 1 till 2

//printing the values of v1
cout << "Values of v1" << endl;
for(int i = 0; i < v1.size(); i++)
{
cout << v1[i] << endl;
}

//printing the values of v2
cout << "Values of v2" << endl;
for(int i = 0; i < v2.size(); i++)
{
cout << v2[i] << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
Values of v1
4
5
6
7
Values of v2
1
4
5

clear

clear all elements of the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> v1 = {1, 2, 3};

vector<int> v2 = {4, 5, 6};

v1.swap(v2);

std::cout << "Vector v1" << endl;
for (auto i: v1)
{
std::cout << i << endl;
}

std::cout << "Vector v2" << endl;
for (auto i: v2)
{
std::cout << i << endl;
}

return 0;
}

Multidimensional std::vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<vector<int>> v {{{1,2,3},{4,5,6},{7,8,9}}};
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout << v[i][j] << "\t";
}
cout << endl;
}
return 0;
}
1
2
3
1   2   3
4 5 6
7 8 9

Passing a multidimensional std::vector to a function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;

void display(const std::vector<std::vector<int>> &v)
{
for(int i=0; i<v.size(); i++)
{
for(int j = 0; j<v[i].size(); j++)
{
cout << v[i][j] << "\t";
}
cout << endl;
}
}

int main()
{
vector<vector<int>> v {{{1,2,3},{4,5,6},{7,8,9}}};
display(v);
return 0;
}
CATALOG