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
4
votes
2 answers

std::set custom comparator for 2D points

I need a list of non-duplicated 2D points, so I use a std::set with a custom comparison function. The function I use has problems after inserting points because sometimes the std::find does not find the already inserted points. const double…
JordiS
  • 43
  • 5
4
votes
3 answers

Modifying elements in std::set

I have the following code -: int main() { set s; s.insert( "asas" ); s.insert( "abab" ); for ( auto item : s ) { cout << item << "\n"; reverse( item.begin(), item.end() ); } cout << "\n"; …
Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
4
votes
1 answer

write-write data race from a std::set insert() and find()?

To experiment with the thread-sanitizer, I created a tiny C++ program which by purpose contains a data race. Indeed, tsan does detect the error, great! However I am puzzled by the generated message... It reports a write-write data race, where I…
Jos v E
  • 165
  • 1
  • 5
4
votes
1 answer

C++ std::set unable to use operator + on iterator because compilation error

So I encountered a weird iterator bug ONLY on std::set : I'm unable to do a simple thing like (it + 1) on an iterator without a compilation error Try to compile this yourself : void setBug() { std::set values; for (auto it =…
Power78
  • 311
  • 4
  • 12
4
votes
1 answer

Comparing two sets of std::weak_ptr

I am trying to compare two sets of C++11 weak_ptr using GCC 4.7.2. The code below shows the smallest possible sample reproducing the error: std::set, std::owner_less > > set1; std::set,…
Hans
  • 2,448
  • 2
  • 24
  • 30
3
votes
1 answer

MSVC: C++14: std:set: comparison function: why "const" is required?

Sample code: #include #include using namespace std; class x { private: int i; public: int get_i() const { return i; } }; struct x_cmp { bool operator()(x const & m1, x const & m2) #if _MSC_VER const #endif { …
pmor
  • 5,392
  • 4
  • 17
  • 36
3
votes
1 answer

Why is inserting sorted keys into std::set so much faster than inserting shuffled keys?

I was accidentally surprised to found that inserting sorted keys into std::set is much much faster than inserting shuffled keys. This is somewhat counterintuitive since a red-black tree (I verified that std::set is implemented as a red-black tree on…
Leon Cruz
  • 345
  • 3
  • 11
3
votes
2 answers

How to move a non-copyable element from std::set to std::map using extract?

Consider the following: #include #include struct MyClass { MyClass(int i); MyClass(MyClass const&) = delete; ~MyClass(); bool operator<(const MyClass& r) const { return v < r.v; } int v; }; void…
Sprite
  • 3,222
  • 1
  • 12
  • 29
3
votes
1 answer

Merge two constant `std::set`s in declaration (not in run-time)

I am trying to elegantly declare a constant std::set object that would be a merger of two other constant std::set objects. #include const std::set set_one = { 1,2,3 }; const std::set set_two = { 11,15 }; const std::set set_all…
3
votes
1 answer

C2676: binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator

I keep getting this error for the code below. Upon reading this, I believed my error to be the it++ in my for loop, which I tried replacing with next(it, 1) but it didn't solve my problem. My question is, is the iterator the one giving me the issue…
mlai
  • 63
  • 1
  • 1
  • 4
3
votes
4 answers

Does set::insert saves a copy or a pointer C++

does the function set::insert saves a pointer to the element or a copy of it. meaning, can I do the following code, or I have to make sure that the pointers are not deleted? int *a; *a=new int(1); set _set; _set.insert (*a); delete a; *a=new…
SIMEL
  • 8,745
  • 28
  • 84
  • 130
3
votes
3 answers

std::set operations with custom comparator

I have a question. When I'm using a std::set with custom comparator, other operations like erase or count doesn't work properly. For example: int sz(int const & n) { return __builtin_popcount(n); } struct comp { bool operator()(int const & a,…
3
votes
1 answer

Work with sets in C++

How can I define sets in C++ and insert values into them?
Bahareh
  • 59
  • 7
3
votes
1 answer

Custom Functor in std::set

#include #include #include using namespace std; int order[26]; struct lexcmp { bool operator()(const string &s1,const string &s2) { int i=0; int j=min(s1.size(),s2.size()); while(1) …
Crosk Cool
  • 664
  • 2
  • 12
  • 27
3
votes
2 answers

how to convert std::vector to std::set without losing the order

I am using the below code for conversion: std::set s(v.begin(), v.end()); However, I need to keep the order of the vector elements. How can I do this?
eaytan
  • 319
  • 2
  • 3
  • 10