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

C++Server Side ProgrammingProgramming

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
raja
Published on 25-Feb-2020 07:26:19
Advertisements