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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:04:59+00:00 2026-06-13T13:04:59+00:00

Could not easily find a solution online… I have something similar to the following.

  • 0

Could not easily find a solution online…

I have something similar to the following.

class Color {
  public:
    Color(std::string n) : name(n) {}
    typedef std::tr1::shared_ptr<Color> Ptr;
    std::string name;
 };

meanwhile elsewhere…

void Function()
{
    std::vector<Color::Ptr> myVector;
    Color::Ptr p1 = Color::Ptr(new Color("BLUE") );
    Color::Ptr p2 = Color::Ptr(new Color("BLUE") );

    // Note: p2 not added.
    myVector.push_back( p1 );

    // This is where my predicament comes in..
    std::find( myVector.begin(), myVector.end(), p2 );
}

How would I write this so my std::find would actually deference the smart_pointers and compare the objects strings rather than their memory addresses? My first approach was to write a custom std::equal function however it does not accept templates as its own template types.

  • 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-13T13:05:00+00:00Added an answer on June 13, 2026 at 1:05 pm

    Easiest may be to use find_if:

    template <typename T>
    struct shared_ptr_finder
    {
        T const & t;
    
        shared_ptr_finder(T const & t_) : t(t_) { }
    
        bool operator()(std::tr1::shared_ptr<T> const & p)
        {
            return *p == t;
        }
    };
    
    template <typename T>
    shared_ptr_finder<T> find_shared(std::tr1::shared_ptr<T> const & p)
    {
        return shared_ptr_finder<T>(*p);
    }
    
    #include <algorithm>
    
    typedef std::vector< std::tr1::shared_ptr<Color> >::iterator it_type;
    it_type it1 = std::find_if(myVector.begin(), myVector.end(), find_shared(p2));
    it_type it2 = std::find_if(myVector.begin(), myVector.end(), shared_ptr_finder<Color>(*p2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried looking at similar problems, but could not easily find one that helped
I could not find a way to make a RTL paragraph in Docx using
I could not find the function that returns the name of the cell referenced.
I could not find a clear answer to this question elsewhere, so I'll try
I could not find a post to do it automatically as I don't want
I am new to WPF and data binding so I could have easily missed
Could not load file or assembly 'EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its
I could not integrate the Test Flight sdk in my ios App for crash
I could not figure out how to get the white fill for this ggplot2
I could not understand this error, the scenario is: This is the directory structure:

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.