stdmap

How to pass std::map as a default constructor parameter in c++ class function

て烟熏妆下的殇ゞ 提交于 2019-12-10 13:40:25
问题 I have a problem when attempting to use std::map in clang-3.3 and clang-3.0 on Ubuntu 12.04: #include <iostream> #include <map> #include <string> class A { public: #if 0 //clang compiles ok typedef std::map<std::string,std::string> MapKeyValue_t; void PrintMap(const MapKeyValue_t &my_map = MapKeyValue_t()) #else // clang compiles fail void PrintMap(const std::map<std::string,std::string> &my_map = std::map<std::string,std::string>()) #endif { std::map<std::string,std::string>::const_iterator

Is using a map where value is std::shared_ptr a good design choice for having multi-indexed lists of classes?

被刻印的时光 ゝ 提交于 2019-12-10 13:37:42
问题 problem is simple: We have a class that has members a,b,c,d... We want to be able to quickly search(key being value of one member) and update class list with new value by providing current value for a or b or c ... I thought about having a bunch of std::map<decltype(MyClass.a/*b,c,d*/),shared_ptr<MyClass>> . 1) Is that a good idea? 2) Is boost multi index superior to this handcrafted solution in every way? PS SQL is out of the question for simplicity/perf reasons. 回答1: Boost MultiIndex may

std::map range erase complexity

纵然是瞬间 提交于 2019-12-10 12:42:14
问题 cppreference.com says that complexity of range erase of std::map is: log(c.size()) + std::distance(first, last) while erase for single element by iterator is amortized constant. So if I erase elements in a loop: for( auto it = first; it != last; it = map.erase( it ) ); that should be linear on std::distance(first, last) , and cplusplus.com agrees with that. What does standard say? Is this just typo on cppreference.com? 回答1: log(c.size()) + std::distance(first, last) When (first,last) is the

Copying from map to a list of pointers

半世苍凉 提交于 2019-12-10 12:07:11
问题 I have this interesting assignment where I have a std::map of CTurist (previous class) and unsigned variable. Here's the code: class CTurist { protected: string tName; int age; public: CTurist() {}; CTurist(string name, int age2) { tName = name; age = age2; } bool operator<(const CTurist& e) const { return age < e.age; } friend ostream& operator<<(ostream& os, const CTurist&& e); friend ifstream& operator>>(ifstream& is, CTurist&& e); }; class CHotel:public CTurist { protected: string hName;

std::string as a key in std::map using a compare operator

我的梦境 提交于 2019-12-10 10:25:47
问题 I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map

boost::serialization: object with private default constructor works in a vector, but not in a map

一世执手 提交于 2019-12-10 10:08:45
问题 Consider the following code: #include <boost/serialization/nvp.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> class Foo{ friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int) { ar & BOOST_SERIALIZATION_NVP(i); } int i; Foo():i(0){} public: Foo(int k):i(k){} }; int main(int argc, char *argv[]) { std::vector< Foo> f; f.push_back(Foo(12)); std::ofstream os("path"); boost::archive::xml_oarchive

std::map sort by data?

六眼飞鱼酱① 提交于 2019-12-10 09:17:22
问题 Is there a way to sort std::map by the data rather than the key? Right now my code duplicates the entire map in to an array just to do this. 回答1: As far as I can remember, std::map will give you the iterator that will go through the items sorted by the key. Only way to go through the sorted items by the value, and still use the map, is to rewrite whole collection to another map, with key and value reversed. 来源: https://stackoverflow.com/questions/3992874/stdmap-sort-by-data

How can I increase the performance in a map lookup with key type std::string?

给你一囗甜甜゛ 提交于 2019-12-09 04:55:09
问题 I'm using a std::map (VC++ implementation) and it's a little slow for lookups via the map's find method. The key type is std::string . Can I increase the performance of this std::map lookup via a custom key compare override for the map? For example, maybe std::string < compare doesn't take into consideration a simple string::size() compare before comparing its data? Any other ideas to speed up the compare? In my situation the map will always contain < 15 elements, but it is being queried non

What is the right c++ variant syntax for calling a member function set to a particular variant?

本秂侑毒 提交于 2019-12-08 08:49:23
问题 The code below uses a boost variant of an std::map which contains int/MyVariant pairs. I am able to initialize my map correctly where the first element contains the 33/A pair and the second contains 44/B pair. A and B each have a function that I would like to be able to call after retrieving respectively their initialized map element: #include "stdafx.h" #include "boost/variant/variant.hpp" #include "boost/variant/get.hpp" #include "boost/variant/apply_visitor.hpp" #include <map> struct A {

How to iterate through a std::map<string,int> and std::vector<int> using single for loop?

微笑、不失礼 提交于 2019-12-08 05:17:27
问题 How to iterate through a std::map<string,int> and std::vector<int> using single for loop ? I have seen this questions but could not solve my problem. I am trying like this map<string,int> data; data["Shravan"] = 1; data["Mama"] = 2; data["Sa1"] = 3; data["Jhandu"] = 4; vector<int> values = {1,2,3,4}; for(const auto& it1: data,it2 : values) { // Do something } Edit : I can not go through one by one. Because i am using the key of std::map and value of std::vector in the same function. Which