syntax of list in c++ code example
Example 1: 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 2: 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;
}