

C++: Convert Vector to Set ( 5 Ways )
source link: https://thispointer.com/c-convert-vector-to-set/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article, we will discuss different ways to convert a vector into a set in C++.
Convert a Vector to a Set in C++ using Range Based Constructor
In C++, set class provides a constructor which accepts a range i.e. [start, end). It creates a set from all the element in the given range i.e. from start to end-1. So, to create a set from all elements of a vector, pass the vector elements as range. For example,
#include<iostream> #include<vector> #include<set> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create a set from vector set<int> set_of_nums(numbers.begin(), numbers.end()); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }
Output:
1, 2, 3, 4, 7, 8, 9, 10,
Vector provides two functions,
- vector::begin() -> Returns the iterator pointing to the start of the vector.
- vector::end() -> Returns the iterator pointing to the an element after the end of vector.
Then we passed iterators returned by begin() and end() to the set constructor, to create a set from the vector.
Create an empty set and add vector elements to it using for_each() & lambda function
Create an empty set and then iterate over all elements of vector and insert them one by one into the set. For iteration over all elements of vector, use the for_each() algorithm and pass a lambda function as argument.
for_each() will apply this lambda function on each of the element in vector. Inside the lambda function we can push the received element into the set. For example,
#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; // Iterate over vector and add elements to set one by one for_each( numbers.begin(), numbers.end(), [&](const auto & elem){ set_of_nums.insert(elem); }); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }
Output:
1, 2, 3, 4, 7, 8, 9, 10,
Create an empty set and ass vector items using copy()
Create an empty set. Then pass the vector elements as range [start, end) to the copy() function as initial two argument and as the third argument pass the back_insert_iterator of the set. It will copy all the elements of vector into the set. For example,
#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; // Iterate over vector and copy elements to set one by one copy( numbers.begin(), numbers.end(), std::inserter( set_of_nums, set_of_nums.end()) ); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }
Output:
1, 2, 3, 4, 7, 8, 9, 10,
Create an empty set and fill with vector elements using generate_n()
generate_n() accepts 3 arguments:
- first – the beginning of the range where elements needs to be generated
- count – number of the elements to be generated
- callback – A generator function object that will be called ‘count’ times to generate the value that will added in the container, whose iterator is provided as ‘first’.
So, we can pass a back_insert_iterator of set as first argument and size of vector (N) as the count argument. Then pass a lambda function as third argument.
This lambda function will be called N times and each time it returns the on each call it returns the ith element of the vector. Where i will start from 0 and go till N-1. For example,
#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; int i = 0; generate_n( inserter(set_of_nums, set_of_nums.begin()), numbers.size(), [&](){ return numbers[i++]; } ); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }
Output:
1, 2, 3, 4, 7, 8, 9, 10,
C++: Create empty set and add vector items using transform()
In all the previous solutions, we copied elements from a vector to the set as it is. What if we want to change the elements while copying?
Suppose we have a vector of integers and we want to convert it to a set, but while adding them into the set, we want to multiple each element by 2. To that we can use the transform() function. For example,
#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; transform ( numbers.begin(), numbers.end(), inserter(set_of_nums, set_of_nums.begin()), [](auto & elem){ return elem; }); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }
Output
1, 2, 3, 4, 7, 8, 9, 10,
transform() function, accepts an input range, an output iterator and a unary function. It the then iterates over all the elements in input range and applies the unary function on it and gives the result to output iterator.
As the input range we gave the vector elements and then as output iterator we passed the set iterator and as the unary function we passed a lambda function which returns the argument by multiplying it by two.
so, transform() function iterated over all the elements in vector and on each element it applied the lambda function and then added them to the set.
Summary:
We learned about different ways to convert a vactor into a set in C++.
Join a list of 2000+ Programmers for latest Tips & Tutorials
Recommend
-
4
In this article, we will discuss different ways to print the contents of a vector in reverse order. Table of Contents C++: Print all items of vector in reverse order using reverse iterator In C++, vec...
-
9
In this article, we will discuss different ways to convert a set into a vector in C++. Convert a Set into a Vector in C++ using Range Based Constructor In C++. vector class provides a constructor which accepts a range i.e. [st...
-
6
In this article we will discuss different ways to convert a vector into an array in C++. Convert a vector into an array by copying items one by one Suppose we have a vector of N integers and we want to create an array from it....
-
6
In this article, we will discuss different ways to convert an array to a vector in C++. Table of Contents Convert an array into a vector in C++ using Range Based Constructor In C++. vector class prov...
-
11
folly 的 sorted_vector_set 和 sorted_vector_map 作者: 张志强 ...
-
5
In R: Join vector elements by row, convert vector lines to strings advertisements Is there a "by row" operation...
-
8
In this article, we will discuss how to convert an array to a set in C++. Table Of Contents Problem Description Here we are given an array and we have to push it’s values in a set.
-
6
Python Program to Convert Set into a ListSkip to content Python Program to Convert Set into a List...
-
5
Convert a Set to an Array in JavaScript Jan 25, 2023 Converting a Set to an
-
6
This tutorial will discuss multiple ways to convert set to a vector during iteration in C++. Table Of Contents Advertisements
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK