#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//Initialize
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
int array[] = {100, 101, 102, 103};
vector<int> v1;
v1.assign(array, array + sizeof(array)/sizeof(array[0]));
vector<int> v2(array, array + sizeof(array)/sizeof(array[0]));
if(!v.empty())
cout<<"Not empty"<<endl;
//Insert
v.resize(15);
v[10] = 10;
v[11] = 11;
v.insert(v.begin()+12, 12);
v1.insert(v1.begin()+4, 2, 30);
v2.insert(v2.begin(), v1.begin(), v1.end());
v.push_back(15);
//Delete
v.erase(v.begin());
v.erase(v.begin(), v.begin()+2);
//Search
vector<int>::iterator it1 = find(v.begin(), v.end(), 7);
cout<<"Found: "<<*it1<<" Index: "<<it1-v.begin()<<endl;
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
//Initialize
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
int array[] = {100, 101, 102, 103};
vector<int> v1;
v1.assign(array, array + sizeof(array)/sizeof(array[0]));
vector<int> v2(array, array + sizeof(array)/sizeof(array[0]));
if(!v.empty())
cout<<"Not empty"<<endl;
//Insert
v.resize(15);
v[10] = 10;
v[11] = 11;
v.insert(v.begin()+12, 12);
v1.insert(v1.begin()+4, 2, 30);
v2.insert(v2.begin(), v1.begin(), v1.end());
v.push_back(15);
//Delete
v.erase(v.begin());
v.erase(v.begin(), v.begin()+2);
//Search
vector<int>::iterator it1 = find(v.begin(), v.end(), 7);
cout<<"Found: "<<*it1<<" Index: "<<it1-v.begin()<<endl;
if(v.empty())
cout<<"Empty"<<endl;
cout<<"Vector size: "<<v.size()<<endl;
//Traverse
cout<<"V: ";
for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout<<*it<<" ";
cout<<endl;
cout<<"V1: ";
for(vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
cout<<*it<<" ";
cout<<endl;
cout<<"V2: ";
for(vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
cout<<*it<<" ";
cout<<endl;
//Clear
v.clear();
}
No comments:
Post a Comment