Lists are non-contiguous memory control sequence containers. List traversal is slower than vector traversal, but if a point is identified, insertion and deletion are rapid.
vector Declaration
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> li; /// vector1 declatation
vector<int> li2; /// vector2 declatation
push_back()
It push the elements into a List from the back
int main()
{
list<int> li; /// list1 declaration
li.push_back(1);
li.push_back(2);
li.push_back(3);
li.push_back(4);
li.push_back(5);
list<int> li2; /// list2 declaration
li2.push_back(11);
li2.push_back(12);
li2.push_back(13);
li2.push_back(14);
list<int> li3 = {21, 22, 23, 24, 25}; /// list3 declaration
Print list
Type 1:-
vector<int>::iterator it; // iterator declaration
for (it = li.begin(); it != li.end(); it++)
{
cout << *it << " ";
}
Type 2:-
// auto iterator ///
for (int i = 0; i < li.size(); i++)
{
cout << li[i] << " ";
}
front()
cout <<"Front : "<< li.front() << endl;
back()
cout <<"Back :"<< li.back() << endl;
for (auto it : li)
{
cout << it << " ";
}
pop_front()
li.pop_front() ;
for (auto it : li)
{
cout << it << " ";
}
insert()
li.insert(li.begin(),99);/// input number
it = li.begin();
advance(it, 3); /// insert index
li.insert(it, 2,9); /// input number and how may time it will print
for (auto it : li)
{
cout << it << " "; /// print list
}
erase()
li.erase(li3.begin());/// erase by index number
it = li3.begin();
advance(it, 3); /// insert index
li3.insert(it, 2, 9); /// input number and how may time it will print
for (auto it : li)
{
cout << it << " "; /// print list
}
pop_back()
li.pop_back() ;
for (auto it : li)
{
cout << it << " ";
}
size()
cout << "size : " << li.size() << endl;
clear()
li.clear();
for (auto it : li)
{
cout << it << " ";
}
empty()
cout <<"empty : "<< li.empty() << endl;
// if empty ==0 then true else empty==1 false
insert()
li.insert(li.begin()+5,99);/// insert index and input number
erase()
li.erase(li.begin()+5 );/// erase by index number
reverse()
reverse(li.begin(),v.end()); // first index
reverse(li.begin()+4,v.end()); //with index number
for (int i = 0; i < li.size(); i++)
{
cout << li[i] << " ";// print vecter
}
sort()
sort(li.begin(),li.end()); // first index
sort(v.begin()+4,li.end());// input index number
for (int i = 0; i < li.size(); i++)
{
cout << li[i] << " ";// print vecter
}
remove()
li3.remove(22);
for (auto it : li)
{
cout << it << " "; /// print list
}
sort()
///// type 1 ////
li2.sort();
for (auto it : li)
{
cout << it << " "; /// print list
}
///// type 2 ////
sort(li.begin(), li.end());
sort(li.begin() , li.end());
for (auto it : li)
{
cout << it << " "; /// print list
}
unique()
li3.unique(); for (auto it : li) { cout << it << " "; /// print list }
li3.unique();
for (auto it : li)
{
cout << it << " "; /// print list
}
merge()
li.merge(li3); for (auto it : li) { cout << it << " "; /// print list }
li.merge(li3);
for (auto it : li)
{
cout << it << " "; /// print list
}
swap()
swap(li, li2); /// swap between 2 vectors
for (int i = 0; i < li.size(); i++)
{
cout << li[i] << " ";// print vecter
}
0 Comments