Convert vector to array c++: In the previous article, we have discussed about Unordered_Map : Erase Elements While Iterating in a Loop. Let us learn How to Convert Vector to Set in C++ Program.
Vector:
Vectors are similar to dynamic arrays in that they can resize themselves when an element is added or removed, and the container takes care of their storage. Iterators can access and traverse vector elements since they’re stored in contiguous storage. Data is inserted at the top of vectors. Inserting at the end takes longer since the array may need to be extended at times. There is no resizing, and removing the final variable still takes the same amount of time. Inserting and erasing at the start or within the middle is linear in terms of your time .
Sets:
Because the value of an element identifies it, sets are a type of associative container in which each element must be unique. The element’s value cannot be changed after it has been added to the set, but the element’s modified value can be removed and re-added.
Given a vector , the task is to convert the given vector to set.
Examples:
Input:
given_vector ={ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }
Output:
1 2 3 4 5 6 7 8 9
Convert Vector to Set
There are several ways to convert vector to set some of them are:
- Basic approach
- Using Range Based Constructor
- Using copy() method
- Using for_each() & lambda
- Using generate_n()
Method #1: Basic approach
We can also write our own vector-to-set conversion routine. The concept is straightforward: start with an empty set, traverse the vector with a range-based for-loop, and insert each element into the set.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating and initializing vector with some random // integer values vector<int> intvector{ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }; // Creating a set set<int> intset; // Traverse the vector and add element to set for (auto element : intvector) { intset.insert(element); } // Traverse the set and print all the set elements for (auto value : intset) cout << value << " "; return 0; }
Output:
1 2 3 4 5 6 7 8 9
Method #2:Using Range Based Constructor
The set class in C++ has a constructor that accepts a range, such as [start, end]. It makes a set out of all the elements in the given range, that is, from start to end-1. To make a set from all of a vector’s elements, pass the vector elements as a range.
Approach:
- Create a vector and initialize with some elements
- Define a set that uses two pointers, begin and end, to copy all elements of the vector.
- Print the converted set.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating and initializing vector with some random // integer values vector<int> intvector{ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }; // converting given vector to set set<int> intset(intvector.begin(), intvector.end()); // Traverse the set and print all the set elements for (auto value : intset) cout << value << " "; return 0; }
Output:
1 2 3 4 5 6 7 8 9
Method #3:Using copy() method
Make a new empty set. Then, as the first two arguments, pass the vector elements as range [start, end] to the copy() function, and as the third argument, pass the set’s back insert iterator. It will copy all of the vector’s elements into the set.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating and initializing vector with some random // integer values vector<int> intvector{ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }; // Creating a set set<int> intset; // copy the vector to set using copy() method copy(intvector.begin(), intvector.end(), inserter(intset, intset.end())); // Traverse the set and print all the set elements for (auto value : intset) cout << value << " "; return 0; }
Output:
1 2 3 4 5 6 7 8 9
Method #4:Using for_each() & lambda
Make an empty set, then iterate through all of the vector’s elements, inserting them one by one into the set. Use the for each() algorithm with a lambda function as an argument to iterate over all elements of a vector.
This lambda function will be applied to each element in the vector by for each(). We can insert the received element into the set within the lambda function.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating and initializing vector with some random // integer values vector<int> intvector{ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }; // Creating a set set<int> intset; // using for_each and lambda fucntion convert vector to // set for_each(intvector.begin(), intvector.end(), [&](const auto& element) { intset.insert(element); }); // Traverse the set and print all the set elements for (auto value : intset) cout << value << " "; return 0; }
Output:
1 2 3 4 5 6 7 8 9
Method #5:Using generate_n()
generate n() takes three arguments:
first
– the start of the range where elements must be generated
count
– the total number of elements to be generated
Callback
– An object of the generator function named 'count' times to produce the added value for the container whose iterator is provided 'first.'
As a result, we can pass a back insert iterator of set as the first argument and vector size (N) as the count argument. Then, as the third argument, pass a lambda function.
This lambda function will be called N times and will return the ith element of the vector on each call. Where I will begin at 0 and work my way up to N-1.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating and initializing vector with some random // integer values vector<int> intvector{ 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }; // Creating a set set<int> intset; // using generate_n function int i = 0; generate_n(inserter(intset, intset.begin()), intvector.size(), [&]() { return intvector[i++]; }); // Traverse the set and print all the set elements for (auto value : intset) cout << value << " "; return 0; }
Output:
1 2 3 4 5 6 7 8 9
Related Programs: