使用 std::transform 转换 std::vector<struct{key; val;}> 进入 std::map<key, val>

Ken*_*Y-N 3 c++ stl stl-algorithm c++11

鉴于这些类型:

struct ComplexLibraryThing { /*lots of fields*/};
typedef std::map<int, ComplexLibraryThing> MyMap;
struct ShadowComplexLibraryThing { /*Shadow of ComplexLibraryThing*/};
struct MyVecType { int a; ShadowComplexLibraryThing b; };
typedef std::vector<MyVecType> MyVector;
Run Code Online (Sandbox Code Playgroud)

我可以为序列化执行此操作(我的序列化库不支持类似地图的类型):

MyVecType map2pair(std::pair<int, ComplexLibraryThing> const &myPair)
{
    MyVecType retVal;
    retVal.a = myPair.first;
    retVal.b = convertForSerialisation(myPair.second);
    return retVal;
}

MyMap myMap = {...};
MyVector myVector;
std::transform(myMap.begin(),
               myMap.end(),
               std::back_inserter(myVector),
               map2pair);
Run Code Online (Sandbox Code Playgroud)

然后,我将向量发送到想要重建 的接收器MyMap。但是,我找不到合适的<algorithm>模板来进行反序列化,如下所示:

MyMap myNewMap;
for (auto const &entry: myVector)
    myNewMap[entry.a] = convertForDeserialisation(entry.b);
Run Code Online (Sandbox Code Playgroud)

我该如何使用 来写这个<algorithm>

(请注意,ComplexLibraryThing地图内的类型不能轻易更改,但我也有一个ShadowComplexLibraryThing可以更改的类型)

dru*_*nly 6

我认为缺少的关键“技巧”是std::inserter。这是一个小演示。

#include <algorithm>
#include <iterator>
#include <map>
#include <vector>

struct foo {int a; int b;};

std::vector<foo> GenerateSource() {
  return {};
} 

std::pair<int, int> ConvertFooToMapPair(const foo& f) {
    return {f.a, f.b};
}

int main(int argc, char* argv[]) {
  std::map<int, int> destination;
  std::vector<foo> source = GenerateSource();
  std::transform(
    source.begin(), source.end(),
    std::inserter(destination, destination.end()),
    ConvertFooToMapPair);
}
Run Code Online (Sandbox Code Playgroud)