Difference between std::set and std::list Last Updated : 02 Nov, 2023 Comments Improve Suggest changes Like Article Like Report Set: Set is a type of associative container which stores elements in a sorted manner. All the elements of a set are unique and can not be modified but can be removed or inserted. It is a template of Standard Template Library or STL in C++. Syntax: set <data_type> sBelow is the program to illustrate the same: C++ // C++ program to demonstrate the // working of set in c++ #include <bits/stdc++.h> using namespace std; // Driver code int main() { // Declaring a set set<int> s; // Inserting elements // into the set s.insert(10); s.insert(5); s.insert(15); s.insert(1); // Insert the duplicate elements s.insert(1); cout << "Elements in set:\n"; // Print the element stored in set for (auto it : s) cout << it << " "; return 0; } OutputElements in set: 1 5 10 15List: The list is a type of sequence container in which elements are stored in non-contiguous memory allocation. It is implemented as a doubly-linked list so it provides iteration in both directions. Syntax: list <data_type> l;Below is the program to illustrate the same: C++ // C++ program to demonstrate the // working of list in cpp #include <bits/stdc++.h> using namespace std; // Driver code int main() { // Declaring a list list<int> l; // Inserting elements // in the list l.push_back(10); l.push_back(15); l.push_back(5); l.push_back(1); l.push_back(1); l.push_back(10); cout << "Elements in list:\n"; // Print the elements of list for (auto it : l) cout << it << " "; return 0; } OutputElements in list: 10 15 5 1 1 10Below is the tabular difference between the set and list: As seen in the above codes, after inserting the values {10, 5, 15, 1, 1} in the set, the elements get sorted and duplicate is not stored in the set. Hence, it is unordered. But in the case of the list the elements are exactly stored in the order they were inserted and duplicate is also stored. Hence, it is ordered. S.No.Set List 1Set is sorted and unorderedThe list is unsorted and ordered2Insertion cannot be done at the desired positionInsertion can be done at any position using the insert() function3Takes logarithmic time for searching an element.Takes linear time for searching for an element.4Elements are unique.May contain duplicate elements.5Can contain only one null value.Can contain more than one null value.6Insertion and deletion take logarithmic time.Insertion and deletion take constant time.7Implemented in HashSet, LinkedHashSet, and TreeSet.Implemented in ArrayList and LinkedList. Comment More infoAdvertise with us Next Article Difference between std::set and std::list A aktmishra143 Follow Improve Article Tags : C++ Linked Lists HashSet cpp-set cpp-list +1 More Practice Tags : CPP Similar Reads Difference Between STL and Standard Library in C++ In C++, the term "Standard Library" and "Standard Template Library" are often misinterpreted as the same. Although they sound same with only a single word difference, they refer to the different part of the C++ programming language. In this article, we will learn what's the difference between the C+ 3 min read Difference between std::set::upper_bound and std::upper_bound in C++ Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and 4 min read Difference between std::set::lower_bound and std::lower_bound in C++ Prerequisite: Random-access Iterators in C++, Bidirectional Iterators in C++. std::lower_bound in C++: The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than the given value. This means that the function 4 min read Difference Between Forward List and List in C++ Forward List is a sequence container that allows unidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Singly Linked List, which requires constant time for insertion and deletion. Elements of the forward list are scattered in the memor 3 min read Difference between C and C++ C++ is often viewed as a superset of C. C++ is also known as a "C with class" This was very nearly true when C++ was originally created, but the two languages have evolved over time with C picking up a number of features that either weren't found in the contemporary version of C++ or still haven't m 3 min read Difference between ArrayList, LinkedList and Vector ArrayList:Array List is an implemented class of List interface which is present in package java.util. Array List is created on the basis of the growable or resizable array. And Array List is an index-based data structure. In ArrayList, the element is stored in a contiguous location. It can store dif 11 min read Difference between std::remove and vector::erase for vectors std::remove : It doesn't actually delete elements from the container but only shunts non-deleted elements forwards on top of deleted elements. vector::erase : Removes from the vector either a single element (position) or a range of elements ([first, last)). std::remove vs vector::erase By using eras 4 min read Difference between Struct and Enum in C/C++ with Examples Structure in C++ A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The âstructâ keyword is used to create a structure. Syntax: struct structureName{ member1; member2; member3; . . . member 3 min read set::begin() and set::end() in C++ STL In C++, std::set::begin() and std::set::end() are built-in functions used to retrieve set::iterators to the beginning and the end of the set container. Set uses bidirectional iterators, so the iterators returned by these functions support the dereferencing, increment, decrement, relational, and equa 3 min read Difference Between set, multiset, unordered_set, unordered_multiset in C++ In C++ Standard Template Library, set, multiset, unordered_set, unordered_multiset are used to store elements. Although they are similar but differ from each other in some functionalities. The differences are discussed below: 1. Set: Sets are associative containers that store unique elements followi 5 min read Like