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
15
votes
8 answers

Set detect insertion failure

Is there a simple way to detect when a set insert does not occur because the item being inserted already exists in the set? For example, I'd like to display a message to the user that shows the insert failure so that they can find and remove the…
01100110
  • 2,294
  • 3
  • 23
  • 32
14
votes
6 answers

How to get the elements in a set in C++?

I am confused as to how to get the elements in the set. I think I have to use the iterator but how do I step through it?
SuperString
  • 21,593
  • 37
  • 91
  • 122
12
votes
1 answer

C++ set -- number of elements with a key less than x

I have a set and I want to see how many elements in it are less than x. (x is also int) What should i do?
user182513
12
votes
1 answer

How do I use comparator with is_transparent type?

with C++14 we are allowed to compare elements of some associative containers (like std::set) with other types than the ones stored in a container. It's supposed to work when comparator has is_transparent denoted as a type (see e.g.…
Michał Góral
  • 1,439
  • 2
  • 16
  • 30
10
votes
2 answers

Elegant way to find keys with given prefix in std::map or elements in std::set

I have map, which keys are std::string. I want to find those elements in the map which starts with "DUPA/" prefix. Finding lower bound is easy but upper bound is a bit problematic. I wrote such piece of code: const char* prefix = "DUPA/"; const…
Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47
10
votes
4 answers

how to remove all even integers from set in c++

I'm new to C++. I'd like to know how experienced coders do this. what I have: set s; s.insert(1); s.insert(2); s.insert(3); s.insert(4); s.insert(5); for(set::iterator itr = s.begin(); itr != s.end(); ++itr){ if (!(*itr % 2)) …
Quincy
  • 1,923
  • 5
  • 27
  • 37
9
votes
4 answers

Why can't I use std::function as a std::set or std::unordered_set value type?

Why can't I have a std::set or std::unordered_set of std::functions? Is there any way to get it to work anyway?
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
9
votes
2 answers

How to have this const-corrected?

I have a const-correctness problem which I don't seem to be able to resolve. Here is the structure of my program: class Node { private: int id; std::set neighbours; public: Node(); Node(int…
Adam Hunyadi
  • 1,890
  • 16
  • 32
9
votes
2 answers

std::set erase complexity anomality?

I am trying to figure out the complexity of erasing multiple elements from std::set. I am using this page as source. It claims that the complexity for erasing a single item using an iterator is amortized O(1), but erasing multiple items using the…
Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
9
votes
4 answers

How do I iterate over collections generically in C++?

Simply put, if I have a set and vector how do I create a generic method that can handle both as params. All I want to do, is iterate over either types of collections. Sounds like it should be trivial but I'm missing something. void…
9
votes
1 answer

Why does std::remove not work with std::set?

The following code: #include #include #include std::set s; int main() { s.insert(1); s.insert(2); std::remove(s.begin(), s.end(), 1); } does not compile with gcc 4.7.2: $ LANG=C g++ test.cpp In file…
ABu
  • 10,423
  • 6
  • 52
  • 103
8
votes
2 answers

how to check whether a set has element(s) in certain range in C++

I need to check if a std::set contains element/elements in a range. For example, if the set is a set {1, 2, 4, 7, 8}, and given an int interval [3, 5] (inclusive with both endpoints), I need to know if it has elements in the set. In this case,…
Qiang Li
  • 10,593
  • 21
  • 77
  • 148
8
votes
2 answers

Removing an element from a std::set while iterating over it in C++17

I've read this SO post, and this one too regarding the erasure of elements from a std::set during iteration. However, it seems that a simpler solution exists in C++17: #include #include int main(int argc,char **argv) { …
OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
8
votes
2 answers

C++ feature, like std::set, which allows duplicates

I have an std::set, which stores std::pairs of two integers. The std::set is also sorted, by allowing me to pass a helping class. However, I have written many lines of code so far and now, at the last tests, "they" told me something, that actually…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
8
votes
5 answers

C++ std::map or std::set - efficiently insert duplicates

I have a bunch of data full of duplicates and I want to eliminate the duplicates. You know, e.g. [1, 1, 3, 5, 5, 5, 7] becomes [1, 3, 5, 7]. It looks like I can use either std::map or std::set to handle this. However I'm not sure whether it's faster…
Gigi
  • 28,163
  • 29
  • 106
  • 188
1
2
3
19 20