1

In the below code aim traversing through the map separately and fetching keys and values , Is there a way that i could do it per entry not separately i need to join key and its value (which is a structure here )

#include <boost/algorithm/string.hpp>
#include <boost/range/adaptors.hpp>
#include <vector>
#include <string>
#include <functional>
#include <iostream>
struct valueInfo
{
   std::string val1;
   std::string val2;
   std::string val3;
   valueInfo(const std::string A, const std::string B, const     std::string C):val1(A),val2(B),val3(C){}

};

typedef std::map<std::string,valueInfo*> AssetMap ;

void foo(const AssetMap& pList)
{

using namespace boost::adaptors;
for (const auto & key : pList | boost::adaptors::map_keys  ) {
    std::cout << key << " ";

}
for (const auto & key : pList | boost::adaptors::map_values) {
         std::cout << key->val1 << key->val2<< key->val3<< "\n";
}

}
int main()
{
AssetMap myMap;
std::string key = "11233";
valueInfo *myvalues = new valueInfo ("Tejas","Male","Employee");
//std::cout<<myvalues;
//myMap.insert(std::make_pair(key, myvalues));
myMap.insert(std::make_pair(key, myvalues));
myMap.insert(std::make_pair("111", myvalues));

myMap.insert(std::make_pair("222", myvalues));

myMap.insert(std::make_pair("333", myvalues));

myMap.insert(std::make_pair("444", myvalues));

foo(myMap);

}

Current Output

111 11233 222 333 444 TejasMaleEmployee
TejasMaleEmployee
TejasMaleEmployee
TejasMaleEmployee
TejasMaleEmployee

Desired output

111{TejasMaleEmployee}
11233{TejasMaleEmployee}
222{TejasMaleEmployee}

Thanks in advance Tejas

LearningCpp
  • 972
  • 12
  • 29
  • 2
    Remove ` | boost::adaptors::map_keys` and operate on `.first` and `.second`? – Yakk - Adam Nevraumont Mar 23 '17 at 20:09
  • 1
    don't put pointers in maps. If you want to share the data, use a shared_ptr or a std::reference_wrapper. Pointers are ambiguous - it's not obvious who's controlling the lifetime of the mapped object. – Richard Hodges Mar 23 '17 at 21:03
  • @OP: hint, the comment about lifetimes is exactly the description of your memory leak. – sehe Mar 24 '17 at 11:16

1 Answers1

0

This code is c++14. c++11 version on request.

No need for the boost adapters. map iterators dereference to pairs of <const key, value>.

I have also fixed the memory leak.

#include <memory>
#include <vector>
#include <string>
#include <functional>
#include <iostream>
#include <map>

struct valueInfo
{
    std::string val1;
    std::string val2;
    std::string val3;

    valueInfo(const std::string A, const std::string B, const std::string C)
            : val1(A)
            , val2(B)
            , val3(C) {}

};

// reference wrapper in unequivocal - the map does not control the lifetime of the object
typedef std::map <std::string, std::reference_wrapper<valueInfo>> AssetMap;

void foo(const AssetMap& pList)
{
    for (auto&& entry : pList) {
        auto&& key = entry.first;
        auto&& value = entry.second.get();
        std::cout << key << " {" << value.val1 << " " << value.val2 << " " << value.val3 << " }\n" ;
    }
}

int main()
{
    AssetMap    myMap;
    std::string key       = "11233";
    auto   myvalues = std::make_unique<valueInfo>("Tejas", "Male", "Employee");
    myMap.emplace(key, *myvalues);
    myMap.emplace("111", *myvalues);
    myMap.emplace("222", *myvalues);
    myMap.emplace("333", *myvalues);
    myMap.emplace("444", *myvalues);


    foo(myMap);

}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Can you pleas point me to the code where there was a potential memory leak ? – LearningCpp Mar 24 '17 at 05:56
  • @LearningCpp why? You can see the differences. c++11 version http://coliru.stacked-crooked.com/a/18fa1fdac5caf4d0 (literally 1 change) – sehe Mar 24 '17 at 11:15