
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Does a Vector Work in C++
In this tutorial, we will be discussing a program to understand how vectors work in C/C++.
A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.
This provides flexibility and reduces the time requirement with arrays to copy the previous elements to the newly created array.
Example
#include <iostream> #include <vector> using namespace std; int main(){ vector<int> myvector{ 1, 2, 3, 5 }; myvector.push_back(8); //not vector becomes 1, 2, 3, 5, 8 for (auto x : myvector) cout << x << " "; }
Output
1 2 3 5 8
Advertisements