Vector!
Vector ke parameter hishabe function e pass korle setar ekta copy create hoy..jeta time abong memory consuming
tai:
void my_function(vector<int>v)
na dia
void my_function(vector<int> &v)
dewa bhalo
vector copy nia:
code:
tai:
void my_function(vector<int>v)
na dia
void my_function(vector<int> &v)
dewa bhalo
v.size() dia vector empty kina ta ber korar theke v.empty() use kora bhalo..karon sob empty vector size empty day na alltime
vector copy nia:
code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
unsigned int i;
vector<int>first;
vector<int>second(4,100);
//4 bar 100 input hoise
for(i=0; i<second.size(); i++)
cout<<second[i]<<" ";
cout<<endl;
vector<int>third(second.begin(),second.end()-1);
//-1 dewar jonno sob element copy hoy nai ses r ta bad gese
for(i=0; i<third.size(); i++)
cout<<third[i]<<" ";
cout<<endl;
vector<int>fourth(third);
//third full copy hoise
for(i=0; i<fourth.size(); i++)
cout<<fourth[i]<<" ";
cout<<endl;
int a[]= {1,2,3,5,6,7,7,2,3,1};
vector<int>fifth(a,a+sizeof(a)/sizeof(int));
//array ta vector e copy howa gelo
for(i=0; i<fifth.size(); i++)
{
cout<<fifth[i]<<" ";
}
cout<<endl;
first.resize(20);
//first vector er size 0 chilo..resize function dia 20 korsi
for(i=0; i<first.size(); i++)
first[i]=i+1;
for(i=0; i<first.size(); i++)
cout<<first[i]<<" ";
cout<<endl;
for(i=0;i<5;i++)
first.push_back(i+20);
//push_back dia vector er ses theke input dewa hoise
//ete vector er size auto change howa gese
for(i=0;i<first.size();i++)
cout<<first[i]<<" ";
cout<<endl;
return 0;
}
ekhane i ke unsigned int hisabe newa hoise karon vector.size() unsigned function ..tai unsigned e na rakle warning dekhabe but code run korbe :)
this creates arrays of vector,100 vectors
lets create a 2D vector and print it!
Input:
Output:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m,i,j;
cin>>n>>m;
vector< vector<int> >v2d(n,vector<int>(m,9));
cout<<"initialization e je man dewa seta print korbe\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<v2d[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
vector< vector<int> >::iterator a;
vector<int>::iterator b;
cout<<"iterator dia new man assign korlam\n\n";
for(a=v2d.begin(); a!=v2d.end(); a++)
{
for(b=a->begin(); b!=a->end(); b++)
{
*b=1;
}
}
cout<<"indexing dia man gula print korlam\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<v2d[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
cout<<"indexing dia new man assign korlam\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
v2d[i][j]=2;
}
}
cout<<"iterator dia print korlam\n\n";
for(a=v2d.begin(); a!=v2d.end(); a++)
{
for(b=a->begin(); b!=a->end(); b++)
{
cout<<*b<<" ";
}
cout<<endl;
}
cout<<"\n";
}
#include<bits/stdc++.h>
int main()
CODE:
#include<bits/stdc++.h>
int main()
for(i=0; i<n; i++)
cout<<"erase element position?:\t";
if(x<yoyo.size())
for(i=0; i<n; i++)
cout<<endl<<"erase range ?:\t";
if(max(x,y)<yoyo.size())
vector<int>::iterator itr;
for(itr=yoyo.begin(); itr!=yoyo.end(); itr++)
yoyo.clear();
OUTPUT:
10
1
erase range ?: 0 5
7 //>deleted 1 2 3 5 9 in range 0-5(not a part in the output)
Process returned 0 (0x0) execution time : 46.943 s
vector.push_back() always starts from the last
int main()
#Upper To Lower:
#include<bits/stdc++.h>
int main()
Method 2:
#include<bits/stdc++.h>
int main()
more way to use vector!
The vector.rbegin() and vector.rend()
Finding The MAX element!
Finding The Minimum element!
A Reverse ITERATOR!
Macro!
this creates arrays of vector,100 vectors
vector<int>v1[100];
this creates vector with 100 zeros
vecotr<int>v2(100);
to create a 3D structure
vector<int>[100][2];
that creates an 2D array (100x2) of vectors and as vectors are 1D so in total 3D
So we can create multi dimensional Structures where everything is DYNAMIC
lets create a 2D vector and print it!
Input:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
vector< vector<int> >v2d;
//a vector in a vector..creating a 2d of vector
vector<int>temp;
for(int i=0;i<10;i++)
{
temp.clear();
for(int j=0;j<=i;j++)
{
temp.push_back(i);
}
v2d.push_back(temp);
}
//using index to print
for(unsigned int i=0;i<v2d.size();i++)
{
for(unsigned int j=0;j<v2d[i].size();j++)
{
cout<<v2d[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
Output:
0
1 1
2 2 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
Process returned 0 (0x0) execution time : 0.073 s
Press any key to continue.
here we used index here but we can also use ITETARATOR!
Code:
vector< vector<int> >::iterator out;
vector<int>:: iterator in;
for(out=v2d.begin();out!=v2d.end();out++)
{
for(in=out->begin();in!=out->end();in++)
{
cout<<*in<<" ";
}
cout<<endl;
}
//here in=out->begin() is equivalent to in=(*out).begin()
//here cout<<*in<<" "; prints the value in *in address
to initialize a 2d vector's size we can follow this:
int m,n;
vector< vector<int> > M(n, vector<int>(m, -1));
so that's creates a 2D vector , n*m filled with -1
CODE:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
cin>>n>>m;
vector< vector<int> >v2d(n,vector<int>(m,-1));
vector< vector<int> >::iterator a;
vector<int>::iterator b;
for(a=v2d.begin();a!=v2d.end();a++)
{
for(b=a->begin();b!=a->end();b++)
{
cout<<*b<<" ";
}
cout<<endl;
}
}
CODE:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
cin>>n>>m;
vector< vector<int> >v2d(n,vector<int>(m,-1));
vector< vector<int> >::iterator a;
vector<int>::iterator b;
for(a=v2d.begin();a!=v2d.end();a++)
{
for(b=a->begin();b!=a->end();b++)
{
cout<<*b<<" ";
}
cout<<endl;
}
}
Iterator is faster then Index method but hard also :p
As vector is in template class we can create any kind of vector's of data in it :)
like:
vector< string >yoyo(20,"disting disting");
Vector e value asign:
Code:
using namespace std;
#define ll long long
int main()
{
int n,m,i,j;
cin>>n>>m;
vector< vector<int> >v2d(n,vector<int>(m,9));
cout<<"initialization e je man dewa seta print korbe\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<v2d[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
vector< vector<int> >::iterator a;
vector<int>::iterator b;
cout<<"iterator dia new man assign korlam\n\n";
for(a=v2d.begin(); a!=v2d.end(); a++)
{
for(b=a->begin(); b!=a->end(); b++)
{
*b=1;
}
}
cout<<"indexing dia man gula print korlam\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cout<<v2d[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n";
cout<<"indexing dia new man assign korlam\n\n";
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
v2d[i][j]=2;
}
}
cout<<"iterator dia print korlam\n\n";
for(a=v2d.begin(); a!=v2d.end(); a++)
{
for(b=a->begin(); b!=a->end(); b++)
{
cout<<*b<<" ";
}
cout<<endl;
}
cout<<"\n";
}
VECTOR e vector.pop_back() vector.push_back() er opposite
eta dia vactor er ses data delete howa jay
CODE:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
vector< string >names;
string a;
for(int i=0; i<10; i++)
{
getline(cin,a);
getchar();
//to kill the enter
names.push_back(a);
}
while(!names.empty())
{
cout<<"last name was: "<<names.back()<<endl;
names.pop_back();
}
cout<<"Vector is now empty\n";
}
vector.resize() dia vector ke jekono size e ante pari....size komaite pari ba baraiteo pari
vector.erase() dia jekono ekta element/ekta range er sob element delete korte parbo
vector.clean() dia pura vector er sob element delete korte parbo
orthat vector r size 0 howa jabe
CODE:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int i,x,n;
cin>>n;
vector< int >yoyo;
for(i=0; i<n; i++)
{
cin>>x;
yoyo.push_back(x);
}
cout<<"erase element position?:\t";
cin>>x;
cout<<endl;
if(x<yoyo.size())
yoyo.erase(yoyo.begin()+x);
for(i=0; i<n; i++)
{
cout<<yoyo[i]<<endl;
}
cout<<endl<<"erase range ?:\t";
int y;
cin>>x>>y;
cout<<endl;
if(max(x,y)<yoyo.size())
yoyo.erase(yoyo.begin()+min(x,y),yoyo.begin()+max(x,y));
vector<int>::iterator itr;
for(itr=yoyo.begin(); itr!=yoyo.end(); itr++)
{
cout<<*itr<<endl;
}
yoyo.clear();
cout<<"vector is cleaned now, size : "<<yoyo.size()<<endl;
yoyo.resize(10);
cout<<"vector is resized to : "<<yoyo.size()<<endl;
}
OUTPUT:
10
1
2
3
4
5
9
7
6
5
4
erase element position?: 3
1
2
3 //>deleted 4(not a part in the output)
5
9
7
6
5
4
4
erase range ?: 0 5
7 //>deleted 1 2 3 5 9 in range 0-5(not a part in the output)
6
5
4
vector is cleaned now, size : 0
vector is resized to : 10
//>resized to 10 and filled with 0s automatically(not a part in the output)
Process returned 0 (0x0) execution time : 46.943 s
Press any key to continue.
vector.push_back() always starts from the last
>if the vector size if 10 it will push values from 10
>if the vector size is 0 it will push values from 0
Vector Sorting:
#Lower to Upper:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,i,x;
cin>>n;
vector<ll>test;
for(i=0;i<n;i++)
{
cin>>x;
test.push_back(x);
}
cout<<endl<<"Lower To Upper Sorting"<<endl;
sort(test.begin(),test.end());
vector<ll>::iterator itr;
for(itr=test.begin();itr!=test.end();itr++)
{
cout<<*itr<<" ";
}
cout<<endl;
}
#Upper To Lower:
Method 1:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,i,x;
cin>>n;
vector<ll>test;
for(i=0;i<n;i++)
{
cin>>x;
test.push_back(x);
}
cout<<endl<<"Upper To Lower Sorting"<<endl;
sort(test.rbegin(),test.rend());
vector<ll>::iterator itr;
for(itr=test.begin();itr!=test.end();itr++)
{
cout<<*itr<<" ";
}
cout<<endl;
}
Method 2:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n,i,x;
cin>>n;
vector<ll>test;
vector<ll>::iterator itr;
for(i=0;i<n;i++)
{
cin>>x;
test.push_back(x);
}
cout<<endl<<"Upper To Lower Sorting"<<endl;
sort(test.begin(),test.end());
reverse(test.begin(),test.end());
for(itr=test.begin();itr!=test.end();itr++)
{
cout<<*itr<<" ";
}
cout<<endl;
}
The vector.rbegin() and vector.rend()
Finding The MAX element!
Finding The Minimum element!
A Reverse ITERATOR!
Macro!
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন