Skip to content
Related Articles

Related Articles

Difference between std::set vs std::vector in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 18 Nov, 2020

Vectors: Vectors are containers similar to dynamic arrays with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are traversed using iterators.

Examples:

vector <int> v;
v.push_back(1);
v.push_back(2);
v.clear();

Below is the implementation of vectors in C++:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the
// working of vector in cpp
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    vector<int> v;
  
    // Inserting elements in vector
    v.push_back(11);
    v.push_back(6);
    v.push_back(12);
    v.push_back(0);
    v.push_back(0);
  
    // Elements are stored in the
    // order of insertion with the
    // duplicate element
    cout << "Elements in vector are:\n";
    for (auto it : v) {
        cout << it << " ";
    }
  
    return 0;
}

chevron_right


Output:

Elements in vector are:
11 6 12 0 0

Set: Set is also one of the templates of Standard Template Library or STL. It is a container of unique elements whose value can’t be modified once added to the set but can be deleted or inserted. The elements of the sets are always stored in sorted form.



Examples:

set <int> s;
s.insert(1);
s.insert(12);
int key = 1;
s.erase(key);

Below is the implementation of sets in C++:
 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the
// working of set in c++
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    set<int> s;
  
    // Insert elements into the set
    s.insert(11);
    s.insert(6);
    s.insert(12);
    s.insert(0);
  
    // Duplicate elements
    s.insert(0);
  
    cout << "Elements in set:\n";
  
    // The inserted elements get sorted
  
    // Print the elements of the set
    for (auto it : s) {
        cout << it << " ";
    }
  
    return 0;
}

chevron_right


Output:

Elements in set:
0 6 11 12

Tabular Difference between the vector and set:

Vector

Set

Elements of the vector are unsorted. Elements of sets are always sorted.
It can contain duplicate elements. It contains only unique elements.
The vector is unordered. Set is ordered.
The time complexity for insertion of a new element is O(1). The time complexity for the insertion of a new element is O(log N).
Vector is faster for insertion and deletion of elements at the end of the container. Set is faster for insertion and deletion of elements at the middle of the container.


Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :