|
-
August 27th, 2003, 07:58 AM
#1
How to use an STL map ???
Hi
Has somebody got an easy example of how to use a STL map ?
really simple one to get me started please
thanks
P
-
August 27th, 2003, 08:09 AM
#2
Code:
#include <map>
using namespace std;
typedef map< int, float> TMapInt2Float;
void main()
{
TMapInt2Float mapTest;
mapTest.insert(TMapInt2Float::value_type(0,1.1));
mapTest.insert(TMapInt2Float::value_type(1,1.2));
mapTest.insert(TMapInt2Float::value_type(2,2.3));
mapTest.insert(TMapInt2Float::value_type(3,3.5));
mapTest.insert(TMapInt2Float::value_type(4,3.4));
mapTest.insert(TMapInt2Float::value_type(5,7.8));
//find a value
TMapInt2Float::iterator it = mapTest( 2);
if( it != mapTest.end())
{
float f = it->second;
}
//iterate the map
for( it = mapTest.begin(); it != mapTest.end(); ++it)
{
int key = it->first;
float value = it->second;
}
}
Note: the example is written in Codeguru's message editor. it might contain errors.
Har Har
-
August 27th, 2003, 08:12 AM
#3
One more thing. you should check the Microsoft Developer Network ( MSDN) as it contains a lot of samples and documentation for STl ( at least for beginner level).
Har Har
-
August 27th, 2003, 08:15 AM
#4
Fantastic !
Thanks PadexArt just what i needed .
have a good day
-
August 27th, 2003, 09:00 AM
#5
Unfotunately some of your code doesn't work when i try to use it ?
i get an exception error from
float f = it->second
and also an initialising error from
TMapInt2Float::iterator it = maptest( 2);
any ideas ?
also i cannot find any help in the msdn to do with STL maps ??
i have created the map ok i just want to know access a certain value by its key ...
thanks
P
-
August 27th, 2003, 09:27 AM
#6
Well this line :
Code:
TMapInt2Float::iterator it = mapTest( 2);
should be :
Code:
TMapInt2Float::iterator it = mapTest.find(2);
-
August 27th, 2003, 09:37 AM
#7
-
August 27th, 2003, 10:37 AM
#8
Btw, i am wondering what's the complexity for STL's map?
-
August 27th, 2003, 10:48 AM
#9
for find() , the complexity is O(logn)
Usually it is implemented as a red-black tree.
-
August 27th, 2003, 07:26 PM
#10
Thanks Philip,
In that case, i guess insert() and erase() will also take O(log(n)) if it is implemented with a red-black tree.
It's a pity and i dont understand why in C++'s STL I can not find a structure which takes the complexity of O(1), like a hashtable.
cheers
-
August 27th, 2003, 07:41 PM
#11
Just about every STL implementation has a hash_map
container, its just not in the standard yet (but it
certainly will be in the next iteration of the standard).
For example, I think that VC.net has a hash_map. I know
that the upgrade from Dinkumware for VC 5&6 has one.
For a standard map() , inserting is O(logn) ... there is
a version of insert() where you can give a "hint" on
where to start the search, but to be honest, I have
never seen a good pratical example of this.
Remove is O(1) if you have the iterator position that
you want to remove. But generally, you would need
to do a find() to get that iterator, which is O(logn).
-
August 27th, 2003, 08:05 PM
#12
OK. Now i understood why i only heard the hash_map but can not find it. Thanks philip.
But if it still need the find(), which is on O(log(n)), to locate the items, then it is not a big change, and then what the "hash" does in this case?
-
September 26th, 2003, 11:17 PM
#13
MSDN is a terrible source for info on the STL. There are mistakes in the documentation!!!
http://www.sgi.com/tech/stl/
DEFENDER OF ALL THINGS STL!!!
-
September 26th, 2003, 11:27 PM
#14
Originally posted by STL MAN
MSDN is a terrible source for info on the STL. There are mistakes in the documentation!!!
http://www.sgi.com/tech/stl/
I don't know who wrote the STL documentation provided with VC but I doubt it is Microsoft. Yet if there are mistakes, they want to know about it. They will fix it if you report the mistakes.
I have not had success getting answers using any STL documentation available using the internet, and i have tried using the SGI documentation.
Whoever you are, you seem to be here only to make trouble.
-
September 26th, 2003, 11:57 PM
#15
STL MAN does not know how providing a link for a site devoted to
the STL is causing trouble. It is true that there are no really good
sites devoted to the STL, but probably the worst is MSDN.
Ultimately the best option is to invest in a good book.
The C++ Standard Library by Nicolai Josuttis
DEFENDER OF ALL THINGS STL!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|