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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:36:19+00:00 2026-05-22T20:36:19+00:00

given a std::vector< std::string > , the vector is ordered by string length, how

  • 0

given a std::vector< std::string >, the vector is ordered by string length, how can I find a range of equal length strength?

I am looking forward an idiomatic solution in C++.

I have found this solution:

// any idea for a better name? (English is not my mother tongue)
bool less_length( const std::string& lhs, const std::string& rhs )
{
    return lhs.length() < rhs.length();
}

std::vector< std::string > words;
words.push_back("ape");
words.push_back("cat");
words.push_back("dog");
words.push_back("camel");
size_t length = 3;
// this will give a range from "ape" to "dog" (included):
std::equal_range( words.begin(), words.end(), std::string( length, 'a' ), less_length );

Is there a standard way of doing this (beautifully)?

  • 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-22T20:36:19+00:00Added an answer on May 22, 2026 at 8:36 pm

    I expect that you could write a comparator as follows:

    struct LengthComparator {
        bool operator()(const std::string &lhs, std::string::size_type rhs) {
            return lhs.size() < rhs;
        }
        bool operator()(std::string::size_type lhs, const std::string &rhs) {
            return lhs < rhs.size();
        }
        bool operator()(const std::string &lhs, const std::string &rhs) {
            return lhs.size() < rhs.size();
        }
    };
    

    Then use it:

    std::equal_range(words.begin(), words.end(), length, LengthComparator());
    

    I expect the third overload of operator() is never used, because the information it provides is redundant. The range has to be pre-sorted, so there’s no point the algorithm comparing two items from the range, it should be comparing items from the range against the target you supply. But the standard doesn’t guarantee that. [Edit: and defining all three means you can use the same comparator class to put the vector in order in the first place, which might be convenient].

    This works for me (gcc 4.3.4), and while I think this will work on your implementation too, I’m less sure that it is actually valid. It implements the comparisons that the description of equal_range says will be true of the result, and 25.3.3/1 doesn’t require that the template parameter T must be exactly the type of the objects referred to by the iterators. But there might be some text I’ve missed which adds more restrictions, so I’d do more standards-trawling before using it in anything important.

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

Sidebar

Related Questions

i have a code like this.... std::vector<string>::iterator p; p = find(v.begin(),v.end(),asdasda); cout << *p
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
Given just a std::string iterator, is it possible to determine the start and end
std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a
Given a sequence number, I need to find the corresponding request message string. I
I'm currently stuck finding the correct syntax for trimming each string in a std::vector.
Please consider the following example: #include <string> #include <vector> using std::string; using std::vector; template
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Here is what my algorithm does: It takes a long std::string and divides it
Given a range of indexes (identifiers), where I want to map each index to

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.