简体   繁体   English

从 2 个地图中插入值<int, vector<char> &gt; 入 1 map <vector<char> , 向量<char> &gt; </char></vector<char></int,>

[英]Insert values from 2 maps<int, vector<char>> into 1 map<vector<char>, vector<char>>

I have我有

map<int,vector> x {1,a}{2,b}{3,c}地图<int,vector> x {1,a}{2,b}{3,c}

map<int,vector> y {1,x}{2,y}{3,z}地图<int,vector> y {1,x}{2,y}{3,z}

map<vector, vector> newMap地图<矢量,矢量> 新地图

I want to the values of x.second and y.second into我想将 x.second 和 y.second 的值转换为

newMap<x.second, y.second>新地图<x.second, y.second>

I want to get {a,x}{b,y}{c,z}我想得到 {a,x}{b,y}{c,z}

map<int, vector<char>>* phrasex;
map<int, vector<char>>* encodedx;
map<char, char>* mapCharacters = new map<char, char>;
auto it1 = encodedx->begin();
auto it2 = phrasex->begin();

    while (it1 != encodedx->end())
    {
        auto t1 = it1->second.begin();
        while (t1 != it1->second.end()) {
            //i'm doing something wrong here
            mapCharacters->insert({ {it1->second},{it2->second} });
            //
        }
        
        ++it1;
        ++it2;
    }

This is a solution using std::transform :这是使用std::transform的解决方案:

std::transform(
    std::begin(x), std::end(x), std::begin(y),
    std::inserter(newMap, std::end(newMap)),
    [](const auto& lhs, const auto& rhs) {
        return std::make_pair(lhs.second, rhs.second);
    });

Note that this requires C++14 because of auto parameters in the lambda function.请注意,由于 lambda function 中的自动参数,这需要 C++14。 In C++11 you would have to write the type std::pair<int, std::vector<char>> explicitly.在 C++11 中,您必须明确编写std::pair<int, std::vector<char>>类型。

Try something like this:尝试这样的事情:

typedef vector<char> vchar;
typedef map<int, vchar>   map_vchar;
typedef map<vchar, vchar> map_vch;
typedef pair<int, vchar>  pr_vchar;

int main() {
    vchar     vc1a(10, 'a');
    vchar     vc2a(10, 'b');
    map_vchar m1 { {1, vc1a}, {2, vc2a} };

    vchar     vc1b(10, 'a');
    vchar     vc2b(10, 'b');
    map_vchar m2 { {1, vc1b}, {1, vc2b} };

    map_vch combined;

    transform(m1.begin(), m1.end(), m2.begin(), inserter(combined, combined.end()),
        [] (const pr_vchar& pr1, const pr_vchar& pr2) {
            return pair<vchar, vchar>{pr1.second, pr2.second};
        ;}
    );

    return 0;
}

I can't be certain, but it appears that the original choice for map<int, vector<char>> could be better substituted with vector<char> , and maybe even a string .我不能确定,但似乎map<int, vector<char>>的原始选择可以更好地替换为vector<char> ,甚至可能是一个string

    map<vector<char>, vector<char>>* mapCharacters = new map<vector<char>, vector<char>>;
    map<int, vector<char>>* phrasex;
    map<int, vector<char>>* encodedx;

    for (map<int, vector<char>>::iterator i = encodedx->begin(), j = phrasex->begin(); i != encodedx->end(), j != phrasex->end(); ++i, ++j)
    {
        for (vector<char>::iterator x = i->second.begin(), y = j->second.begin(); x != i->second.end(), y != j->second.end(); ++x, ++y) {
            
            mapCharacters->insert({ {*x},{*y} });
            
        }
        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM