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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:49:57+00:00 2026-05-26T15:49:57+00:00

In C++, in addition to my question Erasing element from Vector , how can

  • 0

In C++, in addition to my question Erasing element from Vector, how can I generalise the method for deleting an element from a vector into a function that takes the following arguments: the vector, and the element to be deleted from this vector ?

bool removeElementFromVector(vector * collection, void * element) {
    for(int i=0; i<collection->size(); i++){
        if (collection[i]==element){
            swap(collection[i], collection.back());
            collection.pop_back();
            return true;
        }
    }
}

My problem here is that I don’t know how the parameter-list has to look like in order to be able for this to work with any vector<whatever*> and any object whatever!?

EDIT: Solution:

myfunctions.h

template <typename T>
bool removeElementFromVector(vector<T> & collection, T const & element) { 
    // for... 
}

myclass.h

#include "myfunctions.h"
public:
vector<Item*> items;                        
void removeItem(Item * item);          

myclass.cpp

#include "myclass.h"
void myclass::removeItem(Item * item) {
    removeElementFromVector(this->items, item);
}
  • 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-26T15:49:58+00:00Added an answer on May 26, 2026 at 3:49 pm

    In C++, the type safe way to write generic code that will work on different types is not passing void*, but rather templates. In your particular case:

    template <typename T>
    void removeElement( std::vector<T> & collection, T const & element ) {
       collection.erase( std::remove( collection.begin(), collection.end(), element ),
                         collection.end() );
    }
    

    By using a template on the contained type T, you make it generic. Internally, the idiom for removing the elements from the vector is the erase-remove idiom, which will remove the elements that match, and compact the rest of the elements forward maintaining the relative order. I have changed the pointers for references. If your container holds pointers to a given type, and the element passed is a pointer to that type, the compiler will infer T to be type* for you, but the code above will also work for containers that do not hold pointers (a bit more generic)

    If the relative order is not important, you can use the same loop that you had in your question, which will be more efficient (smaller number of copies).

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

Sidebar

Related Questions

I'm attempting implement the add method mentioned in the Generic sparse matrix addition question
In addition to this question: link i'm asking if i can retrieve data like
It's an addition for previous my question about adding columns into jqGrid-based table. Here
I'm pretty sure you can, but in addition to answering the question in the
This question comes from here but can be a totally independent question. I have
This question is the non spam bible. Anyway in addition to that when generating
In addition to this question: iPhone network performance , I would like to know
In my previous question , Andrew Jaffe writes : In addition to all of
In addition to the datastore for your specific site, can you also share one
I am aware that Dijkstra's algorithm can find the minimum distance between two nodes

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.