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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:06:27+00:00 2026-05-27T12:06:27+00:00

I’m wondering if only by applying some standard algorithms is possible to write a

  • 0

I’m wondering if only by applying some standard algorithms is possible to write a short function which compares two std::map<string, string> and returns true if all the key-value (but some) pairs are true.

For example, these two maps should be evaluated as equal

map<string,string> m1, m2;

m1["A"]="1";
m2["A"]="1";

m1["B"]="2";
m2["B"]="2";

m1["X"]="30";
m2["X"]="340";

m1["Y"]="53";
m2["Y"]="0";

Suppose that the two maps have same size and all their elements must be pairwise compared except the value stored by the key "X" and key "Y". A first attempt would be a very inefficient double nested for loop.

I’m sure a better solution can be achieved.

  • 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-27T12:06:28+00:00Added an answer on May 27, 2026 at 12:06 pm

    I am not sure what exactly you are looking for, so let me first give complete equality and then key equality. Maybe the latter fits your needs already.

    Complete Equality

    (While standard equivalence can be tested using std::map‘s own comparison operators, the following can be used as a base for a comparison on a per-value basis.)

    Complete equality can be tested using std::equal and std::operator== for std::pairs:

    #include <utility>
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <map>
    
    template <typename Map>
    bool map_compare (Map const &lhs, Map const &rhs) {
        // No predicate needed because there is operator== for pairs already.
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(),
                          rhs.begin());
    }
    
    int main () {
        using namespace std;
    
        map<string,string> a, b;
    
        a["Foo"] = "0";
        a["Bar"] = "1";
        a["Frob"] = "2";
    
        b["Foo"] = "0";
        b["Bar"] = "1";
        b["Frob"] = "2";
    
        cout << "a == b? " << map_compare (a,b) << " (should be 1)\n";
        b["Foo"] = "1";
        cout << "a == b? " << map_compare (a,b) << " (should be 0)\n";
    
        map<string,string> c;
        cout << "a == c? " << map_compare (a,c)  << " (should be 0)\n";
    }
    

    Key Equality

    C++2003

    Based on the above code, we can add a predicate to the std::equal call:

    struct Pair_First_Equal {
        template <typename Pair>
        bool operator() (Pair const &lhs, Pair const &rhs) const {
            return lhs.first == rhs.first;
        }
    };
    
    template <typename Map>
    bool key_compare (Map const &lhs, Map const &rhs) {
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(),
                          rhs.begin(),
                          Pair_First_Equal()); // predicate instance
    }
    
    int main () {
        using namespace std;
    
        map<string,string> a, b;
    
        a["Foo"] = "0";
        a["Bar"] = "1";
        a["Frob"] = "2";
    
        b["Foo"] = "0";
        b["Bar"] = "1";
        b["Frob"] = "2";
    
        cout << "a == b? " << key_compare (a,b) << " (should be 1)\n";
        b["Foo"] = "1";
        cout << "a == b? " << key_compare (a,b) << " (should be 1)\n";
    
        map<string,string> c;
        cout << "a == c? " << key_compare (a,c)  << " (should be 0)\n";
    }
    

    C++ (C++11)

    Using the new lambda expressions, you can do this:

    template <typename Map>
    bool key_compare (Map const &lhs, Map const &rhs) {
    
        auto pred = [] (decltype(*lhs.begin()) a, decltype(a) b)
                       { return a.first == b.first; };
    
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), pred);
    }
    

    C++ (C++14)

    added 2014-03-12

    Using the new generic lambda expressions, you can do this:

    template <typename Map>
    bool key_compare (Map const &lhs, Map const &rhs) {
    
        auto pred = [] (auto a, auto b)
                       { return a.first == b.first; };
    
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), pred);
    }
    

    As a style-matter, you can also inline the lambda expressions in C++11 and C++14 directly as a parameter:

    bool key_compare (Map const &lhs, Map const &rhs) {
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), 
                          [] (auto a, auto b) { return a.first == b.first; });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT

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.