stdset

Why can't I call a non-const member function on an element in an std::set?

梦想的初衷 提交于 2019-12-11 06:58:14
问题 I hve the next code: set<Item> temp_items; set < Item >::iterator it; temp_items = user->second.get_items(); for (it = temp_items.begin(); it != temp_items.end(); it++) { if (counter == (choice - 1)) { it->get_count(); } } The item function i trying to call is: int Item::get_count() { return _count; } I don't have in here any const type that should prevent me from accessing the item object, and still, I get the next message: the object has type qualifiers that are not compatible with the

Modifying elements in std::set

落爺英雄遲暮 提交于 2019-12-11 02:44:09
问题 I have the following code -: int main() { set<string> s; s.insert( "asas" ); s.insert( "abab" ); for ( auto item : s ) { cout << item << "\n"; reverse( item.begin(), item.end() ); } cout << "\n"; for ( auto item : s ) { cout << item << "\n"; } } Output -: abab asas abab asas The elements of the set are not being modified at all by the reverse() function. I suspect that the elements inside a set cannot be modified at all. But, if this is the case, why doesn't the compiler give an error in the

Comparing two sets of std::weak_ptr

南笙酒味 提交于 2019-12-10 17:44:49
问题 I am trying to compare two sets of C++11 weak_ptr using GCC 4.7.2. The code below shows the smallest possible sample reproducing the error: std::set<std::weak_ptr<int>, std::owner_less<std::weak_ptr<int> > > set1; std::set<std::weak_ptr<int>, std::owner_less<std::weak_ptr<int> > > set2; bool result = (set1 == set2); Trying to compile the above results in a long list of errors, of which the following is the first actual error: /usr/include/c++/4.7/bits/stl_algobase.h:791:6: error: no match for

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

南笙酒味 提交于 2019-12-08 17:33:50
问题 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 ensure this feature is by design. 回答1: It never invalidates iterators or pointers/references to the elements. Only if you remove the element itself does the iterator or pointer/reference become invalid. 23.1.2/8 : The insert members shall not affect the

Copy std::map to std::set in c++

为君一笑 提交于 2019-12-07 03:38:32
问题 Is it possible with a STL algorithm to deep copy a std::map values to a std::set? I don't want to explicitly insert in the new set. I don't want to explicitly do this: std::map<int, double*> myMap; //filled with something std::set<double*> mySet; for (std::map<int, double*>::iterator iter = myMap.begin(); iter!=myMap.end(); ++iter) { mySet.insert(iter->second); } but find a more coincise and elegant way to do this, with a deep copy of values. 回答1: What about this? std::transform(myMap.begin()

C++ set search for pair element?

本秂侑毒 提交于 2019-12-07 03:11:55
问题 So I have a set of pairs<string ,string> 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 is.. myList::iterator i; i = theList.find(make_pair(realName, "*")); return i->second; 回答1: Is C++11 acceptable? auto it = find_if(theList.begin(), theList.end(), [&](const pair<string, string>& val) -> bool { return val.first == realName; }); return it-

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

╄→гoц情女王★ 提交于 2019-12-06 11:17:24
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.size(): " << si.size() << std::endl; // 2 std::cout << "*si.begin(): " << *si.begin() << std::endl; //

C++ set search for pair element?

假如想象 提交于 2019-12-05 07:06:27
So I have a set of pairs<string ,string> 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 is.. myList::iterator i; i = theList.find(make_pair(realName, "*")); return i->second; Is C++11 acceptable? auto it = find_if(theList.begin(), theList.end(), [&](const pair<string, string>& val) -> bool { return val.first == realName; }); return it->second; Or in C++03, first define a functor: struct MatchFirst { MatchFirst(const string& realName) : realName

Copy std::map to std::set in c++

此生再无相见时 提交于 2019-12-05 06:19:23
Is it possible with a STL algorithm to deep copy a std::map values to a std::set? I don't want to explicitly insert in the new set. I don't want to explicitly do this: std::map<int, double*> myMap; //filled with something std::set<double*> mySet; for (std::map<int, double*>::iterator iter = myMap.begin(); iter!=myMap.end(); ++iter) { mySet.insert(iter->second); } but find a more coincise and elegant way to do this, with a deep copy of values. What about this? std::transform(myMap.begin(), myMap.end(), std::inserter(mySet, mySet.begin()), [](const std::pair<int, double*>& key_value) { return

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

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:46:44
问题 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 MyClass { enum Color{red, green , blue} }; How would I init a std::set<MyClass::Color> to contain all enums. I can obviously manually insert them one by one, do a for loop with casting since they are consecutive and start from 0 (I think that is required if I