如何在c ++中将值从矢量转换为地图?

问题描述 投票:4回答:6

我想做这样的事情。有一个stl算法很容易做到这一点吗?

 for each(auto aValue in aVector)
                {
                aMap[aValue] = 1;
                }
c++ map vector stl
6个回答
8
投票

如果你有一对对矢量,其中对中的第一项将是地图的关键,第二项将是与该关键字相关联的值,你可以使用插入迭代器将数据复制到地图:

std::vector<std::pair<std::string, int> > values;

values.push_back(std::make_pair("Jerry", 1));
values.push_back(std::make_pair("Jim", 2));
values.push_back(std::make_pair("Bill", 3));

std::map<std::string, int> mapped_values;

std::copy(values.begin(), values.end(), 
          std::inserter(mapped_values, mapped_values.begin()));

或者,您可以从矢量初始化地图:

std::map<std::string, int> m2((values.begin()), values.end());

7
投票

也许是这样的:

std::vector<T> v;   // populate this

std::map<T, int> m;

for (auto const & x : v) { m[x] = 1; }

0
投票

试试这个:

for (auto it = vector.begin(); it != vector.end(); it++) {
  aMap[aLabel] = it;
  //Change aLabel here if you need to
  //Or you could aMap[it] = 1 depending on what you really want.
}

我认为这是你想要做的。

编辑:如果要更新aLabel的值,可以在循环中更改它。此外,我回顾原始问题,不清楚他想要什么,所以我添加了另一个版本。


0
投票

假设向量中的项目按顺序相关,也许这个例子可以帮助:

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

std::map<std::string, std::string> convert_to_map(const std::vector<std::string>& vec)
{
    std::map<std::string, std::string> mp;
    std::pair<std::string, std::string> par;

    for(unsigned int i=0; i<vec.size(); i++)
    {
        if(i == 0 || i%2 == 0)
        {
            par.first = vec.at(i);
            par.second = std::string();
            if(i == (vec.size()-1))
            {
                mp.insert(par);
            }
        }
        else
        {
            par.second = vec.at(i);
            mp.insert(par);
        }
    }

    return mp;
}

int main(int argc, char** argv)
{
    std::vector<std::string> vec;
    vec.push_back("customer_id");
    vec.push_back("1");
    vec.push_back("shop_id");
    vec.push_back("2");
    vec.push_back("state_id");
    vec.push_back("3");
    vec.push_back("city_id");

    // convert vector to map
    std::map<std::string, std::string> mp = convert_to_map(vec);

    // print content:
    for (auto it = mp.cbegin(); it != mp.cend(); ++it)
        std::cout << " [" << (*it).first << ':' << (*it).second << ']';

    std::cout << std::endl;

    return 0;
}

0
投票

你可能std::transform std::vector成为std::map

std::vector<std::string> v{"I", "want", "to", "do", "something", "like", "this"};
std::map<std::string, int> m;
std::transform(v.begin(), v.end(), std::inserter(m, m.end()),
               [](const std::string &s) { return std::make_pair(s, 1); });

这将从矢量元素创建std::pairs,然后将其插入到地图中。


0
投票

另一种方式:

#include <map>
#include <vector>
#include <boost/iterator/transform_iterator.hpp>

int main() {
    using T = double;
    std::vector<T> v;
    auto f = [](T value) { return std::make_pair(value, 1); };
    std::map<T, int> m(boost::make_transform_iterator(v.begin(), f),
                       boost::make_transform_iterator(v.end(), f));
}

但是我认为它在可读性和执行速度方面不会超出range-for循环。

© www.soinside.com 2019 - 2024. All rights reserved.