stdset

How to efficiently insert a range of consecutive integers into a std::set?

末鹿安然 提交于 2019-12-04 04:33:26
问题 In C++, I have a std::set that I would like to insert a range of consecutive integers. How can I do this efficiently, hopefully in O(n) time where n is the length of the range? I'm thinking I'd use the inputIterator version of std::insert, but am unclear on how to build the input iterator. std::set<int> mySet; // Insert [34 - 75): mySet.insert(inputIteratorTo34, inputIteratorTo75); How can I create the input iterator and will this be O(n) on the range size? 回答1: The efficient way of inserting

How do I find the largest int in a std::set<int>?

為{幸葍}努か 提交于 2019-12-03 08:04:05
问题 I have a std::set<int> , what's the proper way to find the largest int in this set? 回答1: What comparator are you using? For the default this will work: if(!myset.empty()) *myset.rbegin(); else //the set is empty This will also be constant time instead of linear like the max_element solution. 回答2: Sets are always ordered. Assuming you are using the default comparison (less), just grab the last element in the set. rbegin() might be useful. 回答3: I believe you are looking for std::max_element:

advantages of std::set vs vectors or maps

故事扮演 提交于 2019-12-03 04:32:50
问题 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 maps? I can't seem to find an explicit answer to this question. I noticed that sets use maps, but then why not always use maps or always use sets. Instead 2 quite similar containers are provided. Thanks in advance. 回答1: Both std::set and std::map

Why does std::set not have a “contains” member function?

[亡魂溺海] 提交于 2019-12-03 02:31:11
问题 I'm heavily using std::set<int> 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 cumbersome: if (myset.find(number) != myset.end()) .. or the not as obvious: if (myset.count(element) > 0) .. Is there a reason for this design decision ? 回答1: I think it was probably because they were trying to make std::set and std::multiset as similar as

How do I find the largest int in a std::set<int>?

天大地大妈咪最大 提交于 2019-12-02 21:34:56
I have a std::set<int> , what's the proper way to find the largest int in this set? What comparator are you using? For the default this will work: if(!myset.empty()) *myset.rbegin(); else //the set is empty This will also be constant time instead of linear like the max_element solution. Sets are always ordered. Assuming you are using the default comparison (less), just grab the last element in the set. rbegin() might be useful. I believe you are looking for std::max_element : The max_element() function returns an iterator to the largest element in the range [start,end). Since set sorts the

advantages of std::set vs vectors or maps

自古美人都是妖i 提交于 2019-12-02 17:53:52
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 maps? I can't seem to find an explicit answer to this question. I noticed that sets use maps, but then why not always use maps or always use sets. Instead 2 quite similar containers are provided. Thanks in advance. Both std::set and std::map are associative containers. The difference is that std::set s contain only the key, while in std::map there

Why does std::set not have a “contains” member function?

不羁岁月 提交于 2019-12-02 16:54:25
I'm heavily using std::set<int> 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 cumbersome: if (myset.find(number) != myset.end()) .. or the not as obvious: if (myset.count(element) > 0) .. Is there a reason for this design decision ? I think it was probably because they were trying to make std::set and std::multiset as similar as possible. (And obviously count has a perfectly sensible meaning for std::multiset .) Personally I think this was a

How to efficiently insert a range of consecutive integers into a std::set?

只愿长相守 提交于 2019-12-01 21:38:59
In C++, I have a std::set that I would like to insert a range of consecutive integers. How can I do this efficiently, hopefully in O(n) time where n is the length of the range? I'm thinking I'd use the inputIterator version of std::insert, but am unclear on how to build the input iterator. std::set<int> mySet; // Insert [34 - 75): mySet.insert(inputIteratorTo34, inputIteratorTo75); How can I create the input iterator and will this be O(n) on the range size? The efficient way of inserting already ordered elements into a set is to hint the library as to where the next element will be. For that

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

情到浓时终转凉″ 提交于 2019-12-01 05:32:52
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; } }; where actual_thingy points to some data of importance and I want the container ordered by the value of actual_thingy rather than the value of the element pointed at, but I need to store some other data about it, so I created the wrapper class MetadataThingy with a comparator that only considers the value of the actual_thingy pointer (rather than using a container of void * ) Now,

How to do the vector of sets in C++?

不羁岁月 提交于 2019-12-01 03:38:29
问题 I can do a simple array of sets: set < char > * words = new set < char > [10] How I can do a vector of sets? This results in a compiler error: vector < set< char >> v . Thank you for answers! 回答1: If vector < set< char >> v is exactly what you've got there (I hope you cut and pasted), you've run into one of the annoying little features of C++. Those >> look to you like two closing angle brackets for two templates. They look like a right shift operator to the compiler. Change them to > > with