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

  • Home
  • SEARCH
  • 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 6657081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:43:23+00:00 2026-05-26T01:43:23+00:00

So I have 2 std::map s <string, shared_ptr<file> > one is ‘old’ one is

  • 0

So I have 2 std::maps <string, shared_ptr<file> > one is ‘old’ one is ‘new’ I want to get what files were removed and so be capable to iterate thrue differene and do some stuff to shared_ptr. Is such thing possible and how to do it?

  • 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-26T01:43:24+00:00Added an answer on May 26, 2026 at 1:43 am

    While it’s easy enough to write this yourself (iterate over A and check if the key is present in B), this looks like a job for std::set_difference. We’ll need a lambda or some custom predicate to compare keys, though:

    #include <iterator>
    #include <map>
    #include <string>
    #include <algorithm>
    
    typedef std::map<std::string, MyPtr> my_map;
    
    my_map A; // given
    my_map B; // given
    
    void make_a_difference()
    {
      my_map C; // will hold the result
    
      std::set_difference(A.begin(), A.end(),
                          B.begin(), B.end(),
                          std::insert_iterator<my_map>(C, C.end()),
                  [](const my_map::value_type & a, const my_map::value_type & b)
                  { return a.first < b.first; }
                         );
    }
    

    If you want to write this yourself, you should consider taking advantage of the fact that both ranges are already sorted, so you can do better than a flat search for existence by advancing two iterators in parallel.

    If you don’t have C++11, just use this predicate instead of the lambda:

    bool my_comp(const my_map::value_type & a, const my_map::value_type & b)
    {
      return a.first < b.first;
    }
    

    Beware that there is no comparison on the mapped type! So if you have the same string key in both maps, then there will be no such item in the result, even if the two mapped values differ. If this is undesirable, you need a different output container (e.g. a std::multimap<my_map::key_type, my_map::mapped_type>) and a different predicate.

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

Sidebar

Related Questions

I have a std::map< std::string, std::string> cont; I want to see cont[ some_key ]
So I have such function: boost::shared_ptr<my_class> get_class_by_name(std::string name) { typedef std::map<boost::shared_ptr<my_class>, my_description> map_t; BOOST_FOREACH(map_t::value_type
This is kind of last resort.. So I have two maps. typedef std::map <string,
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
If I have a std::vector or std::map variable, and I want to see the
I have created a map with ClassExpression as the key and std::string as the
Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have an std::map<std::string, float> so I can do quick lookups for float values
I have a std::map< std::string, std::string> which initialized with some API call. When I'm

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.