
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to initialize a vector in C++?
Initialization vector can be done in many ways
1) Initialize a vector by push_back() method
Algorithm
Begin Declare v of vector type. Call push_back() function to insert values into vector v. Print “Vector elements:”. for (int a : v) print all the elements of variable a. End.
Example
#include<iostream> #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; v.push_back(6); v.push_back(7); v.push_back(10); v.push_back(12); cout<<"Vector elements:"<<endl; for (int a : v) cout << a << " "; return 0; }
Output
Vector elements: 6 7 10 12
2) Initialize a vector by array
Algorithm
Begin Create a vector v. Initialize vector like array. Print the elements. End.
Example
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v{ 1, 2, 3, 4, 5, 6, 7 }; cout<<"vector elements:"<<endl; for (int a : v) cout << a << " "; return 0; }
Output
vector elements: 1 2 3 4 5 6 7
3) Initialize a vector from another vector
Algorithm
Begin Create a vector v1. Initialize vector v1 by array. Initialize vector v2 by v1. Print the elements. End.
Example
#include<iostream> #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1{ 1, 2, 3, 4, 5, 6, 7 }; vector<int> v2(v1.begin(), v1.end()); cout<<"vector elements:"<<endl; for (int a : v2) cout << a << " "; return 0; }
Output
vector elements: 1 2 3 4 5 6 7
4) Initialize a vector by specifying size and elements
Algorithm
Begin Initialize a variable s. Create a vector v with size s and all values with 7. Initialize vector v1 by array. Initialize vector v2 by v1. Print the elements. End.
Example
#include<iostream> #include <bits/stdc++.h> using namespace std; int main() { int s= 5; vector<int> v(s, 7); cout<<"vector elements:"<<endl; for (int a : v) cout << a << " "; return 0; }
Output
vector elements: 7 7 7 7 7
- Related Articles
- What is the easiest way to initialize a std::vector with hardcoded elements in C++?
- How to append a vector in a vector in C++?
- How to initialize a dynamic array in C++?
- How to initialize a const field in constructor?
- How to initialize a boolean array in JavaScript?
- how to initialize a dynamic array in java?
- How to initialize a rectangular array in C#?
- How to initialize variables in C#?
- How to initialize List in Kotlin?
- How to declare and initialize a dictionary in C#?
- How to declare and initialize a list in C#?
- How to initialize an array in Java
- How to initialize an array in C#?
- How to initialize jagged arrays in C#?
- How to initialize const member variable in a C++ class?

Advertisements