stdmap

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

早过忘川 提交于 2019-12-07 03:49:28
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 will be called inside for loop. Both the container of same size. If you know there's both the same length,

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()

Remove element from std::map based on the time of insertion

旧时模样 提交于 2019-12-06 20:26:16
问题 I need to erase elements from an std::map based on the time of insertion (or something else more efficient than that). The map will probably hold thousands of elements and if I store the time and iterate the map to check each elements time, it will probably end up being quite time consuming. Does anyone have any good idea how to erase elements from a std::map when they are getting old? 回答1: The std::map<> type has no notion of when an element was inserted. It only serves to hold a key / value

How to pass std::map as a default constructor parameter

我是研究僧i 提交于 2019-12-06 18:41:01
问题 I haven't been able to figure this out. It's easy to create two ctors but I wanted to learn if there's an easy way to do this. How can one pass a std::map as the default parameter to a ctor, e.g. Foo::Foo( int arg1, int arg2, const std::map<std::string, std::string> = VAL) I've tried 0 , null , and NULL as VAL , none of the work because they are all of type int, g++ complains. What is the correct default to use here? Or is this kind of thing not a good idea? 回答1: The correct expression for

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; //

Valgrind memory leak with std::string in std::map

早过忘川 提交于 2019-12-06 03:55:54
问题 Here is the output from Valgrind: ==6519== at 0x4C25885: operator new(unsigned long) (vg_replace_malloc.c:319) ==6519== by 0x4EE65D8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:104) ==6519== by 0x4EE7CE0: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:138) ==6519== by 0x4EE80F7: std::basic_string<char, std::char_traits<char>, std:

what is the best way to use the C type uuid_t as a key in a std::map?

对着背影说爱祢 提交于 2019-12-06 02:46:32
问题 Is this an appropriate way to provide unique keys in a map? In other words, is the key being made from the unique value contained in the uuid, or is it being made from the pointer to the uuid_t struct? A side question, is there a more efficient container, when I don't care about the ordering by keys inside the container? #include <uuid/uuid.h> int main(int argc, char **argv) { std::map<uuid_t,int> myMap; uuid_t id1; uuid_t id2; uuid_generate( (unsigned char *)&id1 ); uuid_generate( (unsigned

Insert std::map into std::vector directly

送分小仙女□ 提交于 2019-12-06 02:18:13
Sorry if the question is very trivial. I have a vector of maps: typedef map<char, int> edges; typedef vector<edges> nodes; nodes n; Now let's say I want to push a toy edge. I tried different things and what I worked is edges e; //declare an edge e['c'] = 1; //initialize it n.push_back(e); //push it to the vector How can I just push the pair of values of an edge ('c' and 2) without having to declare a variable and initialize it? Something like: n.push_back(edges('c',2)); but compiler gives an error error: no matching function for call to ‘std::map<char, int>::map(char, int)’ You can list

How to get the first n elements of a std::map

不羁岁月 提交于 2019-12-06 01:35:34
问题 Since there is no .resize() member function in C++ std::map I was wondering, how one can get a std::map with at most n elements. The obvious solution is to create a loop from 0 to n and use the nth iterator as the first parameter for std::erase(). I was wondering if there is any solution that does not need the loop (at least not in my user code) and is more "the STL way to go". 回答1: You can use std::advance( iter, numberofsteps ) for that. 回答2: Universal solution for almost any container,

how are map iterators invalidated when erasing elements? [duplicate]

扶醉桌前 提交于 2019-12-06 01:11:26
问题 This question already has answers here : Iterator invalidation rules (5 answers) Closed 10 months ago . when and how are iterators invalidated in a map when using the erase method ? for example : std :: map < int , int > aMap ; aMap [ 33 ] = 1 ; aMap [ 42 ] = 10000 ; aMap [ 69 ] = 100 ; aMap [ 666 ] = -1 ; std :: map < int , int > :: iterator itEnd = aMap.lower_bound ( 50 ) ; for ( std :: map < int , int > :: iterator it = aMap.begin ( ) ; it != itEnd ; // no-op ) { aMap.erase ( it ++ ) ; }