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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:17:10+00:00 2026-06-05T06:17:10+00:00

I have two vectors of predicates: typedef std::function<bool(int)> pred; vector<pred> v1; vector<pred> v2; I

  • 0

I have two vectors of predicates:

typedef std::function<bool(int)> pred;

vector<pred> v1;
vector<pred> v2;

I need to merge them into one, but the predicates from the second vector should be inverted (i.e. they should return true in the cases they normaly return false). So, in fact i need a kind of “inverting” wrapper.

I created an inversion function and a struct which holds an original predicate and inverts its value:

struct inverted
{
  pred pr;
  inverted(pred pr_) : pr(pr_) {}

  bool operator () (int i) {return !pr(i);}
};

pred CreateInverted(pred pr)
{
  return inverted(pr);
}

Using this, the inversion of the vector predicates might look like:

transform( v2.begin(), v2.end(), v2.begin(), CreateInverted );

But I’d like to know if there are better solutions, specially based on the Standard Library?

No C++11, Boost is allowed.

  • 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-05T06:17:11+00:00Added an answer on June 5, 2026 at 6:17 am

    If your predicate follows certain conventions, you can use std::not1 to create the inverted predicate. Here is an example:

    struct foobar : std::unary_function<bool, int>
    {
        bool operator()(int value) const { return value > 42; }
    };
    
    do_something_if(values.begin(), values.end(), std::not1(foobar());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two vectors typedef std::vector<std::string> messages; typedef std::vector<std::string> addMessage; messages st; addMessage additionlMsgs;
I have two vectors of values and one vector of weights, and I need
I have two vectors of floats and i want them to become one vector
Suppose I have two vectors std::vector<uint_32> a, b; that I know to be of
I have two vectors: a vector and index vector. How can I make the
I have two vectors of integers, and for each element of the second vector
I have two vectors in a game. One vector is the player, one vector
This is kind of last resort.. So I have two maps. typedef std::map <string,
I have two vectors. vector<Object> objects; vector<string> names; These two vectors are populated and
In my code I have two vectors: vector<lipid*> lipids; vector<shared_ptr<bead> > ions; The lipid

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.