0
#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

void push_back(int v, vector<int>& coll) 
{
    coll.push_back(v); 
}

int main() 
{
    int a[] = {1, 2, 3, 4, 5};
    std::vector<int> b;
    for_each(a, a + 5, bind2nd(ptr_fun(push_back), b)); 
}

コンパイラは言った:

/usr/include/c++/4.1.2/bits/stl_function.h: In instantiation of ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >’:
tt5.cpp:15:   instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:435: error: forming reference to reference type ‘std::vector<int, std::allocator<int> >&’
/usr/include/c++/4.1.2/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>, _Tp = std::vector<int, std::allocator<int> >]’:
tt5.cpp:15:   instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:455: error: no matching function for call to ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>&, std::vector<int, std::allocator<int> >&)’
/usr/include/c++/4.1.2/bits/stl_function.h:429: note: candidates are: std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >&)
4

2 に答える 2

5

using namespace std;あなたが避けなければならないものを使用していることを除いて、あなたのプログラムは完全に素晴らしいです。この場合、それが問題を引き起こしている可能性があります。

したがって、行を削除して、などの完全修飾名を使用してみることをお勧めしますusing namespace std;std::vectorstd::for_each

別の修正はこれである可能性があります:std::for_eachあなたがそれを必要としないので、ただ使用しないでください。

これを書いてください:

int a[] = {1, 2, 3, 4, 5};
std::vector<int> b(a, a+5);

終わり!

後でさらにアイテムを挿入する場合は、次のようにします。

int *c = get_n_items(n); //get n items 
b.insert(b.end(), c, c+n); //insert all items at end

お役に立てば幸いです。


あなたの実際のシナリオ:

あなたがコメントで言ったように:

私の実際の仕事では、ソースコレクション要素はオブジェクトです。各要素からメンバーデータを取得して、それらをベクターに挿入したいだけです。

その場合は、する必要がありstd::transformます。

ソースがであり、このソースコレクションの各要素からメンバーデータstd::vector<person>選択 ageし、それらを挿入するとしbますvector<int>

std::vector<person> persons = get_persons();
std::transform(persons.begin(),        //input begin iterator
               persons.end(),          //input end iterator
               std::back_inserter(b),  //output iterator
               select_age);            //selector  

ここで、select_ageは次のように定義されます。

int select_age(person const & p) { return p.age; }

C ++ 11のラムダを使用できる場合は、はるかに簡単です。

std::transform(persons.begin(),        //input begin iterator
               persons.end(),          //input end iterator
               std::back_inserter(b),  //output iterator
               [](person const & p) {return p.age;});  //selector
于 2012-08-31T04:56:21.250 に答える
0

アプローチ1

void push_back(int v, vector<int>& coll)
{
    coll.push_back(v);
}

for_each(a, a + 5, bind2nd(ptr_fun(push_back), b));

アプローチ2

#include <boost/bind.hpp>
#include <boost/ref.hpp>

for_each(a, a + 5, boost::bind(&vector<int>::push_back, boost::ref(b), _1));

アプローチ3

copy (a, a+5, back_inserter(b));
于 2012-08-31T05:25:09.767 に答える