Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list. For implementing a singly linked list, we use forward list.
A list can be created with the help of constructor in C++. The syntax to do it is:
Syntax:
list<type> list_name(size_of_list, value_to_be_inserted);
Below programs show how to create a List with Constructor in C++.
Program 1:
#include <iostream>
#include <list>
using namespace std;
void printList(list< int > mylist)
{
list< int >::iterator it;
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n' ;
}
int main()
{
list< int > myList(10, 100);
printList(myList);
return 0;
}
|
Output:
100 100 100 100 100 100 100 100 100 100
Program 2:
#include <bits/stdc++.h>
using namespace std;
void printList(list<string> mylist)
{
list<string>::iterator it;
for (it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n' ;
}
int main()
{
list<string> myList(5, "Geeks" );
printList(myList);
return 0;
}
|
Output:
Geeks Geeks Geeks Geeks Geeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Jan, 2019
Like Article
Save Article