相关疑难解决方法(0)

将元素从一个列表添加到另一个列表的简单方法

将所有元素从一个std :: list添加到另一个std :: list的"正确"方法是什么?

void
Node::addChilds(const NodeList *list)
{
    for(NodeList::const_iterator i = list->begin();
        i != list->end();
        ++i)
        {
            this->m_childs.push_back(*i);
        }
}
Run Code Online (Sandbox Code Playgroud)

我想到了std :: copy,但afaik for copy我必须调整目标列表的大小,备份结束迭代器(在调整大小之前)等.

我正在寻找单行声明.

c++ stl

9
推荐指数
3
解决办法
2万
查看次数

std :: copy和std :: vector问题

我理解为什么这会导致段错误:

#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;
    int iArr[5] = {1, 2, 3, 4, 5};
    int *p = iArr;

    copy(p, p+5, v.begin());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但为什么这不会导致段错?

#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    vector<int> v;
    int iArr[5] = {1, 2, 3, 4, 5};
    int *p = iArr;

    v.reserve(1);
    copy(p, p+5, v.begin());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stl vector

6
推荐指数
2
解决办法
5126
查看次数

标签 统计

c++ ×2

stl ×2

vector ×1