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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:33:05+00:00 2026-05-31T22:33:05+00:00

I have a std::vector with the element as pair and need to find all

  • 0

I have a std::vector with the element as pair and need to find all the elements with the first component within some ranges, for example, to find all elements such that abs(value of first component – reference)<0.01, I got the code from other post that

bool comp(pair<double, double> v1, pair<double, double> v2)
{
  return (abs(v1.first-v2.first)<0.001);
}

vector< pair<double, double> data;
for (int i=0; i<10; i++)
{
  double a, b; 
  // a & b are initialized randomly (the code was not shown here)
  data.push_back(make_pair(a, b));
}

// we sort the vector here based on the pair.first (code not shown)

// compval is used as reference value, i.e. we are going to find all elements with     abs(pair.first-ref.first)<0.01
pair<double, double> ref
ref.first = 0.5;
ref.second = 0.5;

std::pair< vector< pair<double, double>::iterator, vector< pair<double, double>::iterator> const range = std::equal_range(data.begin(), data.end(), ref, comp);

but if you look at all elements given by the resulting ranges, you will see that all elements of the data instead of the one satisfying abs(pair.first-ref.first)<0.01 will be returned. Is that anything I miss in the code? Thanks.

  • 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-31T22:33:06+00:00Added an answer on May 31, 2026 at 10:33 pm

    equal_range requires a sorted range.

    The order implied by your comparison function does not match the order that you sorted the range into – and so the call to equal_range has undefined behaviour.

    For example – say your list contained {{.5, 0}, {.6, 0}} (these are sorted), and you then applied std::bind(comp({.5,.5},_1)) to each element.

    comp({.5,.5},{.5,0}) would return true.

    comp({.5,.5},{.6,0}) would return false.

    Your sort order is saying: “.5 is less than .6“, but at the same time your comp function is saying “.6 is less than .5” (because a there is a value which is less than .5 but not less than .6). This is contradictory, and the cause of your problems.

    To actually find all the elements such that std::bind(comp({.5,.5}, _1)) returns true, you could use std::copy_if(data.begin(), data.end(), some_output_iterator, std::bind(comp(ref, std::placeholders::_1))) (although there are a variety of different ways of doing this, depending on your exact needs).

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

Sidebar

Related Questions

I have some numbers stored in a std::vector<int> . I want to find which
I have a std::vector and I want to check all the elements in it.
suppose I have a std::vector of pair. How can I use, efficiently, method std::find
I have a std::vector<int> , and I want to delete the n th element.
Say, I have a std::vector<SomeClass *> v; in my code and I need to
A different question inspired the following thought: Does std::vector<T> have to move all the
Let's say I have a std::vector and I get by some means the address
I have a table represented by a std::vector where every element is a boost::fusion::vector
I have some vector of elements passed to a function and I want to
If I have a vector of pairs: std::vector<std::pair<int, int> > vec; Is there and

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.