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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:02:48+00:00 2026-06-12T18:02:48+00:00

Find upper_bound of a String Key in STL Map I’m trying to find the

  • 0

Find upper_bound of a String Key in STL Map

I’m trying to find the upper_bound of String Key in STL Map , but its not giving me the exact result. If you can run this program, you will find the result as weird, both upper & lower bounds pointing to “qwerzzx”

Is there any mistake in my Code or i’m misinterpretating upper bound operation ..?

#include<iostream> 
#include<cstring>
#include <map>
using namespace std;
int main()
{
    map<string, int> testmap;
    map<string, int>::iterator poslow;
    map<string, int>::iterator posup;

    testmap.insert(make_pair<string, int>("asdfghjkliopp", 1));
    testmap.insert(make_pair<string, int>("asdfghjklioppswert", 1));
    testmap.insert(make_pair<string, int>("sdertppswert", 1));
    testmap.insert(make_pair<string, int>("sdertppswedertyuqrt", 1));
    testmap.insert(make_pair<string, int>("qwerzzx", 1));
    testmap.insert(make_pair<string, int>("qwerzzxasdf", 1));
    testmap.insert(make_pair<string, int>("qwsdfgqwerzzx", 1));
    testmap.insert(make_pair<string, int>("xcvbqwsdfgqwerzzx", 1));
    testmap.insert(make_pair<string, int>("xcvbqwsdersdfgqwerzzx", 1));
    poslow = testmap.lower_bound("qw");
    posup = testmap.upper_bound("qw");
    cout<<"Lower POS  ::: "<<poslow->first<<" UPPER POS :: "<<posup->first<<"\n";
    testmap.erase(poslow, posup);
}
  • 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-12T18:02:50+00:00Added an answer on June 12, 2026 at 6:02 pm

    Upper bound gives you the last position where you can insert the argument, while still keeping the sequence sorted (whereas lower_bound gives you the first such position). Since “qw” is lexicographically smaller than “qwerzzx”, that’s both the lower and upper bound for that word.

    In other words, [lower_bound, upper_bound) is the interval of elements that are equal to the argument – and in this case, it is empty.

    If your intention is to find the last word with this prefix, you can try appending some characters in the end to make sure it’s lexicographically larger than the last one in the map. For example, if you only have alphabetical characters, you can lookup the character right after 'z' in the ASCII table and append it to “qw”. This way you should be able to get an iterator to, in your case, “xcvbqwsdfgqwerzzx”.

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

Sidebar

Related Questions

i find a lot of questions about this problem but i did not solve...
I am trying to find if an image has in its source name noPic
I find it not so easy as one might think to start with TDD
I'm dealing with very large memory mapped files (200+ GB) which can not be
I am trying to work out a few guesses on algorithm complexity, but every
The documentation for upper_bound states: ...it attempts to find the element value in an
In Java, find if the first character in a string is upper case without
Let's say I'm using a map that uses three strings as a key. Here's
I'm trying to find the location (lat/long) of a street address w/o city -
I am trying to efficiently find all the JIRA issues that were closed in

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.