Как из set сделать vector
This post will discuss how to convert a set to a vector in C++.
C++, copy set to vector
std::copy doesn't add elements to the container into which you are inserting: it can't; it only has an iterator into the container. Because of this, if you pass an output iterator directly to std::copy , you must make sure it points to a range that is at least large enough to hold the input range.
std::back_inserter creates an output iterator that calls push_back on a container for each element, so each element is inserted into the container. Alternatively, you could have created a sufficient number of elements in the std::vector to hold the range being copied:
Or, you could use the std::vector range constructor:
Just use the constructor for the vector that takes iterators:
Assumes you just want the content of s in v, and there's nothing in v prior to copying the data to it.
here's another alternative using vector::assign :
22k 2 2 gold badges 71 71 silver badges 91 91 bronze badges 563 1 1 gold badge 5 5 silver badges 6 6 bronze badgesYou haven't reserved enough space in your vector object to hold the contents of your set.
334k 71 71 gold badges 704 704 silver badges 809 809 bronze badgesI think the most efficient way is to preallocate and then emplace elements:
That way we will only invoke copy constructor for every element as opposed to calling default constructor first and then copy assignment operator for other solutions listed above. More clarifications below.
We need to avoid constructing a vector with the size argument which causes all elements default constructed (for nothing). Like with solution using std::copy(), for instance.
And, finally, vector::assign() method or the constructor taking the iterator range are not good options because they will invoke std::distance() (to know number of elements) on set iterators. This will cause unwanted additional iteration through the all set elements because the set is Binary Search Tree data structure and it does not implement random access iterators.
I am getting the run time error in following code. Please let me know can i copy vector elements in set?
16k 9 9 gold badges 64 64 silver badges 119 119 bronze badges 1,847 1 1 gold badge 16 16 silver badges 25 25 bronze badgesSubscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
1. Using Range Constructor
The most elegant solution is to use the std::vector range constructor, which takes two input iterators pointing to the beginning and the end of an input sequence.
Output:
a b c d e
4 Answers 4
You are not initialising itr :
Or remove itr entirely:
In this instance, you could simply initialise kk thus (but if you want to add to kk follow the line above):
124k 23 23 gold badges 173 173 silver badges 235 235 bronze badgesIf the goal is to create a set from the vector elements (rather than update an already existing set that might have some elements in it), then do that, by using the constructor:
52.4k 7 7 gold badges 78 78 silver badges 119 119 bronze badgesYou need to initialize the iterator.
240k 77 77 gold badges 314 314 silver badges 536 536 bronze badgesRelated
Hot Network Questions
2. Using std::copy function
Another good alternative is to use the std::copy , which inserts elements from a source container into a destination container. The destination container should be large enough to accommodate all elements.
How to Convert Set to Vector in C++?
Given the following set with integers (or other types)
We want to convert it to:
Using a Loop to copy a C++ set to std::vector
Intuitively, we can push to the vector one by one, in a loop, by reading the elements in the set.
Alternatively, we can pre-allocate the size of the vector, and use something like this (slightly a bit faster):
Using vector constructor to convert a C++ set to std::vector
In the constructor of the vector, we can pass the iterator of begin and end for the input set, like below:
Using vector.assign to convert a C++ set to std::vector
Using std::copy to copy a C++ set to std::vector
Using std::copy, we can specify the begin and end iterator of the source data to copy from, and the begin iterator of the target data source, like this:
We have to make sure the target data has enough space/storage to copy the data to, thus, the target vector has to be pre-allocated.
Alternatively, we can use the back_inserter that will insert at the end of the target vector, thus no need to allocate the target vector.
Related Posts
Removing duplicate elements and only keeping the unique items in an array (or vector, list)…
Although, sorting a linked list can be done via Recursive Divide-and-Conquer algorithm i.e. merge sorting,…
Support Vector Machines are perhaps one of the most popular and talked about machine learning…
Let's say, we want to extract the unique elements (numbers) in a given array (vector,…
A downside of K-Nearest Neighbors is that you need to hang on to your entire…
Given a 32-bit integer (signed or unsigned), convert it to its hexadecimal in lowercase. The…
Design and implement an iterator to flatten a 2d vector. It should support the following…
A MP3 is a popular audio compressed (lossy) file format. If a song is roughly…
UCS-2 is a character encoding standard in which characters are represented by a fixed-length 16…
This is a legacy code snippet that uses the magic goto, which I find it…
Читайте также: