473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Iterating over an STL map

Hi all,

I read somewhere that the STL map class stores entries internally sorted by the
key. I wrote a small test program (below) that verifies that iterating through
the map returns entries with ascending key values. My question is: Is this
guaranteed behavior or is it just the current implementation which might change
in the future?

TIA - Bob

--- Code example ---

#include "stdafx.h"
#include <map>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
map<int, stringmyMap;

myMap[2] = "2";
myMap[5] = "5";
myMap[1] = "1";
myMap[3] = "3";
myMap[6] = "6";

for (map<int, string>::iterator it = myMap.begin();
it != myMap.end(); it++)
{
cout << it->first << " : " << it->second.c_str() << endl;
}

return 0;
}
Jun 27 '08 #1
3 1213
Bob Altman wrote:
Hi all,

I read somewhere that the STL map class stores entries internally sorted by the
key. I wrote a small test program (below) that verifies that iterating through
the map returns entries with ascending key values. My question is: Is this
guaranteed behavior or is it just the current implementation which might change
in the future?
Bob:

It is guaranteed.

--
David Wilkinson
Visual C++ MVP
Jun 27 '08 #2
>I read somewhere that the STL map class stores entries internally sorted by the
>key. I wrote a small test program (below) that verifies that iterating through
the map returns entries with ascending key values. My question is: Is this
guaranteed behavior or is it just the current implementation which might change
in the future?
I'd expect it to be guaranteed.

The sorting is done by the traits object - and defaults to less<Key>,
but if need be you can always specify your own.

Dave
Jun 27 '08 #3
"David Lowndes" <Da****@example.invalidwrote in message
news:k3********************************@4ax.com...
I read somewhere that the STL map class stores entries internally sorted by
the
key. I wrote a small test program (below) that verifies that iterating
through
the map returns entries with ascending key values. My question is: Is this
guaranteed behavior or is it just the current implementation which might
change
in the future?

I'd expect it to be guaranteed.

The sorting is done by the traits object - and defaults to less<Key>,
but if need be you can always specify your own.

Dave
Many thanks to both Davids! - Bob
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards...
4
by: Fernando Rodríguez | last post by:
Hi, While iterating through a list I'd like to know not just the current element, but also its index. Is there a better way than this: i = 0 newList = for element in aList:...
7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.