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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:46:54+00:00 2026-05-20T01:46:54+00:00

I am using C++, I have a vector of paired variables, for which the

  • 0

I am using C++, I have a vector of paired variables, for which the first element is a vector and the second element is an integer. My problem is that I am trying to remove certain elements from this vector, and can’t seem to get it to work!

The code is shown below, and everything is working as I’d expect up to the line:

vec1.erase(i);

as I can cout some other relevant variables from the vector to the screen, and they are correct.

compareVecs is just an element of this class that compares some variables.

void myCode::PairRemoval(vector<myTypeDef> &vec1, const vector<myTypeDef> &vec2, double conditionMax) {
  bool condition=false;
  for (unsigned int i=0; i<vec1.size(); i++){
    for (unsigned int j=0; j<vec2.size(); j++){
      if (vec1[i].first.compareVecs(vec2[j].first) <= conditionMax) {
    condition = true;
       break;
      }
     }  
    if (condition) {
      vec1.erase(i);
      cout<<"removed"<<endl;
    } 
  }
}

I hope to remove the pair from this vector, if the condition is met.

Thanks in advance for any help!

  • 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-20T01:46:55+00:00Added an answer on May 20, 2026 at 1:46 am

    You shouldn’t iterate a vector AND erase elements inside the loop. When you erase elements, the vector size changes and the indexes also change. A recommended way would be to store the elements you have to remove in another structure (like a vector) and then loop to remove them.

    void myCode::PairRemoval(vector<myTypeDef> &vec1, const vector<myTypeDef> &vec2, double conditionMax) {
        bool condition=false;
        vector<vector<myTypeDef>::iterator> toRemove;
        for (vector<myTypeDef>::iterator it = vec1.begin(); it != vec1.end(); it++){
            for (unsigned int j=0; j<vec2.size(); j++){
                if ((*it).first.compareVecs(vec2[j].first) <= conditionMax) {
                    condition = true;
                    break;
                }
            }  
            if (condition) {
                toRemove.push_back(it);
            } 
        }
        for(int i = 0; i < toRemove.size(); i++){
            vec1.erase(toRemove[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a plug-in vector established using System.AddIn that accepts the body of a
I have a program that draws some vector graphics using System.Drawing and the Graphics
I have a class that looks like this : #include <iostream> #include <vector> using
given that I have a vector: x <- c(m1, m2, m3) and every element
I have a vector, call it x, which contains very small numbers that I
I have a vector of Student objects which I want to sort using #include
I have a vector drawing app where users can draw lines using multiple quadratic
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have a .NET web service (using asmx...have not upgraded to WCF yet) that
I have an arbitrary number of lists which I would like to process using

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.