codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

82 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/&lt;vector&gt; Reference
⚡C++ Programming

&lt;vector&gt; Reference

Updated 2026-05-12
30 min read

<vector> Reference

The &lt;vector&gt; container is one of the most commonly used containers in C++ due to its dynamic array capabilities. It provides a flexible way to store elements that can grow and shrink as needed, making it ideal for applications where the number of elements isn't known at compile time.

In this tutorial, we'll explore all the essential methods provided by the &lt;vector&gt; container, including push_back, pop_back, insert, erase, resize, reserve, capacity, shrink_to_fit, data, and emplace. Understanding these methods will help you effectively manage dynamic arrays in your C++ programs.

Introduction

The &lt;vector&gt; container is part of the Standard Template Library (STL) and provides a dynamic array that can resize itself automatically when elements are added or removed. This makes it a versatile choice for various programming tasks, from simple data storage to complex algorithms.

In this tutorial, we'll cover each method in detail, providing clear explanations, code examples, and expected outputs. By the end of this guide, you'll have a solid understanding of how to use &lt;vector&gt; effectively in your C++ projects.

Core Content

1. push_back

The push_back method adds an element to the end of the vector.

Example:

push_back.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec;
6 vec.push_back(10);
7 vec.push_back(20);
8
9 for (int num : vec) {
10 std::cout << num << " ";
11 }
12
13 return 0;
14}

Output:

Output
10 20

2. pop_back

The pop_back method removes the last element from the vector.

Example:

pop_back.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 vec.pop_back();
7
8 for (int num : vec) {
9 std::cout << num << " ";
10 }
11
12 return 0;
13}

Output:

Output
10 20

3. insert

The insert method adds an element at a specified position in the vector.

Example:

insert.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 auto it = vec.insert(vec.begin() + 1, 15);
7
8 for (int num : vec) {
9 std::cout << num << " ";
10 }
11
12 return 0;
13}

Output:

Output
10 15 20 30

4. erase

The erase method removes an element from a specified position in the vector.

Example:

erase.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 auto it = vec.erase(vec.begin() + 1);
7
8 for (int num : vec) {
9 std::cout << num << " ";
10 }
11
12 return 0;
13}

Output:

Output
10 30

5. resize

The resize method changes the size of the vector to a specified number of elements.

Example:

resize.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 vec.resize(2);
7
8 for (int num : vec) {
9 std::cout << num << " ";
10 }
11
12 return 0;
13}

Output:

Output
10 20

6. reserve

The reserve method allocates enough memory to hold a specified number of elements, without changing the size of the vector.

Example:

reserve.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec;
6 vec.reserve(10);
7
8 std::cout << "Capacity: " << vec.capacity() << std::endl;
9
10 return 0;
11}

Output:

Output
Capacity: 10

7. capacity

The capacity method returns the maximum number of elements that the vector can hold without needing to reallocate memory.

Example:

capacity.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 std::cout << "Capacity: " << vec.capacity() << std::endl;
7
8 return 0;
9}

Output:

Output
Capacity: 4

8. shrink_to_fit

The shrink_to_fit method reduces the capacity of the vector to fit its size, freeing up memory.

Example:

shrink_to_fit.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 vec.reserve(10);
7 std::cout << "Capacity before shrink_to_fit: " << vec.capacity() << std::endl;
8
9 vec.shrink_to_fit();
10 std::cout << "Capacity after shrink_to_fit: " << vec.capacity() << std::endl;
11
12 return 0;
13}

Output:

Output
Capacity before shrink_to_fit: 10
Capacity after shrink_to_fit: 3

9. data

The data method returns a pointer to the underlying array used by the vector.

Example:

data.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec = {10, 20, 30};
6 int* arr = vec.data();
7
8 for (size_t i = 0; i < vec.size(); ++i) {
9 std::cout << arr[i] << " ";
10 }
11
12 return 0;
13}

Output:

Output
10 20 30

10. emplace

The emplace method constructs an element in-place at a specified position, avoiding unnecessary copying or moving of elements.

Example:

emplace.cpp
1#include <iostream>
2#include <vector>
3#include <string>
4
5int main() {
6 std::vector<std::string> vec;
7 vec.emplace_back("Hello");
8 vec.emplace_back("World");
9
10 for (const auto& str : vec) {
11 std::cout << str << " ";
12 }
13
14 return 0;
15}

Output:

Output
Hello World

Practical Example

Let's put all these methods together in a practical example. We'll create a program that manages a list of integers, allowing users to add, remove, and display elements.

vector_practical.cpp
1#include <iostream>
2#include <vector>
3
4int main() {
5 std::vector<int> vec;
6 int choice, num;
7
8 while (true) {
9 std::cout << "1. Add element
102. Remove last element
113. Display elements
124. Exit
13";
14 std::cout << "Enter your choice: ";
15 std::cin >> choice;
16
17 switch (choice) {
18 case 1:
19 std::cout << "Enter an integer to add: ";
20 std::cin >> num;
21 vec.push_back(num);
22 break;
23 case 2:
24 if (!vec.empty()) {
25 vec.pop_back();
26 } else {
27 std::cout << "Vector is empty!" << std::endl;
28 }
29 break;
30 case 3:
31 for (int n : vec) {
32 std::cout << n << " ";
33 }
34 std::cout << std::endl;
35 break;
36 case 4:
37 return 0;
38 default:
39 std::cout << "Invalid choice!" << std::endl;
40 }
41 }
42
43 return 0;
44}

Summary

MethodDescription
push_backAdds an element to the end of the vector.
pop_backRemoves the last element from the vector.
insertInserts an element at a specified position in the vector.
eraseRemoves an element from a specified position in the vector.
resizeChanges the size of the vector to a specified number of elements.
reserveAllocates enough memory to hold a specified number of elements.
capacityReturns the maximum number of elements that the vector can hold without reallocating.
shrink_to_fitReduces the capacity of the vector to fit its size, freeing up memory.
dataReturns a pointer to the underlying array used by the vector.
emplaceConstructs an element in-place at a specified position, avoiding unnecessary copying or moving.

What's Next?

Now that you have a comprehensive understanding of the &lt;vector&gt; container and its methods, you can move on to learning about other STL containers and algorithms. The next topic is "<algorithm> Reference," where we'll explore various algorithms available in the C++ Standard Library and how to use them effectively.

Stay tuned for more tutorials and happy coding!


Previous<ctime> ReferenceNext <algorithm> Reference

Recommended Gear

<ctime> Reference<algorithm> Reference