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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:17:08+00:00 2026-05-14T00:17:08+00:00

I have a vector of Rect: vector<Rect> myRecVec; I would like to remove the

  • 0

I have a vector of Rect: vector<Rect> myRecVec;

I would like to remove the ones which are overlapping in the vector:

So I have 2 nested loop like this:

vector<Rect>::iterator iter1 = myRecVec.begin();
vector<Rect>::iterator iter2 = myRecVec.begin();

while( iter1 != myRecVec.end() ) {
    Rectangle r1 = *iter1;

    while( iter2 != myRecVec.end() ) {
        Rectangle r2 = *iter1;

        if (r1 != r2) { 
             if (r1.intersects(r2)) {
                 // remove r2 from myRectVec
             }
        }
    }
}

My question is how can I remove r2 from the myRectVect without screwing up both my iterators? Since I am iterating a vector and modifying the vector at the same time?
I have thought about putting r2 in a temp rectVect and then remove them from the rectVect later (after the iteration). But how can I skip the ones in this temp rectVect during iteration?

  • 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-14T00:17:08+00:00Added an answer on May 14, 2026 at 12:17 am
    1. You need to increment iter1 and iter2.
    2. erase returns an iterator to the next element. When you delete, use this instead of incrementing the iterator.

    Like so:

    while( iter1 != myRecVec.end() ) {
        Rectangle r1 = *iter1;
    
        iter2 = iter1 + 1;
    
        while( iter2 != myRecVec.end() ) {
            Rectangle r2 = *iter2;
    
            if( r1.intersects(r2) ) {
                iter2 = myRectVec.erase(iter2);
            }
            else {
                ++iter2;
            }
        ++iter1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of pairs representing hops between nodes which I would like
Have a vector of vectors that looks something like this 3 1 2 0
I have Vector declared in index.jsp page like this: <%! Vector vNumbers = new
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have the vector output = PV_out(:); I am trying to break this down
I have a vector of strings which are changing its contents for no apparent
I have vector with 6 numbers, which I want insert to list and add
I have a vector-like class that contains an array of objects of type T
I have Vector of elements that i set into BaseAdapter subclass and this adapter
say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them

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.