473,486 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::map observation

Hello,

I noticed that as I insert items to std::map<std::string, std::string> map1
as:
map1["test"] = "hello";
map1["hello"] = "test";

Then use an iterator to walk in the map, the items will be retrieved by the
alphabetical order of the keys...is there is a way to allow std::map to enum
items in the order of insertion?

--
Elias
Jul 22 '05 #1
3 1618
lallous wrote:
Hello,

I noticed that as I insert items to std::map<std::string, std::string>
map1 as:
map1["test"] = "hello";
map1["hello"] = "test";

Then use an iterator to walk in the map, the items will be retrieved by
the alphabetical order of the keys...is there is a way to allow std::map
to enum items in the order of insertion?


No. An std::map is a balanced tree that stays sorted whatever you put in it.
The insertion order is lost. Read any introductory text on trees. If you
want to enum items in the order of insertion you need a linear data
structure, such as a list or a deque.

Jul 22 '05 #2
lallous wrote:
Hello,

I noticed that as I insert items to std::map<std::string, std::string> map1
as:
map1["test"] = "hello";
map1["hello"] = "test";

Then use an iterator to walk in the map, the items will be retrieved by the
alphabetical order of the keys...is there is a way to allow std::map to enum
items in the order of insertion?

--
Elias


From the implementations of the map containers I have seen, the map is
stored in a red-black, ordered tree. This greatly increases the lookup
speed. Storing in input order is very slow, and would make most
instances of std::map useless to the programmer.

If you want something ordered in the order they were put in there, use a
vector or list of pairs, and do a scan of the container (for loop) for
the key.

Brian

Jul 22 '05 #3

"lallous" <la*****@lgwm.org> wrote in message news:bu************@ID-161723.news.uni-berlin.de...
Hello,

I noticed that as I insert items to std::map<std::string, std::string> map1
as:
map1["test"] = "hello";
map1["hello"] = "test";

Then use an iterator to walk in the map, the items will be retrieved by the
alphabetical order of the keys...is there is a way to allow std::map to enum
items in the order of insertion?


The only ordering the std::map knows is the one specified in it's declaration
(by default the simple Less (operator<) ordering). You can't change it, it's
inherently how the map is arranged internally. The "insertion order" is not
remembered anywhere. If you need the insertion order, you'll have to remember
it elsewhere.

Jul 22 '05 #4

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

Similar topics

3
30007
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
1
1847
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
44
8711
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
3533
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
6115
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
3684
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
6458
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
9628
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
8
4049
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
7099
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
7123
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
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
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
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.