0% found this document useful (0 votes)
25 views

C++ Program

Uploaded by

Nareshkumar B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

C++ Program

Uploaded by

Nareshkumar B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

vector.

cpp

#include<iostream>
#include<vector>

using namespace std;

int main()
{
vector <int> v;
for (int i=1; i <=5; i++)
v.push_back(i);
for(int i=0; i <=4; i++)
cout<<v[i]<<endl;
return 0;
}
run: g++ vector.cc -o vector

vector1.cpp
#include<iostream>
#include<vector>

int main()
{
vector <int> v;
for (int i =0; i <=5; i++)
v.push_back(i);

cout<<"size"<<v.size()<<endl; // size of the vector. - 5


cout<<"capacity:"<<v.capacity()<<endl; // size of memory allocated - 8
cout<<"maximumm:"<<v.max_size()<<endl;
-464616446146146464
if (v.empty()) // returns 1 if it is empty.
cout<<"empty"<<endl;
else
cout<<"vector is not empty"<<endl;

cout<<"first element"<<v.front()<<endl; - 1
cout<<"last element"<<v.back()<<endl; - 5
cout<<"3rd index element"<<v.at(3)<<endl; - 4

return 0;
}
run: g++ vector1.cpp -o vector1

#include<iostream>
#include<list>
#include<iterator>

int main()
{
list <int> numbers;
numbers.push_back(10);
numbers.push_front(5);
numbers.push_back(20);
numbers.push_back(25);
numbers.pop_back();
numbers.pop_front();

auto it = numbers.begin();
for (it, it!= numbers.end(); it++) // i want to insert number 18 infront of
20;
{
if (*it == 20)
{
numbers.insert(it,18); // position,value
}
}

for (it=numbers.begin(); it!=numbers.end();it++){


cout<<*it;
}

return 0;
}

#include <iostream>
#include<map>
using namespace std;

int main()
{
map<string,int> coffee;
coffee["Espresso"]=20;
coffee["Tea"] = 30;
coffee["Cold tea"] = 50;

map<string,int> :: iterator it;


it = coffee.begin();
for (it; it!= coffee.end(); it++){
cout<<it->first<<":"<<it->second<<endl;
}

return 0;
}

#include<iostream>
#include<map>

int main()
{
multimap<int,string> buddy;
buddy.insert(make_pair(10,"vinoth"));
buddy.insert(make_pair(20,"suresh"));
buddy.insert(make_pair(30,"naresh"));

buddy.insert(pair<int,string>(10,"laksh"));

multimap<int,string> ::iterator it = buddy.begin();


for(it; it!= buddy.end(); it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
return 0;
}

You might also like