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

The Archive Base Latest Questions

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

I have a source container of strings I want to remove any strings from

  • 0

I have a source container of strings I want to remove any strings from the source container that match a predicate and add them into the destination container.

remove_copy_if and other algorithms can only reorder the elements in the container, and therefore have to be followed up by the erase member function. My book (Josuttis) says that remove_copy_if returns an iterator after the last position in the destination container. Therefore if I only have an iterator into the destination container, how can I call erase on the source container? I have tried using the size of the destination to determine how far back from the end of the source container to erase from, but had no luck. I have only come up with the following code, but it makes two calls (remove_if and remove_copy_if).

Can someone let me know the correct way to do this? I’m sure that two linear calls is not the way to do this.

#include <iostream> #include <iterator> #include <vector> #include <string> #include <algorithm> #include <functional>  using namespace std;    class CPred : public unary_function<string, bool> { public:         CPred(const string& arString)                 :mString(arString)         {         }          bool operator()(const string& arString) const         {                 return (arString.find(mString) == std::string::npos);         } private:         string mString; };  int main() {         vector<string> Strings;         vector<string> Container;          Strings.push_back('123');         Strings.push_back('145');         Strings.push_back('ABC');         Strings.push_back('167');         Strings.push_back('DEF');          cout << 'Original list' << endl;         copy(Strings.begin(), Strings.end(),ostream_iterator<string>(cout,'\n'));          CPred Pred('1');          remove_copy_if(Strings.begin(), Strings.end(),                        back_inserter(Container),                        Pred);          Strings.erase(remove_if(Strings.begin(), Strings.end(),                       not1(Pred)), Strings.end());          cout << 'Elements beginning with 1 removed' << endl;         copy(Strings.begin(), Strings.end(),ostream_iterator<string>(cout,'\n'));          cout << 'Elements beginning with 1' << endl;         copy(Container.begin(), Container.end(),ostream_iterator<string>(cout,'\n'));          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. 2026-05-11T09:23:39+00:00Added an answer on May 11, 2026 at 9:23 am

    I see your point, that you’d like to avoid doing two passes over your source container. Unfortunately, I don’t believe there’s a standard algorithm that will do this. It would be possible to create your own algorithm that would copy elements to a new container and remove from the source container (in the same sense as remove_if; you’d have to do an erase afterward) in one pass. Your container size and performance requirements would dictate whether the effort of creating such an algorithm would be better than making two passes.

    Edit: I came up with a quick implementation:

    template<typename F_ITER, typename O_ITER, typename FTOR> F_ITER move_if(F_ITER begin, F_ITER end, O_ITER dest, FTOR match) {   F_ITER result = begin;   for(; begin != end; ++begin)   {     if (match(*begin))     {       *dest++ = *begin;     }     else     {       *result++ = *begin;     }   }    return result; } 

    Edit: Maybe there is confusion in what is meant by a ‘pass’. In the OP’s solution, there is a call to remove_copy_if() and a call to remove_if(). Each of these will traverse the entirety of the original container. Then there is a call to erase(). This will traverse any elements that were removed from the original container.

    If my algorithm is used to copy the removed elements to a new container (using begin() the original container for the output iterator will not work, as dirkgently demonstrated), it will perform one pass, copying the removed elements to the new container by means of a back_inserter or some such mechanism. An erase will still be required, just as with remove_if(). One pass over the original container is eliminated, which I believe is what the OP was after.

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

Sidebar

Related Questions

I have a part of HTML source file that contains strings that I want
Within my Subversion project I have a few directories that contain other open source
I have source XMLfiles that come in with multiple root elements and there is
I have an executable that defaults to 32-bit. It doesn't have source code and
Have a source xml document that uses namespace containing prefixes and a default namespace.
I have a source base that, depending on defined flags at build time, creates
I have a List with a few strings in it. I want to see
I have a string as a HTML source and I want to check whether
I have the source for a large (>250 files) library that makes heavy use
I have a string that contains source code of an other groovy file in

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.