stdset

C2676: binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator

佐手、 提交于 2021-02-17 03:00:51
问题 I keep getting this error for the code below. Upon reading this, I believed my error to be the it++ in my for loop, which I tried replacing with next(it, 1) but it didn't solve my problem. My question is, is the iterator the one giving me the issue here? #include <iostream> #include <vector> #include <stack> #include <set> using namespace std; struct Node { char vertex; set<char> adjacent; }; class Graph { public: Graph() {}; ~Graph() {}; void addEdge(char a, char b) { Node newV; set<char>

Error C2676: std::set::const_iterator doesn't have operator+ function?

断了今生、忘了曾经 提交于 2021-02-10 20:09:42
问题 std::set<int> s = { 1,2,3,4,5 }; std::set<int> s2(s.begin(), s.begin() + 2); I want to assgin several values of s into s2 . But got below compile error: Error C2676 binary ' + ': ' std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> ' does not define this operator or a conversion to a type acceptable to the predefined It seems std::set::const_iterator doesn't have operator+ method. 回答1: It seems std::set::const_iterator doesn't have operator+ method. You are right about

Error C2676: std::set::const_iterator doesn't have operator+ function?

巧了我就是萌 提交于 2021-02-10 20:08:38
问题 std::set<int> s = { 1,2,3,4,5 }; std::set<int> s2(s.begin(), s.begin() + 2); I want to assgin several values of s into s2 . But got below compile error: Error C2676 binary ' + ': ' std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> ' does not define this operator or a conversion to a type acceptable to the predefined It seems std::set::const_iterator doesn't have operator+ method. 回答1: It seems std::set::const_iterator doesn't have operator+ method. You are right about

std::set custom comparator for 2D points

眉间皱痕 提交于 2020-03-02 14:52:37
问题 I need a list of non-duplicated 2D points, so I use a std::set with a custom comparison function. The function I use has problems after inserting points because sometimes the std::find does not find the already inserted points. const double tolerance = 0.1; struct MyPoint2D { MyPoint2D(double x, double y) : _x(x), _y(y) {} double _x, _y; }; auto compMyPoint2D = [&](const MyPoint2D& pointA, const MyPoint2D& pointB) -> bool { if (pointA._x < pointB._x - tolerance) return true; if (pointA._x >

std::set custom comparator for 2D points

青春壹個敷衍的年華 提交于 2020-03-02 14:41:43
问题 I need a list of non-duplicated 2D points, so I use a std::set with a custom comparison function. The function I use has problems after inserting points because sometimes the std::find does not find the already inserted points. const double tolerance = 0.1; struct MyPoint2D { MyPoint2D(double x, double y) : _x(x), _y(y) {} double _x, _y; }; auto compMyPoint2D = [&](const MyPoint2D& pointA, const MyPoint2D& pointB) -> bool { if (pointA._x < pointB._x - tolerance) return true; if (pointA._x >

How to use emplace() in a std::map whose value is a std::set (map from something to a set)?

懵懂的女人 提交于 2020-01-02 07:18:51
问题 The Problem I have a std::map<int, std::set<int>> named misi . I'm wondering why misi.emplace(2345, {6, 9}); and misi.emplace({2345, {6, 9}}); don't work as expected, as shown below. The Code #include <set> // std:set #include <map> // std::map #include <utility> // std::piecewise_construct, std::pair #include <tuple> // std::forward_as_tuple #include <iostream> // std::cout, std::endl int main() { // --- std::set initializer list constructor --- std::set<int> si({42, 16}); std::cout << "si

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

只愿长相守 提交于 2019-12-30 10:00:12
问题 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

Efficiently initialise std::set with a sequence of numbers

喜你入骨 提交于 2019-12-30 02:44:47
问题 An obvious (naive?) approach would be: std::set<int> s; for (int i = 0; i < SIZE; ++i) { s.insert(i); } That's reasonable readable, but from what I understand, not optimal since it involves repeatedly searching for the insertion position and does not take advantage of the fact that the input sequence is already sorted. Is there a more elegant/efficient (or a de facto) way of initialising an std::set with a sequence of numbers? Or, more generically, how does one efficiently insert an ordered

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

半腔热情 提交于 2019-12-29 06:34:07
问题 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()); 回答1: 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

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

时光总嘲笑我的痴心妄想 提交于 2019-12-27 11:43:29
问题 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