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 8405019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:44:40+00:00 2026-06-09T22:44:40+00:00

Could you please explain to me what am I doing wrong in the following

  • 0

Could you please explain to me what am I doing wrong in the following code?
I would expect values >= 80 in the second vector but it’s empty.

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

using namespace std;

class Tester
{
    public:
        int value;
        Tester(int foo)
        {
            value = foo;
        }
};

bool compare(Tester temp)
{
    if (temp.value < 80)
        return true;
    else
        return false;
}

int main()
{
    vector<Tester> vec1;
    vector<Tester> vec2;
    vec1.reserve(100);
    vec2.reserve(100);

    for(int foo=0; foo<100; ++foo)
        vec1.push_back(Tester(foo));

    remove_copy_if(vec1.begin(), vec1.end(), vec2.begin(), compare);

    cout<< "Size: " << vec2.size() << endl;

    cout<< "Elements"<<endl;
    for(int foo=0; foo < vec2.size(); ++foo)
        cout << vec2.at(foo).value << " ";
    cout<<endl;

    return 0;
}
  • 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-09T22:44:42+00:00Added an answer on June 9, 2026 at 10:44 pm

    The function std::remove_copy_if() copies the non-matching elements from one sequence to another sequence. The call

    remove_copy_if(vec1.begin(), vec1.end(), vec2.begin(), compare);
    

    assumes that there is a suitable sequence starting at vec2.begin() which is actually not the case: there is nothing. If there weren’t any memory reserve()d for vec2 you would probably get a crash. What you want is an iterator which expand the sequence as necessary:

    std::remove_copy_if(vec1.begin(), vec1.end(), std::back_inserter(vec2), compare);
    

    With this the call to reserve() isn’t necessary but only a potential performance optimization.

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

Sidebar

Related Questions

Could somebody please explain what GCC is doing for this piece of code? What
Could anyone please explain what and how this code below is doing/working? RoleEnvironment.Chaning +=
could anyone please explain what's wrong with it ? am I doing something wrong
Could somebody please explain what this notation is in javascript? What is function(d) doing?
Could someone please explain when would I want to use delegation instead of inheritance?
Could you please explain why this code is not syntactically correct? private void addEditor(final
Could someone please explain to me on when should one use the include and
Wondering if someone could please explain the difference between these two queries and advise
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer

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.