Vector Operations
Advanced Vector Operations in C++
C++ Vector Operations Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec = {5, 2, 8, 1, 9, 3};
cout << "Original vector: ";
for (int num : vec) {
cout << num << " ";
}
cout << endl;
// Sort vector
sort(vec.begin(), vec.end());
cout << "Sorted vector: ";
for (int num : vec) {
cout << num << " ";
}
cout << endl;
// Reverse vector
reverse(vec.begin(), vec.end());
cout << "Reversed vector: ";
for (int num : vec) {
cout << num << " ";
}
cout << endl;
// Find element
auto it = find(vec.begin(), vec.end(), 5);
if (it != vec.end()) {
cout << "Element 5 found at index: " << distance(vec.begin(), it) << endl;
}
// Insert element
vec.insert(vec.begin() + 2, 100);
cout << "\nAfter inserting 100 at index 2: ";
for (int num : vec) {
cout << num << " ";
}
cout << endl;
// Erase element
vec.erase(vec.begin() + 2);
cout << "After erasing element at index 2: ";
for (int num : vec) {
cout << num << " ";
}
cout << endl;
// Clear vector
vec.clear();
cout << "\nSize after clear: " << vec.size() << endl;
cout << "Is empty: " << (vec.empty() ? "Yes" : "No") << endl;
return 0;
}Original vector: 5 2 8 1 9 3 Sorted vector: 1 2 3 5 8 9 Reversed vector: 9 8 5 3 2 1 Element 5 found at index: 2 After inserting 100 at index 2: 9 8 100 5 3 2 1 After erasing element at index 2: 9 8 5 3 2 1 Size after clear: 0 Is empty: Yes
Understanding Vector Operations
This program teaches you how to perform Advanced Vector Operations in C++. Vectors support various operations like sorting, reversing, finding, inserting, and erasing elements. These operations use iterators to traverse and manipulate elements efficiently.
---
1. What This Program Does
The program demonstrates advanced vector operations:
These operations enable powerful vector manipulation.
---
2. Header Files Used
---
3. Understanding Vector Operations
Operation Types
:
Iterators
:
---
4. Sorting Vector
Using sort()
:
sort(vec.begin(), vec.end());
How it works
:
Example
:
{5, 2, 8, 1, 9, 3} → {1, 2, 3, 5, 8, 9}
---
5. Reversing Vector
Using reverse()
:
reverse(vec.begin(), vec.end());
How it works
:
Example
:
{1, 2, 3, 5, 8, 9} → {9, 8, 5, 3, 2, 1}
---
6. Finding Elements
Using find()
:
auto it = find(vec.begin(), vec.end(), 5);
How it works
:
Checking Result
:
if (it != vec.end()) {
int index = distance(vec.begin(), it);
} else {
// Element not found
}
---
// Element found7. Inserting Elements
Using insert()
:
vec.insert(vec.begin() + 2, 100);
How it works
:
Example
:
---
8. Erasing Elements
Using erase()
:
vec.erase(vec.begin() + 2);
How it works
:
Example
:
---
9. Clearing Vector
Using clear()
:
vec.clear();
How it works
:
Checking Empty
:
vec.empty() // Returns true if size is 0
---
10. When to Use These Operations
Best For
:
Performance Considerations
:
---
11. Important Considerations
Iterators
:
Performance
:
Modifying Operations
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning advanced STL operations, understanding iterators, and preparing for complex algorithms and data manipulation in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Vector Operations
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.