我有一个map<T,vector<double> >
,说T=char
的价值观是vector<double>
说length<n
的n=5
。我想将每个vector<double>
从地图转移到一个vector<double>
有长度的大的n*mapsize
,每个向量插入在索引处5*k
。如果可能,所有这些都无需复制。
#include <vector>
#include <map>
using namespace std;
int main()
{
typedef vector<double> Vec;
typedef map<char, vector<double> >::iterator ItMap;
//create a map<char,vector<double>> with 2 keys holding resp 2-3 elem vectors
double v_value[] = {1.1,2.4};
Vec v(v_value, v_value + sizeof(v_value)/sizeof(double));
map<char,vector<double> > myMap;
myMap['A'] = v;
v.push_back(10);
myMap['B'] = v;
//create the vector that should get all the small vectors
Vec receipt;
receipt.reserve(10);
for(ItMap it = myMap.begin(); it != myMap.end(); ++it) {
it->second.resize(5);
receipt.insert(receipt.end(),it->second.begin(),it->second.end());
}
}
编辑:我用我的解决方案进行了编辑,但它确实复制了。