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
2
votes
1 answer
std::set member variable copied instead of moved when class instance moved
I've been working with a pair of classes. The former stores meta data and the latter acts as a container and supports various sorts of indexing of based on the meta data. Stripped down versions are posted below.
The latter class uses an std::set to…

apmccartney
- 743
- 8
- 16
2
votes
5 answers
How to summarize the elements of std::set
I'm trying to find the sum of elements in a set and I was wondering what are good ways to find it.
I built two classes, one called Customer and one called Item, I want to write a function that will calculate the total payment a customer needs to…

Jokerah
- 47
- 6
2
votes
1 answer
Initializer_list initialization of std::set with trivial operator<. Bug in gcc+ / standard library?
This is how my code looks like
#include
#include
using namespace std;
enum Enum_type
{
Enum_type_1 = 1,
Enum_type_2,
Enum_type_3,
Enum_type_4
};
class my_class
{
public:
my_class(Enum_type type) :
…

Marcin K.
- 683
- 1
- 9
- 20
2
votes
3 answers
iterate ordered versus unordered containers
I want to know which data-structures are more efficient for iterating through their elements between std::set, std::map and std::unordered_set, std::unordered_map.
I searched through SO and I found this question. The answers either propose to copy…

101010
- 41,839
- 11
- 94
- 168
2
votes
0 answers
How to insert() repeatedly into a std::set with a hint?
According to cplusplus.com, the std::set::insert() overload that takes a hint iterator of where to insert an item changes from C++98 to C++11. In C++98 the hint should be:
The function optimizes its insertion time if position points to the element…

WilliamKF
- 41,123
- 68
- 193
- 295
2
votes
2 answers
C++ container for storing sorted unique values with different predicates for sorting and uniqueness
I have a record with 2 fields (say, A and B). 2 instances of the record should be considered equal, if their As are equal. On the other hand, a collection of the record instances should be sorted by the B field.
Is there a container like std::set,…

noober
- 4,819
- 12
- 49
- 85
2
votes
1 answer
How can I insert into a set and a deque
I have a C++ function that needs to insert a range of consecutive integers into a set and for each new element of the set at the end of a dequeue in the same order as the iteration. Below is a solution that is approximately O(log(n) * n) due to the…

WilliamKF
- 41,123
- 68
- 193
- 295
2
votes
1 answer
Composited container from std::set is faster than std::set itself?
I have made some composited container Range which accepts either a min/max range as a std::pair or a set of integers as a std::set.
internally it saves a copy of the input by as void *
This container supports iterators and I was curious how fast…

Gabriel
- 8,990
- 6
- 57
- 101
2
votes
1 answer
std::map and std::set does not return bool with a hint
How can I implement this function which apparently does not exist (why?) efficiently:
std::pair std::set::insert (const_iterator hint, const value_type& val);
I want to insert a value efficiently (with a hint), and at the same time…

Gabriel
- 8,990
- 6
- 57
- 101
2
votes
3 answers
C++ std::set comparator
This is the code:
struct comp
{
bool operator()(Reputation *one, Reputation *two)
{
if (one->Amount < 0 && two->Amount >= 0)
return false;
if (one->Amount >= 0 && two->Amount < 0)
return true;
…

Andreas Bonini
- 44,018
- 30
- 122
- 156
2
votes
1 answer
Qt containers - which should I choose?
Qt offers a set of STL-like containers.
However, there is no container available, which stores ordered values by their order (like std::set) and QSet for some reasons behaves like std::unordered_set. I realize that probably O(1) lookups in the "set"…

Yippie-Ki-Yay
- 22,026
- 26
- 90
- 148
1
vote
1 answer
Bellman-Ford with heap doesn't work with custom compare function
I have implemented a Bellman-Ford algorithm to solve a problem (with a graph), but this solution was too slow, so I have replaced the Bellman-Ford's queue with a heap (std::set) , so the solution for the shortest path will be found faster. (somehow…

XCS
- 27,244
- 26
- 101
- 151
1
vote
2 answers
Passing a set of pointers to member functions of a template class
I have here a minimized version of code below, that won't compile.
#include
#include
#include
template
class Points{
public:
std::vector data;
void func1()
{
// some operation on data which…

Prapanch Nair
- 185
- 1
- 10
1
vote
2 answers
How to remove duplicate values while they are being read?
MRE (because license issues):
std::set pool;
// values: 10 10 1 3 4 3 3 2 5 7 5 4 3 9 8 8 7
// (values is an iterable collection)
for (const auto& value : values) {
pool.insert(value);
}
// expected read: 10 1 3 4 2 5 7 9 8
//…

justanotherguy
- 399
- 5
- 16
1
vote
1 answer
Implement unordered_map, unordered_set, map, set using vectors
This is more of an intellectual exercise, but are there battle-tested C++ libraries implementing hash map/set (std::unordered_map, std::unordered_set), red-back trees (std::map, std::set) using std::vectors?
Take std::unordered_set for instance. My…

user2961927
- 1,290
- 1
- 14
- 22