如何使用std :: back_inserter插入容器(只有一个指向容器的指针)

时间:2015-02-27 12:13:36

标签: c++ stl iterator containers

我想从foo转变为bar。

struct A
{
    explicit A(int d): m_d(d) {};
private:
    int m_d;
};
A some_function (int i) { return A(2*i); }

std::vector<int> foo; 
std::vector<A> bar; // This is part of a Library not under my control

界面仅提供pbar

std::vector<A>* pbar =&bar; // This is the interface to the part of a Library not under my control

取消引用pbar是否合法并将其用作back_inserter的参数?为什么?

std::transform (foo.begin(), foo.end(), std::back_inserter(*pbar), some_function);

这里有一个完整的例子:http://coliru.stacked-crooked.com/a/2aec8d000cabf78b

1 个答案:

答案 0 :(得分:1)

是的,完全没有问题。我不确定你的困惑是什么来源,所以我不确定如何回答&#34;为什么。&#34;如果您有一个指向对象的非const指针,则可以取消引用它并将其传递给需要非const引用的函数(如std::back_inserter)。

相关问题