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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:01:45+00:00 2026-06-07T00:01:45+00:00

I needed to store various strings in a map but I wanted to keep

  • 0

I needed to store various strings in a map but I wanted to keep them ordered by size.

using a predicate like this:

struct strsize_less {
    bool operator()(const string& l, const string& r) {
        return l.size() < r.size();
    };
};

would accomplish the ordering but it would discard the strings with the same size

int main() {
    typedef map<string, int, strsize_less> mymap_t;
    mymap_t mymap;

    mymap["j"    ] = 0;
    mymap["i"    ] = 1;
    mymap["hh"   ] = 2;
    mymap["gg"   ] = 3;
    mymap["fff"  ] = 4;
    mymap["eee"  ] = 5;
    mymap["dddd" ] = 6;
    mymap["cccc" ] = 7;
    mymap["bbbbb"] = 8;
    mymap["aaaaa"] = 9;

    for( mymap_t::iterator i = mymap.begin(); i!=mymap.end(); ++i )
        cout << "k = " << i->first << " - > v= " << i->second << endl;

    return 0;
}

the output is:

k = j - > v= 1
k = hh - > v= 3
k = fff - > v= 5
k = dddd - > v= 7
k = bbbbb - > v= 9

My question: Is there any predicate I can use to achieve the following output:

k = j - > v= 0
k = i - > v= 1
k = hh - > v= 2
k = gg - > v= 3 
k = fff - > v= 4
k = eee - > v= 5
k = dddd - > v= 6
k = cccc - > v= 7
k = bbbbb - > v= 8
k = aaaaa - > v= 9
  • 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-07T00:01:47+00:00Added an answer on June 7, 2026 at 12:01 am

    Compare both the size and the string in the comparer:

    struct strsize_less
    {
        bool operator()(std::string const& l, std::string const& r) const
        {
            if (l.size() < r.size())
                return true;
    
            if (l.size() > r.size())
                return false;
    
            return l < r;
        };
    };
    

    Or, using C++11’s std::tie:

    struct strsize_less
    {
        bool operator()(std::string const& l, std::string const& r) const
        {
            return std::tie(l.size(), l) < std::tie(r.size(), r);
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ASP.NET MVC 2 and stuck with the situation when needed to store
I feel like I am missing something very simple here, but this is the
Needed Finfo but deleted msi package, so uninstalled php 5.3.0, downloaded 5.3.2 and installed.
I needed to create an application using Struts2 as MVC,Hibernate for data access and
I have to develope something to hold blob's. I have a various options but
The problem: We have large product images we want thumbnails of at various size
Recently in a project I am working on, I needed to store callbacks in
I am developing a light-weight online shop. This shop integrates with various sources. It
I have a requirement whereby I needed to store a simple cache of a
Previously, when I needed to store a number of related variables, I'd create a

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.