Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Create a Set of Sets in C++?
Next article icon

How to Create a Set of Vectors in C++?

Last Updated : 22 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++.

For Example,

Input:
vector<int> vec1={1, 2, 3};
vector<int> vec2={4, 5, 6};
vector<int> vec3={1, 2, 3};Output:
Set Elements are:
{1 2 3}
{4 5 6}

Declaring a Set of Vectors in C++

To create a set of vectors, first declare a std::set with std::vector<dataType> as its template parameter and then add the vectors to the set using set::insert() method.

Syntax to Declare Set of Vectors in C++

set<vector<datatype>> setOfVector;

In the above set, each element will be a vector of type 'datatype'

C++ Program to Create a Set of Vectors

C++
// C++ program to create set for vectors  #include <iostream> #include <set> #include <vector> using namespace std;  int main() {      // Creating set of vectors     // that will store each vector     set<vector<int> > st;      // Initializing vectors     vector<int> vec1{ 1, 2, 3 };     vector<int> vec2{ 4, 5, 6 };     vector<int> vec3{ 1, 2, 3 };      // Inserting the above vectors into the set     st.insert(vec1);     st.insert(vec2);     st.insert(vec3);      // Iterating and printing vectors from the set     cout << "Set Elements are: " << endl;     for (auto& it : st) {         for (int elem : it) {             cout << elem << " ";         }         cout << endl;     }      return 0; } 

Output
Set Elements are:   1 2 3   4 5 6   

Time Complexity: O(N * M log N), here N is the number of vectors in the set and M is the average size of the vectors.
Auxiliary Space: O(N * M)




Next Article
How to Create a Set of Sets in C++?
author
sireeshakanneganti112
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-vector
  • cpp-set
  • CPP Examples
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to Create a Stack of Vectors in C++?
    In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac
    2 min read
  • How to Create a Vector of Pairs in C++?
    In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in
    5 min read
  • How to Create a Set of Sets in C++?
    In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set
    2 min read
  • How to Create a Vector of Tuples in C++?
    In C++, a tuple is an object that allows the users to store elements of various data types together while a vector is used to store elements of the same data types. In this article, we will learn how we can create a vector of tuples in C++. Example: Input: Tuple1 ={10,A,5.3} Tuple2= {20,B,6.5}Output
    2 min read
  • How to Create a Deque of Vectors in C++?
    In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++
    2 min read
  • How to Create a Vector of Arrays in C++?
    In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3
    2 min read
  • How to Create a Vector of Vectors of Pairs in C++?
    In C++ STL, we can nest different containers into each other at any number of levels. On such container is the Vector of Vectors of Pairs i.e. 2D vector of pairs. In this article, we will learn how to create and traverse a vector of vector of pairs in C++ STL. Vector of Vectors of Pairs in C++ The 2
    3 min read
  • How to Create a Multimap of Vectors in C++?
    In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++. For Example, Input:myPa
    2 min read
  • How to convert a Vector to Set in C++
    This article shows how to convert a vector to a set in C++. Example: Input: vector = {1, 2, 3, 1, 1} Output: set = {1, 2, 3} Input: vector = {1, 2, 3, 3, 5}Output: set = {1, 2, 3, 5} Below are the various ways to do the required conversion: Method 1: Naive SolutionGet the vector to be converted.Crea
    4 min read
  • How to Create a Set of Pairs in C++?
    In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences