
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How does a vector work in C/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
- Related Articles
- How does a vector work in C++?
- How does #include work in C++?
- How does Garbage Collector work in C#
- How does the Comma Operator work in C++
- How does generic lambda work in C++14?
- How does a kaleidoscope work?
- How does a Lock work?
- How does a vaccine work?
- How does a list work in Java?
- How does the compilation/linking process work in C/C++?
- How to append a vector in a vector in C++?
- How does a Lens antenna work?
- How does a packet filters work?
- How does a ClassLoader work in Java?\n
- How does jQuery.scrollTop() work?

Advertisements