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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:30:52+00:00 2026-06-08T23:30:52+00:00

I am trying to delete an element in a vector of Objects. The vector

  • 0

I am trying to delete an element in a vector of Objects. The vector is filled with instances of Object and at some point, I want to remove a certain element in a vector not by index, but by the element itself.

A simple example would be:

std::vector< string > strVector;
strVector.push_back( "abc" );
strVector.push_back( "def" );
strVector.push_back( "ghi" ); // So strVector should contain "abc", "def", and "ghi"

How do I remove “ghi” from that vector? Note that I don’t know where “ghi” is in that vector.

// Something like this. Assume strVector = [ "abc", "cba", "ccb", "bac", "aaa" ]
strVector.removeElement( "ccb" );

A more relevant example to what I a working on:

class MyClass {
   std::vector< Object > myObjVector;
   void main( ARGS ) {
      for ( int i = 0; i < 10; i++ ) {
         Object myObject = Object( );
         myObjVector.push_back( myObject );
      }

      int j = getANumber( ); // j could be any number within the size of the vector
      Object myOtherObject = myObjectVector.at( j );

      // How do I erase myOtherObject (which is an object inside the vector) ?
      removeFromVector( myOtherObject );
   }
}

I hope the question’s clear. Thanks in advance.

EDIT: I figured it out, thanks to all those who answered. The trick was to give the class something unique that identifies it (like a name or a tag, as long as they are guaranteed to be unique) then use the erase-remove idiom to remove the object from the array.

  • 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-08T23:30:54+00:00Added an answer on June 8, 2026 at 11:30 pm

    If your use-case has no duplicates, then you are better off using an std::set and using the std::set::erase which takes a value.

    std::set< string > strSet;
    strSet.insert( "abc" );
    strSet.insert( "def" );
    strSet.insert( "ghi" );
    strSet.insert( "ccb" );
    
    strSet.erase("ccb");
    

    If you need to cope with duplicates, then you have to specify the desired behaviour of the removal. Should it remove one or all of the elements matching a value? Do you care about preserving the order of the remaining elements? If you require using a vector, then lokk at the erase-remove idiom. But note that std::vector::erase has linear time complexity, whereas the relevant variant of std::set::erase has logarithmic time complexity. And erase-remove would remove all elements equal to the given value.

    Note: if you want to use an std::set for a user defined type, you must provide either a less-than bool operator<(const UserType&) const or a comparison function or functor, implementing strict weak ordering.

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

Sidebar

Related Questions

I have a vector populated with objects: std::vector<Stuff*> stuffVector; and am trying to delete
I'm trying to delete certain lines from a file. My file is a .txt
I am trying to delete all specific key element in my linked list in
i am trying to remove parent tag, but removing parent tag should not remove
I am trying to initiate a click event for multiple instances of an element
I'm trying to delete the whole content of an RootPanel element based on an
I'm trying to delete everything from a std::vector by using the following code vector.erase(
I am trying to delete an element on a specific match between the value
I am trying to delete the child element in the dom from its parent
I am trying to delete a hidden element. I have tried the following codes

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.