Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:06:23+00:00 2026-06-10T13:06:23+00:00

#include <iostream> #include <vector> #include <algorithm> using namespace std; void push_back(int v, vector<int>& coll)

  • 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)); 
}

the compiler said:

/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> >&)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:06:25+00:00Added an answer on June 10, 2026 at 1:06 pm

    Your program is perfectly fine, except that you’re using using namespace std; which you should be avoiding. In this case, that is possibly causing the problem.

    So I suggest you to remove using namespace std; line, and try using fully-qualified names such as std::vector and std::for_each etc.

    Another fix could be this : just don’t use std::for_each, as you don’t need it.

    Write this:

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

    Done!

    Now if you want to insert more items later on, then do this:

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

    Hope that helps.


    Your real scenario:

    As you said in the comment:

    On my real work,the source collection element is an object.I just want to pick up a member data from each element and insert them to an vector.

    If that is the case, then you should std::transform.

    Suppose the source is std::vector<person> and you want to pick age member data from each element of this source collection, and insert them into b which is 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  
    

    where select_age is defined as:

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

    If you can use C++11’s lambda, then it is much easier:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
The code is following: #include <iostream> #include <vector> #include <algorithm> using namespace std; struct
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
#include <iostream> #include <vector> #include <iterator> using namespace std; struct Point { int x;
#include <iostream> #include <vector> #include <algorithm> using namespace std; class A { public :
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
#include <iostream> using namespace std; // This first class contains a vector and a
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <functional> #include <deque> using namespace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.