इनिशियलाइज़ेशन वेक्टर कई तरह से किया जा सकता है
1) एक वेक्टर को push_back() विधि द्वारा प्रारंभ करें
एल्गोरिदम
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.
उदाहरण
#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;
} आउटपुट
Vector elements: 6 7 10 12
2) सरणी के आधार पर सदिश प्रारंभ करें
एल्गोरिदम
Begin Create a vector v. Initialize vector like array. Print the elements. End.
उदाहरण
#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;
} आउटपुट
vector elements: 1 2 3 4 5 6 7
3) किसी वेक्टर को दूसरे वेक्टर से प्रारंभ करें
एल्गोरिदम
Begin Create a vector v1. Initialize vector v1 by array. Initialize vector v2 by v1. Print the elements. End.
उदाहरण
#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;
} आउटपुट
vector elements: 1 2 3 4 5 6 7
4) आकार और तत्वों को निर्दिष्ट करके एक वेक्टर प्रारंभ करें
एल्गोरिदम
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.
उदाहरण
#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;
} आउटपुट
vector elements: 7 7 7 7 7