- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Convert List to Set
Lists in C++ are containers the same as vectors, but list implementation is based on doubly linked lists compared to the array implementation of vectors. Lists generally do not contain elements in contiguous locations, the elements of a list are distributed throughout the memory. Lists offer the same constant time operations anywhere in it, that is the main feature of using lists. Sets on the other hand are containers that contain unique values of a certain type and all the elements are sorted in ascending order. The two containers are different, but there are various ways to convert a list into a set. We discuss the method in detail below.
Naïve Method
The easiest and the most naïve method is to define two different containers; one of list type and the other of set type, and copy each element of the list to the set.
Syntax
list<int> myList; set<int> mySet; for ( int const &val: myList ) { mySet.insert(val); }
Algorithm
- Take input in a list.
- Iterate through each element in the list and insert them into the set.
- Display the contents of the set.
Example
#include <iostream> #include <set> #include <list> using namespace std; int main(){ //initializing the list list<int> myList = { 10, 30, 65, 98, 76, 44, 32, 73, 81, 29 }; set<int> mySet; cout<< "The list contents are:" << endl; //displaying the list contents for ( int const &val: myList ) { cout << val << ' '; } //copying the elements of the list for ( int const &val: myList ) { mySet.insert(val); } cout << "\nThe set contents are:" << endl; for ( int const &val: mySet ) { cout << val << ' '; } return 0; }
Output
The list contents are: 10 30 65 98 76 44 32 73 81 29 The set contents are: 10 29 30 32 44 65 73 76 81 98
Using Range Constructor
The beginning and end pointers of the list must be supplied as arguments to the constructor when building the set to use the range constructor.
Syntax
list<int> myList; set<int> mySet(begin(myList), end(myList));
Algorithm
Take input in a list.
Pass the beginning and ending pointer of the list to the range constructor of the set while creating the set.
Display the contents of the set.
Example
#include <iostream> #include <set> #include <list> using namespace std; int main(){ //initializing the list list<int> myList = { 30, 70, 56, 89, 67, 44, 23, 37, 18, 92 }; //using the range constructor set<int> mySet(begin(myList), end(myList)); cout<< "The list contents are:" << endl; //displaying the list contents for ( int const &val: myList ) { cout << val << ' '; } cout << "\nThe set contents are:" << endl; for ( int const &val: mySet ) { cout << val << ' '; } return 0; }
Output
The list contents are: 30 70 56 89 67 44 23 37 18 92 The set contents are: 18 23 30 37 44 56 67 70 89 92
Using the copy function
The copy function in C++ allows copying data from one container to another. To use the copy function, the beginning and the end pointers of the list has to be passed as arguments to the function as well as the pointers to the set and the beginning of the set within an inserter function.
Syntax
list<int> myList; set<int> mySet; copy(begin(myList), end(myList), inserter(mySet, begin(mySet)));
Algorithm
Take input in a list.
Define a new set.
Pass the beginning and end pointers of the list and the pointers to the set and the beginning of the set within an inserter function as arguments to the copy function.
Display the contents of the set.
Example
#include <iostream> #include <set> #include <list> using namespace std; int main(){ //initializing the list list<int> myList = { 33, 74, 52, 84, 65, 47, 28, 39, 13, 96 }; set<int> mySet; //using the copy function copy(begin(myList), end(myList), inserter(mySet, begin(mySet))); cout<< "The list contents are:" << endl; //displaying the list contents for ( int const &val: myList ) { cout << val << ' '; } cout << "\nThe set contents are:" << endl; for ( int const &val: mySet ) { cout << val << ' '; } return 0; }
Output
The list contents are: 33 74 52 84 65 47 28 39 13 96 The set contents are: 13 28 33 39 47 52 65 74 84 96
Conclusion
When we are using sets, we cannot add or store duplicate elements into the set, but duplicate elements are allowed to store inside lists or array-like data structures. There are also some situations in which using a set rather than a list is preferred. These conversion techniques that we have seen before are really helpful for that.