list in cpp code example
Example 1: cpp std list example
#include <algorithm>
#include <iostream>
#include <list>
int main()
{
std::list<int> l = { 7, 5, 16, 8 };
l.push_front(25);
l.push_back(13);
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end()) {
l.insert(it, 42);
}
std::cout << "l = { ";
for (int n : l) {
std::cout << n << ", ";
}
std::cout << "};\n";
}
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: list stl
template < class T, class Alloc = allocator<T> > class list;