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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:33:21+00:00 2026-06-14T23:33:21+00:00

I created this template function to find and delete and item from collection of

  • 0

I created this template function to find and delete and item from collection of shared_ptr

template<class T>
bool FindAndDelete(set<shared_ptr<T>>& collection, shared_ptr<T> item)
{
    auto foundItem = find(collection.begin(), collection.end(), item);
    if(foundItem != collection.end())
    {
        collection.erase(foundItem);
        return true;
    }
    else
    {
        return false;
    }
}

Question:
How could I generalize it more to cover all collections? (vector, list, etc…)

for example

template<class K, class T>
bool FindAndDelete(K<shared_ptr<T>>& collection, shared_ptr<T> item);

Note: I come from C#, so maybe the code is a bit off 🙂 Correct me please

  • 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-14T23:33:22+00:00Added an answer on June 14, 2026 at 11:33 pm

    If you want to remove elements from a container, then something like this would work:

    template<class K>
    bool FindAndDelete(K& collection, typename K::value_type item);
    

    Bear in mind that the value_type of maps is an std::pair<key_type, mapped_type>, so you may want to provide special versions for those, for example

    template<typename T, typename K>
    bool FindAndDelete(std::map<T,K>K& collection, 
                       typename std::map::<T,K>::key_type key);
    

    and similarly for std::multimap and C++11 std::unordered_* variants. These containers have find member functions that are more efficient than std::find, so it would be worthwhile to have dedicated implementations of findAndDelete to take advantage of this.

    You could also have a look at std::remove_if and the erase remove idiom as an alternative to your implementation for non-associative containers. This could be more efficient in the case where you have duplicates.

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

Sidebar

Related Questions

I created this example The remove function doesn't work, and I can't figure out
The error is in this code: //myutil.h template <class T, class predicate> T ConditionalInput(LPSTR
I created a class which implements the Sieve of Atkin to find primes. The
As of right now I've created a template class, and I've created a registration
I have created this editor template: @model DateTime? @using MyMvcApp.Properties <div id=dateTimePicker_@(ViewData.ModelMetadata.PropertyName)> <script type=text/javascript
I created this report in iReport Professional 4.5.1 and deployed it to JasperReports Server
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
I created this layout of successive text input fields, 1- Enter data into empty
I created this regular expression to validate names: ^[a-zA-Z0-9\s\-\,]+.\*?$ Is there a way add
I created this regex to match all words that start with @ in my

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.