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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:35:24+00:00 2026-06-14T01:35:24+00:00

Is there a convenient way to remove items from a vector (or another stl

  • 0

Is there a convenient way to remove items from a vector (or another stl container) whose values are within an certain interval?

So for example: I got an vector with float values

1.1 1.3 2.2 3.2 4.1 5.2 5.1 1.1 8.0 2.1

and a delta of 0.2, which should lead to the following result

1.1 2.2 3.2 4.1 5.1 8.0

thus removing all “duplicate” items within the delta and keep one of the values within the range.
It can be assumed that the values are “clustered”, where the difference between these is more than 3*delta. Only one value (the mean) of the cluster should be kept, all the others from the cluster should be removed.

For sure, it is possible to iterate with nested loops, but this seems quite complicated, because of the changing iterators, so I thought of a more convenient way. I found remove_if for example, but this function cannot “compare”.

Thanks for suggestions.

  • 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-14T01:35:27+00:00Added an answer on June 14, 2026 at 1:35 am

    You can use std::unique with a predicate:

    template <typename It, typename Predicate>
    It unique(It first, It last, Predicate pred);
    

    The most used form of std::unique takes no predicate and just removes duplicates from a sequence. But you can write one that implements your filter (in your case, compares two values using a gap) and you are set. Something like:

    bool CompareWithGap(double a, double b)
    {
      return abs(a - b) <= 0.2;
    }
    

    And use it to call std::unique:

    auto it = std::unique(v.begin(), v.end(), CompareWithGap);
    

    Where v is your vector (or any other sequence).

    EDIT: Forgot to mention that you need to sort your sequence before using std::unique. If this is not an option, you have to write your own algorithm.

    2nd EDIT: For completion, as pointed out by Christian Rau in his comment, you can now erase the items removed from your sequence by using the erase method in it:

    v.erase(it, v.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a convenient way to have global variables accessible within modules,without compiler errors,
Is there any convenient way to read and parse data from incoming request. E.g
Is there any convenient way in C++ to convert a vector to a vector
Is there an convenient way to log access to stored procedures from withing Oracle?
I'm looking for a convenient way to use jQuery to remove all classes from
Is there a convenient way to generate code from any new view controllers I've
Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy
Is there a convenient way to do this for my extension? I know I
Is there any convenient way to type in braces in Math in Latex? Especially
Is there any convenient way to take an array/set of objects and create a

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.