stdset

find_if in set in C++: unresolved overloaded function type

北战南征 提交于 2019-12-25 18:26:20
问题 I have used find_if before with lists and vectors in C++ and it has worked fine. But now, when I try to use it with sets I get the following error: error: no matching function for call to ‘find_if(std::set<A, Acmp>::iterator, std::set<A, Acmp>::iterator, <unresolved overloaded function type>)’| My class is the following: bool searchForA(A i) { return (i.getVal() > 0); } void B::findA() { set<A, Acmp> cont; set<A, Acmp>::iterator it; A *a1 = new A(5); A *a2 = new A(7); cont.insert(*a1); cont

C++, copy set to vector

一个人想着一个人 提交于 2019-12-20 08:23:47
问题 I need to copy std::set to std::vector : std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output; std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable Where is the problem? 回答1: You need to use a back_inserter : std::copy(input.begin(), input.end(), std::back_inserter(output)); std::copy doesn't add elements to the container into which you are inserting: it can't; it only has an iterator into the container. Because

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

一个人想着一个人 提交于 2019-12-18 16:11:25
问题 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 to (a) simply insert all the values into the container, or (b) check whether they already exist in the container and only insert if they don't - are inserts very efficient? Even if there's a better way... can you suggest a fast way to do this?

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

时光总嘲笑我的痴心妄想 提交于 2019-12-18 05:53:15
问题 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? 回答1: for(set<int>::iterator itr = s.begin(); itr != s.end(); )

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

◇◆丶佛笑我妖孽 提交于 2019-12-18 05:28:24
问题 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; }

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

穿精又带淫゛_ 提交于 2019-12-17 19:42:43
问题 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

how to find the intersection of two std::set in C++?

五迷三道 提交于 2019-12-17 17:34:14
问题 I have been trying to find the intersection between two std::set in C++, but I keep getting an error. I created a small sample test for this #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { set<int> s1; set<int> s2; s1.insert(1); s1.insert(2); s1.insert(3); s1.insert(4); s2.insert(1); s2.insert(6); s2.insert(3); s2.insert(0); set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end()); return 0; } The latter program does not generate

Overloading operator for set

核能气质少年 提交于 2019-12-14 04:10:08
问题 I am using a std::set to implement a specific algorithm. The set had duplicates in it, so I assume I had to overload the operator. The overload looks like this. class Vec3f { ... bool operator () ( const Vector3f& v0, const Vector3f& v1 ) const { float epsilon = 1e-7; return ((v1[0] - v0[0]) > epsilon) && ((v1[1] - v0[1]) > epsilon) && ((v1[2] - v0[2]) > epsilon); } ... "Vec3f.h" int main(){ ... std::set<Vec3f,Vec3f> visited; ... } I overloaded it so I could use the < operator needed in std:

C++ std::set comparator

a 夏天 提交于 2019-12-12 20:24:54
问题 This is the code: struct comp { bool operator()(Reputation *one, Reputation *two) { if (one->Amount < 0 && two->Amount >= 0) return false; if (one->Amount >= 0 && two->Amount < 0) return true; if (one->Amount >= 0) return one->Amount <= two->Amount; else return one->Amount >= two->Amount; } }; And this is the problem: Debug Assertion Failed! File: ..\VC\include\xtree Line: 638 Expression: invalid operator< After that, I can choose "Abort", "Retry" or "Ignore". If I choose ignore many more

std::set doesn't detect duplicate custom objects

☆樱花仙子☆ 提交于 2019-12-12 04:13:29
问题 I have a map for keeping the team name and players of the team std:pair<std::string, std::vector<std::string> > and a set of pointers to players to keep the players sorted in descending order by wins. And there's the catch - one player can participate in more than one team. class Player { public: int wins; std::string name; Player() {} Player(std::string name) : wins(0), name(name) {} bool operator<(const Player* rhs) const { return this->wins > rhs->wins; } bool operator()(const Player* lhs,