In C++, `std::set`s are a kind of associative container that stores unique elements, and in which the elements themselves are the keys.
Questions tagged [stdset]
294 questions
3
votes
3 answers
Efficient way to store C++ object with multiple fields of different types in std::set/std::map
I have a general question. Let's assume I have a C++ class with multiple fields of different types. I want/need to store the objects of this class in std::set or std::map (in order to access them in O(log(N)).
In order to do it I need to overload…

Alex Lop.
- 6,810
- 1
- 26
- 45
3
votes
4 answers
Merging std::sets into std::vector
Consider the following.
I have two std::sets and want to merge them in the std::vector in sorted order.
Which is the most effective way to do that?
I did something like this, but I think that there must be a better way of doing it.
std::set…

Eduard Rostomyan
- 7,050
- 2
- 37
- 76
3
votes
2 answers
Why do std::set and set::map's default constructors require heap allocation?
When I try this:
#include
#include
#include
#include
template
struct MyAlloc
{
typedef std::allocator Base;
typedef typename Base::value_type value_type;
typedef typename Base::pointer…

user541686
- 205,094
- 128
- 528
- 886
3
votes
5 answers
How to efficiently insert a range of consecutive integers into a std::set?
In C++, I have a std::set that I would like to insert a range of consecutive integers. How can I do this efficiently, hopefully in O(n) time where n is the length of the range?
I'm thinking I'd use the inputIterator version of std::insert, but am…

WilliamKF
- 41,123
- 68
- 193
- 295
3
votes
1 answer
std::map insertion without creating a temporary value
I've a map that stores, string and a set of doubles as follows.
typedef std::map > exprType;
typedef std::map >::iterator exprIter;
exprType exPricesData;
std::vector…

nsivakr
- 1,565
- 2
- 25
- 46
3
votes
4 answers
Searching in std::set without iterator C++
I have std::set which contains come int values. Now i use iterator to find out whether set contans value.
But my application use this search very ofter and search using iterator too slow, can i do something like that:
std::set fdsockets;
void…

Roman
- 1,377
- 2
- 11
- 12
3
votes
4 answers
Should I use a map or a set if my key is part of my value?
In C++, I have an class which is ordered by its name which is a std::string. I wish to only have one per each unique name in either a std::map or std::set.
I could use a std::set since the operator< will order my instances by their name, however, I…

WilliamKF
- 41,123
- 68
- 193
- 295
3
votes
3 answers
What does same 'value' mean for a std::set?
In C++, the std::set::insert() only inserts a value if there is not already one with the same 'value'. By the same, does this mean operator== or does it mean one for which operator< is false for either ordering, or does it mean something else?

WilliamKF
- 41,123
- 68
- 193
- 295
2
votes
1 answer
C++: std::map and std::set aren't ordered if using custom class (not pointers)
This must be something incredibly stupid, yet I can't manage to make head or tail from it.
This is the testing code.
#include
#include
#include
#include
#include

DrHell
- 461
- 3
- 17
2
votes
4 answers
How to filter out elements of certain data types in a vector of std::variant?
I have a std::vector of std::variant elements with type int or std::set. I want to loop over this vector and insert an extra item if the iterated element is of type std::set. However, it seems that querying the index at run-time is not…

livemyaerodream
- 890
- 6
- 19
2
votes
2 answers
How to provide compare function of std::set to another function
I am using a set of pointers to a class, and have provided a compare function to order the set by comparing the objects rather than the pointers. I am now trying to write another function and to provide it with the same compare function as used in…

ghborrmann
- 101
- 6
2
votes
1 answer
How to iterate std::set based on offset from set.begin()?
I need to get an iterator based on offset.
i.e. having the iterator for begin :
auto it = set.begin()
I need to get to the iterator having offset ofst:
it + ofst
Is there a way to do that? of I need incremental increase it++ the iterator ofst…

YAKOVM
- 9,805
- 31
- 116
- 217
2
votes
1 answer
Inserting std::unique_ptr into std::set
What is the best way to insert a std::unique_ptr made with std::make_unique() into a std::set? Both insert() and emplace() work, but which one is better?

Gordem
- 115
- 9
2
votes
1 answer
Deduce type of Compare for std::set from constructor arguments
I wanna write simpler syntax when declaring std::set with custom compare:
auto s = std::set({1,3,7,9,2,4},[](int a,int b){return a>b;});
but it does not work out of the box. CLang produces:
/Users/kyb/devel/untitled3/main.cpp:13:14: error:…

kyb
- 7,233
- 5
- 52
- 105
2
votes
0 answers
How does std::set guarantee constant time min/max element access?
std::set min/max element access (through begin/rbegin) is constant time. Does it achieve this by updating min/max during insertion/removal?

qwr
- 9,525
- 5
- 58
- 102