list in c++ code example
Example 1: how to make a list in c++
#include <list>
std::list<int> ints;
Example 2: list in cpp
#include <bits/stdc++.h>
using namespace std;
void display_list(list<int>li)
{
for(auto i:li)
{
cout<<i<<" ";
}
}
int main()
{
list<int>list_1;
int n,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
list_1.insert(x);
}
if(list_1.empty()==false)
{
display_list(list_1);
}
list_1.sort();
list_1.reverse();
list_1.pop_back();
list_1.pop_front();
display_list(list_1);
return 0;
}
Example 3: how to write C++ list
#include <iostream>
#include <list>
int main ()
{
std::list<int> first;
std::list<int> second (4,100);
std::list<int> third (second.begin(),second.end());
std::list<int> fourth (third);
int myints[] = {16,2,77,29};
std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are: ";
for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
std::cout << *it << ' ';
std::cout << '\n';
return 0;
}
Example 4: linked list in c++ stl
#include <bits/stdc++.h>
#include <iostream>
#include <list>
#include <iterator>
#define ll long long
using namespace std;
void showList(list <int> l){
list <int> :: iterator it;
for(it = l.begin(); it != l.end(); it++){
cout<<*it<<" ";
}
}
int main(){
list <int> l1;
list <int> l2;
for(int i=0; i<10; i++){
l1.push_back(i*2);
l2.push_back(i*3);
}
cout<<"content of list 1 is "<<endl;
showList(l1);
cout<<endl;
cout<<"content of list 2 is "<<endl;
showList(l2);
cout<<endl;
l1.reverse();
showList(l1);
cout<<endl;
l1.sort();
showList(l1);
cout<<endl;
l2.pop_front();
l2.pop_back();
l2.push_back(10);
l2.push_front(20);
return 0;
}
Example 5: list stl
template < class T, class Alloc = allocator<T> > class list;