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
1
vote
1 answer
How to to compare two points in 2d space
I have a class for holding my Points in 2D space like this:
class Point{
public:
Point(double a, double b){ x = a; y = b; }
//some additional information
private:
double x, y;
};
I like to have these Points in a std::set but I dont know how…

MoNo
- 307
- 4
- 15
1
vote
0 answers
std set insert and union efficient way
I was working on a problem where I am trying to take union and was trying a very simple benchmarking code to see the efficiency.
The code is very simple (inserting) few million elements into a set.
To keep it simple lets keep set_union away from…

user179156
- 841
- 9
- 31
1
vote
2 answers
Why const is required when dereferencing std::set::iterator?
I have the following code:
std::set< std::vector > testSet;
vector v0 = vector(3);
vector v11 = vector(3);
v0[0] = 0;
v0[1] = 10;
v0[2] = 20;
std::cout << v0[0] << endl;
testSet.insert(v0);
…

user107986
- 1,461
- 1
- 17
- 24
1
vote
1 answer
Order in std::set and difference to std::unordered_set
I expected the order of std::set to be consistent like std::list or std::vector, only that a value will not be appended a second time. MSVC 110 prooved me wrong. I expected the following code the yield the result shown below, which is the case when…

Niklas R
- 16,299
- 28
- 108
- 203
1
vote
1 answer
std::set with personal comparison function have identical values
I want to store a std::set of Point3D object, my comparison function is defined as follows (lexicographic order):
bool operator<(const Point3D &Pt1, const Point3D &Pt2)
{
const double tol = 1e-5;
if(fabs(Pt1.x() - Pt2.x()) > tol)
{
…

nathanael
- 15
- 3
1
vote
2 answers
C++ Set Iterators not reaching end of Set
I recently decided to attempt a C++ solution to a recently ended topcoder.com competition, as a way to learn the language (being new to C++). However, I became stumped when iteration through a std::set object did not successfully reach all elements…

Matthew
- 13
- 3
1
vote
1 answer
Cplusplus std::set of bidimensional array
I'm new to C++ and I need to use Set from the STL but I'm struggling with the concept.
I have an array like this int my_data[3]
I want to create (don't know if this is possible) a set with space for 3 ints and that the key for the set would be the…

Favolas
- 6,963
- 29
- 75
- 127
1
vote
1 answer
C++ writing a generic functions for all std::sets
I need to write a little function that makes a new std::set taking the last n elements from an existing one.
Here is the code:
template
std::set get_first_subset(std::set const& set, size_t size) {
…

Paolo.Bolzoni
- 2,416
- 1
- 18
- 29
1
vote
3 answers
How to detect if the current element in a set is the last element?
I have a foreach loop that goes though a std::set that looks like
for (auto& line : lines){
//use line
bool end = /* Check if line is the last element */
}
With a std::vector I could check &line == &lines.back();
Is there a way I can do…

PomfCaster
- 812
- 1
- 7
- 12
1
vote
2 answers
Accessing a static std::set with accessors, is this wise or should I just access it directly?
This question follows on from the outcome of this one:
How to automatically maintain a list of class instances?
With reference to the previous question, I created my static list of objects static std::set< Object* > objects;
However, to avoid…

Interminable
- 1,338
- 3
- 21
- 52
1
vote
2 answers
How to create set of integers with non-standard order in C++?
In C++03, I'd like to create a std::set where when iterating, one integer comes first, after that, I don't care what order, but I need an ordering to ensure there are no duplicates in the set. For example, if I had a set of years, and when…

WilliamKF
- 41,123
- 68
- 193
- 295
1
vote
3 answers
Is there an O(1) way to turn a std::map into a std::set
I'd like to do some set intersection operations on the keys of a std::map<> instance without duplicating the keys into a std::set<> beforehand.
It's not documented in the API, but is there any way in O(1) time to extract the keys from a std::map<>…

slacy
- 11,397
- 8
- 56
- 61
1
vote
1 answer
How to insert an element in a sorted array so the the array remains sorted?
In c++, std::set which stores its element in sorted order can insert elements in O(log n) time. But all the methods i know take linear time:
Inserting the element at the end of the array and swapping it with the previous element until the previous…

2147483647
- 1,177
- 3
- 13
- 33
1
vote
1 answer
vector uniqueness in C++
This is a followup of my previous question in which I wanted to know the most efficient way of storing non-duplicate arbitrary data in an std::set.
The answers helpfully pointed out that for custom classes, you'd need to implement operator< (if I…

Gigi
- 28,163
- 29
- 106
- 188
1
vote
2 answers
Accessing member via set's NON-const iterator that doesn't affect invariants
Let's say I have code like so:
#include
#include
typedef int OtherTypes;
struct MyType
{
double Field1;
OtherTypes MoreFields;
MyType(double blah) :
Field1(blah)
{
}
bool operator < (const MyType…

Happy Green Kid Naps
- 1,611
- 11
- 18