C++ Unordered_Set of Vectors

How to store unordered_set to a vector?

The problem with your code is that your vector does not have elements 0 and 1. Either initialize the vector with required number of elements, or insert the elements into an empty vector as below:

int main(){
vector<unordered_set<int>> v1;
unordered_set<int> s1 = {1,2}, s2 = {3,2};
v1.push_back(s1);
v1.push_back(s2);
for (auto &s : v1[0]) {
cout << s << " ";
}
}

Moving a vector into an unordered_set

This solution actually requires more characters, but it does express the intent more directly:

std::unordered_set<T> ht(std::make_move_iterator(v.begin()),
std::make_move_iterator(v.end()));

insert vector into unordered set after declaration c++

Use an iterator with insert().

us.insert(v.cbegin(), v.cend());

No need for the vector as long as the sequence conforms to the input iterator concept and returns strings. An array, other set, vector, etc. are all fine.

When is it worth it to change from std::vector to std::unordered_set?

There are many factors affecting the point where the std::vector falls behind other approaches. See std::vector faster than std::unordered_set? and Performance of vector sort/unique/erase vs. copy to unordered_set for some of the reasons why this is the case. As a consequence, any calculation of this point would have to be rather involved and complicated. The most expedient way to find this point is performance testing.

Keep in mind that some of the factors are based on the hardware being used. So not only do you need performance testing on your development machine, but also performance testing on "typical" end-user machines. Your development machine can give you a gut check, though, (as can an online tool like Quick Bench) letting you know that you are not even in the ballpark yet. (The intuition of individual programmers is notoriously unreliable. Some people see 100 as a big number and worry about performance; others don't worry until numbers hit the billions. Either of these people would be blown away by the others' view.)

Given the difficulty in determining the point where std::vector falls behind, I think this is a good place to give a reminder that premature optimization is usually a waste of time. Investigating this performance out of curiosity is fine, but until this is identified as a performance bottleneck, don't hold up work on more important aspects of your project for this. Choose the approach that fits your code best.

That being said, I personally would assume the break point is well, well over 10 items. So for the 5-element collection of the question, I would be inclined to use a vector and not look back.

Iteration speed of unordered_set vs vector

std::vector is the fastest STL container for linear iterations like in your scenario. This is simply because memory is contiguously allocated and therefore can benefit of caching mechanisms. A vector iterator just increments a pointer internally.

You might improve performance further by trying to use a smaller type than int when possible.

In your case I think parallelization/SIMD would give largest performance boost for reduction. This might be achieved by auto vectorization (check project compiler settings).

You could also try OMP to do this like this (not tested)

size_t end = VecofElems.size();
#pragma omp parallel for shared(vec_cumulative_sum ) reduction(+: vec_cumulative_sum )
for (size_t i = 0; i < end; ++i) {
vec_cumulative_sum += VecofElems[i];
}

This would run multiple threads in parallel. Therefore it's important to declare vec_cumulative_sum as being shared amongst the threads.

std::for_each provides some simple way to make use of parallelism including vectorization. This allows to simply use any STL container like std::vector (and any other container providing iterators) like this

std::for_each(std::execution::par, VecofElems.begin(), VecofElems.end(), 
, []() {
// have some fun using a lambda, or function
}
}

How to write unordered set of vector of vector, namely unordered_set vector vector int set?

I would declare class or struct named: Point that contain two int variables and push every point to the graph (that now need to be vector<Point>) and then push the graph to your set (you can also declare class or struct named: Graph that contain vector)
and then your code should be :

unordered_set<Graph> set;
Graph graph;
graph.push_back(Point(1,1));
graph.push_back(Point(2,2));
set.insert(graph);

In addition, I don't think you need to use unorderd_set, I think set is enough and in this case, your code will be(without the classes):

std::set < std::vector<std::vector<int>>> set;
std::vector<std::vector<int>> graph;
graph.push_back({ 1, 1 });
graph.push_back({ 1, 1 });
set.insert(graph);

if you still want to use unorderd_ser I would look here (you need to give unordered_set a hash function.): https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2280?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(C2280)%26rd%3Dtrue&view=vs-2019

they say that this is a deleted function, so this use of unorderd_set is iliglle check this link and get your answer for unorderd_set, I still offer you to use a regular set

hope I answered your question:)

Why would anyone use set instead of unordered_set?

When, for someone who wants to iterate over the items of the set, the order matters.



Related Topics



Leave a reply



Submit