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

C++ std::set Find function overloading == operator

I am using sets. I use a custom struct as the key. I am inserting a value and trying to find the inserted value. But it never seems to find the element. I have overridden both the == operator and the < operator. Here is the code of the…
The Flying Dutchman
  • 582
  • 2
  • 7
  • 18
7
votes
3 answers

C++ set search for pair element?

So I have a set of pairs And I want to use find() to search for a single string which would be in the "first" of the pair, then if I find that string in first I want to return second from that function. My current attempt…
user1288735
  • 71
  • 1
  • 1
  • 2
7
votes
1 answer

How does std:set check if there is an equivalent element in set during the insertion?

Let's use the following code as an example. auto cmp = [](ll a, ll b) { return gcd(12, a) < gcd(12, b); }; set s(cmp); for(ll i = 0; i < 2; i++){ ll x; cin >> x; s.insert(x); cout << "Success! \n"; } I defined…
7
votes
5 answers

C++ equivalent of Python difference_update?

s1 and s2 are sets (Python set or C++ std::set) To add the elements of s2 to s1 (set union), you can do Python: s1.update(s2) C++: s1.insert(s2.begin(), s2.end()); To remove the elements of s2 from s1 (set difference), you can do Python:…
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
7
votes
2 answers

How to count the number of distinct values in a C++ std::map

I have a c++ map declared as follows std::map wordMap= { { "is", 6 }, { "the", 5 }, { "hat", 9 }, { "at", 6 } }; I would like to know how to find the number of distinct values of int existing in wordMap. In…
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
7
votes
2 answers

Storing in std::map/std::set vs sorting a vector after storing all data

Language: C++ One thing I can do is allocate a vector of size n and store all data and then sort it using sort(begin(),end()). Else, I can keep putting the data in a map or set which are ordered itself so I don't have to sort afterwards. But in…
7
votes
3 answers

std::set find behavior with char * type

I have below code line: const char *values[] = { "I", "We", "You", "We"}; std::set setValues; for( int i = 0; i < 3; i++ ) { const char *val = values[i]; std::set::iterator it = setValues.find( val ); if( it…
user987316
  • 894
  • 4
  • 13
  • 35
7
votes
1 answer

For how long the iterator returned by std::set.find() lives?

I need to keep track of std::set element by saving the iterator returned by set.find(). My questions is does insertion and removing other elements invalidates the obtained iterator? From a simple test I did I can see it is not, but I'd like to…
jackhab
  • 17,128
  • 37
  • 99
  • 136
7
votes
3 answers

How to elegantly put all enums into a std::set

I have an enum and I want to put them all in the set( and then remove some with set_intersection algorithm, but that is offtopic). All works great except Im stuck on step 1. :) If I have(real class has enum with higher cardinality) class…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
6
votes
2 answers

How to use a std::set as a data member with an explicit comparator member function?

I am using C++17. std::set is a template type: template< class Key, class Compare = std::less, class Allocator = std::allocator > class set; One can have a std::set as a data member. For example: #include class Foo { …
user1387866
  • 2,834
  • 3
  • 22
  • 28
6
votes
3 answers

std::set won't compile

The const here is the cause of the compilation problem. However, having implemented an AVL tree myself, I can't understand why. Here's the code: #include int main () { int a; // I want the set to carry the "promise" // to…
Elliott
  • 2,603
  • 2
  • 18
  • 35
6
votes
2 answers

Why cant items from std::set be 'popped'?

I know that std::set does not allow non-const access to it's items. I know that it's impossible to move items out of a the set – because any sort of non-const access might break the ordering of the set. However, we CAN erase an item from the set.…
Thor Correia
  • 1,159
  • 1
  • 12
  • 20
6
votes
2 answers

with custom struct contains duplicates

I've been learning c++. I am stuck with this problem. I have set that contains a custom struct that contains two long int's a & b. I have a custom comparer struct that compares the numbers and returns true if either a or b is different. typedef long…
Srikanth R
  • 410
  • 5
  • 17
6
votes
5 answers

How can I get a std::set of characters in a string, as strings?

I have a std::string. I want the set of unique characters in it, with each character represented as a std::string. I can get the set of characters easily: std::string some_string = ... std::set char_set(some_string.begin(),…
EMBLEM
  • 2,207
  • 4
  • 24
  • 32
6
votes
4 answers

Is it possible to use elements of a different type than contained in a std::set to perform search and deletion?

Let's say I have the following: struct MetadataThingy { void *actual_thingy; int some_metadata; int more_metadata; bool operator<(MetadataThingy const& other) const { return actual_thingy < other.actual_thingy; …
1 2
3
19 20