C++ 如何在c+;中将值从向量转换为映射+;?
我想做这样的事。有没有一种stl算法可以轻松做到这一点C++ 如何在c+;中将值从向量转换为映射+;?,c++,map,vector,stl,C++,Map,Vector,Stl,我想做这样的事。有没有一种stl算法可以轻松做到这一点 for each(auto aValue in aVector) { aMap[aValue] = 1; } 试试这个: for (auto it = vector.begin(); it != vector.end(); it++) { aMap[aLabel] = it; //Change aLabel here if you n
for each(auto aValue in aVector)
{
aMap[aValue] = 1;
}
试试这个:
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
的值,可以在循环中更改它。另外,我回顾了最初的问题,不清楚他想要什么,所以我添加了另一个版本。可能是这样的:
std::vector<T> v; // populate this
std::map<T, int> m;
for (auto const & x : v) { m[x] = 1; }
std::vector v;//填充此
std::map m;
对于(auto-const&x:v){m[x]=1;}
如果有对向量,其中对中的第一项是映射的键,第二项是与该键关联的值,则可以使用插入迭代器将数据复制到映射:
std::vector<std::pair<std::string, int> > values {
{"Jerry", 1},
{ "Jim", 2},
{ "Bill", 3} };
std::map<std::string, int> mapped_values;
std::copy(values.begin(), values.end(),
std::inserter(mapped_values, mapped_values.begin()));
std::向量值{
{“Jerry”,1},
{“吉姆”,2},
{“法案”,3};
std::映射的_值;
std::copy(values.begin()、values.end(),
插入器(映射的_值,映射的_值.begin());
或者,您可以从向量初始化贴图:
std::map<std::string, int> m2((values.begin()), values.end());
映射m2((values.begin()),values.end());
假设向量中的项目按顺序关联,此示例可能会有所帮助:
#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;
}
#包括
#包括
#包括
#包括
std::map将_转换为_映射(const std::vector&vec)
{
std::map-mp;
性病:配对;
对于(unsigned int i=0;i您可以将
还有另一种方式:
#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));
}
#包括
#包括
#包括
int main(){
使用T=double;
std::向量v;
自动f=[](T值){return std::make_pair(值,1);};
映射m(boost::make_transform_迭代器(v.begin(),f),
boost::make_transform_迭代器(v.end(),f));
}
但就可读性和执行速度而言,我不认为它比range for loop更好。对于将数组或向量的值直接转换为映射,我们可以这样做
map<int, int> mp;
for (int i = 0; i < m; i++)
mp[a[i]]++;
map-mp;
for(int i=0;i
其中,a[i]是我们随身携带的数组。由于您没有指定任何类型,因此非常通用的方法如下所示:
模板
映射toMap(迭代器开始、迭代器结束、KeySelectorFunc选择器){
地图;
std::transform(begin,end,std::inserter(map,map.end()),[selector](常量值和值)可变{
返回std::make_pair(选择器(值),值);
});
返回图;
}
用法:
struct TestStruct{
int-id;
std::字符串s;
};
std::vector testStruct={
TestStruct{1,“Hello”},
TestStruct{2,“Hello”},
TestStruct{3,“Hello”}
};
std::map map=toMap(testStruct.begin(),testStruct.end(),
[](const TestStruct&t){
返回t.id;
}
);
用于(常数自动和配对:映射){
std::coutaLabel
与向量有什么关系?我想你需要详细说明一下……aLabel来自哪里?你真的想要一个所有值都设置为1的映射吗?很抱歉,如果没有更多细节,它应该是aValue+1,这差不多是最好的答案。有,但我想我更喜欢这个答案,而不是使用tr这很好,因为您可以使用transform的两个输入迭代器版本,并将两个向量压缩到一个映射中。
#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));
}
map<int, int> mp;
for (int i = 0; i < m; i++)
mp[a[i]]++;