C ++将整数向量(作为struct的成员)插入到整数向量中

时间:2014-09-24 19:05:19

标签: c++ vector struct

我想将两个整数向量(id1.aid2.a)推入整数newId的向量中,但我得到错误C2440:'初始化'

 cannot convert from 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>>' to 'int'

这是我的代码:

struct Ss {
    std::vector<int> a;
    double b;
    double c;
};

// function
Ss getNewId(Ss id1, Ss id2) {
    Ss newSs = Ss();
    std::vector<int> newId = vector<int>();

    //newId.push_back(id1.a);
    //newId.insert(id1.a.begin(), id1.a.end());
    newId.emplace_back(id1.a.begin(), id1.a.end());
    newId.emplace_back(id2.a.begin(), id2.a.end());

    newSs.a = newId ;

    // some code...//

    return newSs;

现在,我确实使用以下方法修复了错误:

std::copy(id1.a.begin(), id1.a.end(), std::inserter(newId, newId.end()));
std::copy(id2.a.begin(), id2.a.end(), std::inserter(newId, newId.end()));

但我真的不知道为什么我不能使用(insertpush_back等)将矢量推入矢量中,我想学习。谢谢你的帮助。

0 个答案:

没有答案
相关问题