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
168
votes
8 answers
C++, copy set to vector
I need to copy std::set to std::vector:
std::set input;
input.insert(5);
input.insert(6);
std::vector output;
std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable
Where is the…

CrocodileDundee
- 1,853
- 3
- 13
- 8
123
votes
6 answers
How to find the intersection of two STL sets?
I have been trying to find the intersection between two std::set in C++, but I keep getting an error.
I created a small sample test for this
#include
#include
#include
#include
using namespace std;
int main()…

ILikeTacos
- 17,464
- 20
- 58
- 88
112
votes
11 answers
Why does std::set not have a "contains" member function?
I'm heavily using std::set and often I simply need to check if such a set contains a number or not.
I'd find it natural to write:
if (myset.contains(number))
...
But because of the lack of a contains member, I need to write the…

Jabberwocky
- 48,281
- 17
- 65
- 115
85
votes
10 answers
c++ STL set difference
Does the C++ STL set data structure have a set difference operator?

Steve
- 1,955
- 9
- 28
- 30
70
votes
6 answers
How do I find the largest int in a std::set?
I have a std::set, what's the proper way to find the largest int in this set?

leeeroy
- 11,216
- 17
- 52
- 54
45
votes
5 answers
Why does std::set seem to force the use of a const_iterator?
Consider the simple program below, which attempts to iterate through the values of a set using NON-const references to the elements in it:
#include
#include
class Int
{
public:
Int(int value) : value_(value) {}
int value()…

atomicpirate
- 657
- 5
- 12
44
votes
5 answers
advantages of std::set vs vectors or maps
This may be a stupid question, I am quite new to C++ and programming in general.
I wish to understand the use of several STL containers and with that in mind, I was wondering what the advantages are of using std::set vs for example using vectors or…

brunodd
- 634
- 1
- 5
- 10
42
votes
6 answers
Is the C++ std::set thread-safe?
I've a question about the thread safety of std::set.
As far as I know I can iterate over a set and add/erase members and that doesn't invalidate the iterators.
But consider following scenario:
thread 'A' iterates over a set of…
Racer
37
votes
2 answers
Is there a difference between using .begin() vs .end() for std::inserter for std::set?
If there is any difference between it1 and it2?
std::set s;
auto it1 = std::inserter(s, s.begin());
auto it2 = std::inserter(s, s.end());

Loom
- 9,768
- 22
- 60
- 112
36
votes
2 answers
Why use std::less as the default functor to compare keys in std::map and std::set?
I am wondering why std::map and std::set use std::less as the default functor to compare keys. Why not use a functor that works similar to strcmp? Something like:
template struct compare
{
// Return less than 0 if lhs < rhs
…

R Sahu
- 204,454
- 14
- 159
- 270
29
votes
9 answers
How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?
I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The…

WilliamKF
- 41,123
- 68
- 193
- 295
22
votes
2 answers
Does std::set store objects contiguously in memory?
Does std::set store objects in contiguous memory like std::vector?
I haven't been able to find this on the web, cppreference doesn't mention details on memory allocation. But I can't see why it couldn't use contiguous memory, hence my question.

mfnx
- 2,894
- 1
- 12
- 28
22
votes
5 answers
Efficiently initialise std::set with a sequence of numbers
An obvious (naive?) approach would be:
std::set s;
for (int i = 0; i < SIZE; ++i) {
s.insert(i);
}
That's reasonable readable, but from what I understand, not optimal since it involves repeatedly searching for the insertion position and…

Shawn Chin
- 84,080
- 19
- 162
- 191
21
votes
1 answer
Is it possible to force STL set to reevaluate predicate?
Consider the following data structures and code.
struct Sentence {
std::string words;
int frequency;
Sentence(std::string words, int frequency) : words(words), frequency(frequency) {}
};
struct SentencePCompare {
bool operator()…

merlin2011
- 71,677
- 44
- 195
- 329
16
votes
2 answers
How STL ordered containers know their end?
I know that the standard doesn't dictate the way that STL containers must be implemented, but rather it mandates a set of requirements for each one of them.
However, it is widely known that STL ordered containers are usually implemented as red–black…

101010
- 41,839
- 11
- 94
- 168