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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:27:34+00:00 2026-06-15T17:27:34+00:00

I create a map: map<string, string> cuts; cuts[cutb] = a>1; cuts[cuta] = b>3; cuts[cutc]

  • 0

I create a map:

map<string, string> cuts;

cuts["cutb"] = "a>1";
cuts["cuta"] = "b>3";
cuts["cutc"] = "c<5";

When I iterate over this map with map<string, string>::iterator itr = cuts.begin(); it gets ordered alphabetically: cuta, cutb, cutc, etc.

How do I force the iterator to follow the order in which I’ve defined the elements i.e. cutb, cuta, cutc?

  • 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-15T17:27:35+00:00Added an answer on June 15, 2026 at 5:27 pm

    You need to use some other implementation of the data structure. The red-black tree implementation will not do for you in your case.

    Looking at it from more general perspective: why you need to create a map if you traverse the entries in order of addition? Won’t list/array/vector of pairs suffice for you?

    EDIT: Tip: whenever you choose to use a specific data structure ask yourself what exactly operations you need it to support. After answering that, you should be able to choose more precisely. Map is good if you need to find by key, insert by key and to check if a certain key exists. However, the price you will pay in the implementations that optimise those operations is usually that you lose the order of insertion of the elements.

    EDIT2: looping through list of pairs:

    // pair is class in namespace std
    vector<pair<string, string> > v;
    v.push_back(make_pair("cutb", "a>1"));
    v.push_back(make_pair("cuta", "b>3"));
    v.push_back(make_pair("cutc", "c<5"));
    for (int i = 0; i < v.size(); i++) {
      // the first element of the pair is addressed with .first; the second - .second
      cout << "For key " << v[i].first << " the value is "  << v[i].second << endl;
    }
    

    Iterating in vector can also be done using iterators, but I like the index approach more, as it is just as optimal, but easier to understand.

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

Sidebar

Related Questions

How want to create a Map<String , Object> . In this map everytime the
I want to create a map Integer to String , and this map order
I need to create a real dynamic JPA CriteriaBuilder . I get an Map<String,
My aim is to create a map function like this map = function ()
In Bash I can create a map (hashtable) with this common construction hput() {
I have a map of type: map[string]interface{} And finally, I get to create something
I need to create a map with three types. map<string,string,double> How do I create
I have a map of constants, like this: private static Map<String, Character> _typesMap =
Given a Map[String, Int] in Scala, I want to create a trait that allows
I try create a map for open ~/.vimrc, but open the ~/.vimrc only when

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.