Questions tagged [stdset]

In C++, `std::set`s are a kind of associative container that stores unique elements, and in which the elements themselves are the keys.

294 questions
6
votes
1 answer

Copy std::map to std::set in c++

Is it possible with a STL algorithm to deep copy a std::map values to a std::set? I don't want to explicitly insert in the new set. I don't want to explicitly do this: std::map myMap; //filled with something std::set
linello
  • 8,451
  • 18
  • 63
  • 109
5
votes
2 answers

How to do the vector of sets in C++?

I can do a simple array of sets: set < char > * words = new set < char > [10] How I can do a vector of sets? This results in a compiler error: vector < set< char >> v . Thank you for answers!
Zhake
  • 71
  • 1
  • 4
5
votes
1 answer

Using lambda expression as Compare for std::set, when it's inside a vector

I want to use a lambda expression as custom Compare for a std::set of integers. There are many answers on this site explaining how to do this, for example https://stackoverflow.com/a/46128321/10774939. And indeed, #include #include…
x432ph
  • 400
  • 1
  • 10
5
votes
3 answers

Insert a sorted range into std::set with hint

Assume I have a std::set (which is by definition sorted), and I have another range of sorted elements (for the sake of simplicity, in a different std::set object). Also, I have a guarantee that all values in the second set are larger than all the…
penelope
  • 8,251
  • 8
  • 45
  • 87
5
votes
3 answers

Can I insert into a set, all the elements of a vector that matches a condition, in a single line of code

I have a vector of elements. I want to populate a set using the elements of this vector that match a certain condition. Can I do this using one line, or in any way that is more concise than the below? // given vector v set s; for (const…
nappyfalcon
  • 909
  • 1
  • 10
  • 17
5
votes
4 answers

limit the size of a std::set

I have a short question about the std::set container. Right now I am feeding my set using the pushback function. Of corse the set becomes larger and larger for every push_back. I am only intrested in the latest 30 elements or so... The older…
Lumpi
  • 2,697
  • 5
  • 38
  • 47
5
votes
1 answer

How do I update a member in a set of std::pair?

I tried doing this: std::set< pair > mySet; // fill the set with something mySet.find( make_pair(someValueX, someValueY) )->first = newX; But I get the following error on compilation: error: assignment of member 'std::pair
Tudor Versoiu
  • 122
  • 2
  • 8
5
votes
1 answer

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

The Problem I have a std::map> named misi. I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include // std:set #include
4
votes
2 answers

Creating the container of smart pointers by cloning elements of another container

I have a class, which supports cloning (via method clone). I have a bunch of its instances in a vector of std::unique_ptr. Now, I want to create an std::set of same smart pointers from the above vector, ideally during its construction. The obvious…
one_two_three
  • 501
  • 2
  • 10
4
votes
1 answer

'Candidate template ignored: couldn't infer template argument' with std::set

I'm writing a function to manipulate a set with it's iterators, and it's giving me the error candidate template ignored: couldn't infer template argument The minimum reproducible code is as follows: #include using namespace…
4
votes
2 answers

How to construct a std::set or Boost flat_set from objects’ data members?

#include #include #include class Myclass { int member_a; int member_b; public: Myclass() {}; Myclass(int a_init, int b_init) : member_a(a_init), member_b(b_init) {}; operator int() const { return…
Tim
  • 403
  • 4
  • 9
4
votes
3 answers

STL way of creating/filling std::set from std::vector

I want to create and fill a set from the contents of a member variable of each entry of a vector. This is what I am doing: struct S { int i; }; int main() { std::vector structPtrs; // code to fill the above vector // create set…
CinCout
  • 9,486
  • 12
  • 49
  • 67
4
votes
2 answers

Why does std::(multi)set provide non const iterator methods

Why do the sets (std::set and std::multiset) in the C++ standard library provide non const_iterator methods (set::begin and multiset::begin)? Access to the keys through iterators is always const. It does not matter if the set itself is const or not.…
Curious
  • 20,870
  • 8
  • 61
  • 146
4
votes
3 answers

set::key_comp vs set::value_comp in C++?

What is the difference between set::key_comp vs set::value_comp in C++? Going to cplusplus.com page there is no significant difference. Furthermore on set::key_comp & related set::value_comp pages last sentence is "(...) key_comp and its sibling…
andrew
  • 3,083
  • 4
  • 24
  • 29
4
votes
1 answer

How to find min/max in std::map like in std::set?

Since both set and map are ordered containers, can the min and the max be found in 0(1) time for std::map like in std::set ? // for std::set // std::set s; auto min = *s.begin(); auto max = *s.rbegin(); How do I obtain the max and min in…
nnrales
  • 1,481
  • 1
  • 17
  • 26