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

  • Home
  • SEARCH
  • 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 6013867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:36:38+00:00 2026-05-23T02:36:38+00:00

I am trying to copy a vector of pair to another: vector<pair<int,int>> vp =

  • 0

I am trying to copy a vector of pair to another:

vector<pair<int,int>> vp = {pair<int,int>(1,1), pair<int,int>(2,2)};
vector<pair<int,int>> vp2;
for_each(vp.begin(), vp.end(), [vp2](pair<int,int> p){
                                               if(/*some condition*/){
                                                vp2.push_back(p);
                                               }
                                              });

I get this compiler error:

error: passing ‘const std::vector<std::pair<int, int> >’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >, value_type = std::pair<int, int>]’ discards qualifiers

Using gcc 4.5.1 on ubuntu.

  • 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-05-23T02:36:39+00:00Added an answer on May 23, 2026 at 2:36 am

    Copying is much easier than that:

    vector<pair<int,int>> vp2(vp.begin(), vp.end());
    

    or even:

    vector<pair<int,int>> vp2 = vp; // or
    vector<pair<int,int>> vp2(vp);
    

    The error in your code is that you capture vp2 by value which effectively makes it const in your anonymous method, and you cannot call push_back on a const vector. The following should work:

    for_each(vp.begin(), vp.end(), [&vp2](pair<int,int> p){vp2.push_back(p);});
    

    But there’s no reason to use this instead of the simpler code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy a QList in a std::vector this is my code: std::copy(_param_31.listJobs->list_USCOREjobs.begin(),
Greetings, I am trying to perform a copy from one vector (vec1) to another
I was trying to do something like this: #include <vector> #include <algorithm> int main()
I am trying to copy the contents of certain folders to another folder using
Why am I getting runtime error while trying to do: std::vector<int> vi; std::generate_n(std::back_inserter(vi),10,rand); std::vector<int>
This does not work. I'm trying to learn how to use std::copy but I
I am trying to copy the vector data from sample to Y as below
I'm trying to save objects in an stl container (in this case a vector)
I am trying to define a vector < Queue >. this vector should contain
I am trying to get some C++ code originally written in Microsoft Visual Studio

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.