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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:54:02+00:00 2026-05-28T01:54:02+00:00

How can i directly find the number of elements between any two iterator pointing

  • 0

How can i directly find the number of elements between any two iterator pointing to two different key in map without the for loop?

     #include <string>
     #include <iostream>
     #include <map>
     #include <utility>
     #include <iterator>
     #include <stdio.h>
     using namespace std;
     int main()
   {

     std::map<string, string> m;
     m.insert(pair< string, string>("4-2"," 61-7" ));
     m.insert(pair< string, string>("5-2"," 61-7" ));
     m.insert(pair< string, string>("5-3"," 61-7" ));
     m.insert(pair< string, string>("5-4"," 61-7" ));
     m.insert(pair< string, string>("5-5"," 61-7" ));
     m.insert(pair< string, string>("5-6"," 61-7" ));

     map<string, string>::iterator it4;
     map<string, string>::iterator it5;
     it4=m.find("5-2");
     it5=m.find("5-5");
     cout << " value of it4" << it4->first << it4->second <<  endl;
     cout << " value of it5" << it5->first << it5->second <<  endl;
     m.clear();
     getchar();

  }

My desired output is difference of elements betwen it5 and it4 which here is 2 as elements with key “5-3” and “5-4” lies between them . I dont want to iterate in for loop as in my case number of elements in map are large enough . And I have to regularly know the difference . using a for loop will take lot of time if two iterator are far away .

  • 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-28T01:54:02+00:00Added an answer on May 28, 2026 at 1:54 am

    You can use std::distance(it4, it5) from <iterator>. The cost will be linear in the result, though, so that doesn’t really save you. If you must have quick access to this information, consider a Boost.Multi_index container so you can add a random-access index to your map.

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

Sidebar

Related Questions

The examples I can find use a two layer architecture, where the controllers directly
How can I easily find the greatest number in a given list of numbers?
Is there any way that I can find the container pointed to by an
One can, of course, use fopen or any other large number of APIs available
I have created a page on my site where users can post directly to
JS library like JQuery can be linked directly from other site (e.g. google). Usually
I know with swift directly you can create a Recipient_List and do ->addBcc() .
To my surprise and delight I read that an adminsitrator can import (nearly directly)
Basic doubt...If QTP generates vbscript code as we record actions, can't we directly write
I'm attempting to use Panda with my GWT application. I can upload videos directly

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.