list of list cpp code example

Example: list in cpp

//code by Soumyadeep Ghosh
//ig: @soumyadepp

#include <bits/stdc++.h>

using namespace std;

void display_list(list<int>li)
{
  //auto variable to iterate through the list
  for(auto i:li)
  {
    cout<<i<<" ";
  }
}
int main()
{
  //definition
  list<int>list_1;
  int n,x;
  cin>>n;
  //taking input and inserting using insert function
  for(int i=0;i<n;i++)
  {
    cin>>x;
    list_1.insert(x);
  }
  //if list is not empty display it
  if(list_1.empty()==false)
  {
    display_list(list_1);
  }
  list_1.sort(); //sorts the list
  list_1.reverse(); //reverses the list
  list_1.pop_back(); //deletes last element of the list
  list_1.pop_front(); //deletes the first element of the list
  
  display_list(list_1);  //function to display the list
  
  
  return 0;
}
//in addition , you can use nested lists such as list<list<int>> or list<vector<list>> etc

Tags:

Cpp Example