如何使用boost来连接地图的键和值,特别是如果值是结构

时间:2023-02-09 21:15:03

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

在此先感谢Tejas

1 个解决方案

#1


0  

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

这段代码是c ++ 14。根据要求提供c ++ 11版本。

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

}

#1


0  

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

这段代码是c ++ 14。根据要求提供c ++ 11版本。

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

}