stdset

c++ STL set difference

孤街浪徒 提交于 2019-11-29 20:23:21
Does the C++ STL set data structure have a set difference operator? Yes there is, it is in <algorithm> and is called: std::set_difference . The usage is: #include <algorithm> #include <set> #include <iterator> // ... std::set<int> s1, s2; // Fill in s1 and s2 with values std::set<int> result; std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(result, result.end())); In the end, the set result will contain the s1-s2 . Yes, there is a set_difference function in the algorithms header. Edits: FYI, the set data structure is able to efficiently use that algorithm, as

how to remove all even integers from set<int> in c++

我的梦境 提交于 2019-11-29 09:50:19
I'm new to C++. I'd like to know how experienced coders do this. what I have: set<int> s; s.insert(1); s.insert(2); s.insert(3); s.insert(4); s.insert(5); for(set<int>::iterator itr = s.begin(); itr != s.end(); ++itr){ if (!(*itr % 2)) s.erase(itr); } and of course, it doesn't work. because itr is incremented after it is erased. does it mean Itr has to point to the begin of the set everytime after i erase the element from the set? for(set<int>::iterator itr = s.begin(); itr != s.end(); ){ if (!(*itr % 2)) s.erase(itr++); else ++itr; } effective STL by Scott Myers Erasing an element from std:

Why does std::set seem to force the use of a const_iterator?

旧巷老猫 提交于 2019-11-29 09:03:40
Consider the simple program below, which attempts to iterate through the values of a set using NON-const references to the elements in it: #include <set> #include <iostream> class Int { public: Int(int value) : value_(value) {} int value() const { return value_; } bool operator<(const Int& other) const { return value_ < other.value(); } private: int value_; }; int main(int argc, char** argv) { std::set<Int> ints; ints.insert(10); for (Int& i : ints) { std::cout << i.value() << std::endl; } return 0; } When compiling this, I get an error from gcc: test.c: In function ‘int main(int, char**)’:

Is there a difference between using .begin() vs .end() for std::inserter for std::set?

主宰稳场 提交于 2019-11-29 05:25:26
If there is any difference between it1 and it2? std::set<sometype> s; auto it1 = std::inserter(s, s.begin()); auto it2 = std::inserter(s, s.end()); James Kanze In practice, not much. If you're inserting a large number of already in order elements into an empty set , the second will be somewhat faster, but that's about it. std::insert_iterator calls insert with the iterator; std::set interprets it as a hint, and inserts in constant time (rather than lg n) if the insertion is immediately before the hint. (Actually, if the set is empty, I think both will do exactly the same thing.) From http:/

Why use std::less as the default functor to compare keys in std::map and std::set?

≡放荡痞女 提交于 2019-11-29 05:25:12
I am wondering why std::map and std::set use std::less as the default functor to compare keys. Why not use a functor that works similar to strcmp? Something like: template <typename T> struct compare { // Return less than 0 if lhs < rhs // Return 0 if lhs == rhs // Return greater than 0 if lhs > rhs int operator()(T const& lhs, T const& rhs) { return (lhs-rhs); } } Say a map has two object in it, with keys key1 and key2 . Now we want to insert another object with key key3 . When using std::less , the insert function needs to first call std::less::operator() with key1 and key3 . Assume std:

c++ STL set difference

人盡茶涼 提交于 2019-11-28 16:25:00
问题 Does the C++ STL set data structure have a set difference operator? 回答1: Yes there is, it is in <algorithm> and is called: std::set_difference. The usage is: #include <algorithm> #include <set> #include <iterator> // ... std::set<int> s1, s2; // Fill in s1 and s2 with values std::set<int> result; std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(result, result.end())); In the end, the set result will contain the s1-s2 . 回答2: Yes, there is a set_difference function

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

佐手、 提交于 2019-11-28 11:20:32
The following code: #include <iostream> #include <set> #include <algorithm> std::set<int> 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 included from /usr/include/c++/4.7/algorithm:63:0, from test.cpp:3: /usr/include/c++/4.7/bits/stl_algo.h: In instantiation of '_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = std::_Rb_tree_const_iterator<int>; _Tp = int]': test.cpp:12:38: required from here /usr/include/c++/4.7/bits/stl_algo.h:1135:13: error: assignment of read-only location '__result

Why use std::less as the default functor to compare keys in std::map and std::set?

人盡茶涼 提交于 2019-11-27 22:56:08
问题 I am wondering why std::map and std::set use std::less as the default functor to compare keys. Why not use a functor that works similar to strcmp? Something like: template <typename T> struct compare { // Return less than 0 if lhs < rhs // Return 0 if lhs == rhs // Return greater than 0 if lhs > rhs int operator()(T const& lhs, T const& rhs) { return (lhs-rhs); } } Say a map has two object in it, with keys key1 and key2 . Now we want to insert another object with key key3 . When using std:

Is the C++ std::set thread-safe?

泪湿孤枕 提交于 2019-11-26 22:13:32
问题 I've a question about the thread safety of std::set. As far as I know I can iterate over a set and add/erase members and that doesn't invalidate the iterators. But consider following scenario: thread 'A' iterates over a set of shared_ptr<Type> thread 'B' occasionally adds items to this set. I've experienced segfaults as the program runs and I'm not sure why this happens. Is lack of thread safety the cause? 回答1: STL has no built in thread support, so you'll have to extend the STL code with

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

谁都会走 提交于 2019-11-26 16:26:38
I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The insertion is out of my control, so I cannot check for duplicates before inserting. int unsortedRemoveDuplicates(std::vector<int> &numbers) { std::set<int> uniqueNumbers; std::vector<int>::iterator allItr = numbers.begin(); std::vector<int>::iterator unique = allItr; std::vector<int>::iterator endItr = numbers.end(); for (; allItr != endItr; ++allItr) {