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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:16:12+00:00 2026-05-15T10:16:12+00:00

Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items

  • 0

Suppose I have the following two data structures:

std::vector<int> all_items;
std::set<int> bad_items;

The all_items vector contains all known items and the bad_items vector contains a list of bad items. These two data structures are populated entirely independent of one another.

What’s the proper way to write a method that will return a std::vector<int> contain all elements of all_items not in bad_items?

Currently, I have a clunky solution that I think can be done more concisely. My understanding of STL function adapters is lacking. Hence the question. My current solution is:

struct is_item_bad {
    std::set<int> const* bad_items;
    bool operator() (int const i) const {
        return bad_items.count(i) > 0;
    }
};

std::vector<int> items() const {
    is_item_bad iib = { &bad_items; };
    std::vector<int> good_items(all_items.size());
    std::remove_copy_if(all_items.begin(),  all_items.end(), 
                        good_items.begin(), is_item_bad);
    return good_items; 
}

Assume all_items, bad_items, is_item_bad and items() are all a part of some containing class. Is there a way to write them items() getter such that:

  • It doesn’t need temporary variables in the method?
  • It doesn’t need the custom functor, struct is_item_bad?

I had hoped to just use the count method on std::set as a functor, but I haven’t been able to divine the right way to express that w/ the remove_copy_if algorithm.

EDIT: Fixed the logic error in items(). The actual code didn’t have the problem, it was a transcription error.

EDIT: I have accepted a solution that doesn’t use std::set_difference since it is more general and will work even if the std::vector isn’t sorted. I chose to use the C++0x lambda expression syntax in my code. My final items() method looks like this:

std::vector<int> items() const {
    std::vector<int> good_items;
    good_items.reserve(all_items.size());
    std::remove_copy_if(all_items.begin(), all_items.end(),
                        std::back_inserter(good_items),
                        [&bad_items] (int const i) {
                            return bad_items.count(i) == 1;
                        });
}

On a vector of about 8 million items the above method runs in 3.1s. I bench marked the std::set_difference approach and it ran in approximately 2.1s. Thanks to everyone who supplied great answers.

  • 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-15T10:16:13+00:00Added an answer on May 15, 2026 at 10:16 am

    Since your function is going to return a vector, you will have to make a new vector (i.e. copy elements) in any case. In which case, std::remove_copy_if is fine, but you should use it correctly:

    #include <iostream>
    #include <vector>
    #include <set>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    std::vector<int> filter(const std::vector<int>& all, const std::set<int>& bad)
    {
            std::vector<int> result;
            remove_copy_if(all.begin(), all.end(), back_inserter(result),
                      [&bad](int i){return bad.count(i)==1;});
            return result;
    }
    
    int main()
    {
            std::vector<int> all_items = {4,5,2,3,4,8,7,56,4,2,2,2,3};
            std::set<int> bad_items = {2,8,4};
            std::vector<int> filtered_items = filter(all_items, bad_items);
            copy(filtered_items.begin(), filtered_items.end(), std::ostream_iterator<int>(std::cout, " "));
            std::cout << std::endl;
    }
    

    To do this in C++98, I guess you could use mem_fun_ref and bind1st to turn set::count into a functor in-line, but there are issues with that (which resulted in deprecation of bind1st in C++0x) which means depending on your compiler, you might end up using std::tr1::bind anyway:

    remove_copy_if(all.begin(), all.end(), back_inserter(result),
         bind(&std::set<int>::count, bad, std::tr1::placeholders::_1)); // or std::placeholders in C++0x
    

    and in any case, an explicit function object would be more readable, I think:

    struct IsMemberOf {
            const std::set<int>& bad;
            IsMemberOf(const std::set<int>& b) : bad(b) {}
            bool operator()(int i) const { return bad.count(i)==1;}
    };
    std::vector<int> filter(const std::vector<int>& all, const std::set<int>& bad)
    {
            std::vector<int> result;
            remove_copy_if(all.begin(), all.end(), back_inserter(result), IsMemberOf(bad));
            return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The simplest method I know of ? Using floating point… May 15, 2026 at 7:23 pm
  • Editorial Team
    Editorial Team added an answer Make sure you put quotes around the word, and use… May 15, 2026 at 7:23 pm
  • Editorial Team
    Editorial Team added an answer This seems to work: namespace WindowsFormsApplication2 { using System; using… May 15, 2026 at 7:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.