How to create a List with Constructor in C++ STL


In this tutorial, we will be discussing a program to understand how to create a List with constructor in C++ STL.

List are data structures to store elements in memory in a non-contiguous fashion. They are insertion and deletion quick as compared to vectors.

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
//printing the list
void print_list(list<int> mylist){
   list<int>::iterator it;
   //printing all the elements
   for (it = mylist.begin(); it != mylist.end(); ++it)
      cout << ' ' << *it;
   cout << '\n';
}
int main(){
   //creating list with help of constructor
   list<int> myList(10, 100);
   print_list(myList);
   return 0;
}

Output

100 100 100 100 100 100 100 100 100 100

Updated on: 25-Feb-2020

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements