Hi
Has somebody got an easy example of how to use a STL map ?
really simple one to get me started please
thanks
P
Printable View
Hi
Has somebody got an easy example of how to use a STL map ?
really simple one to get me started please
thanks
P
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.
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).
Fantastic !
Thanks PadexArt just what i needed .
have a good day
:D
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
Well this line :
should be :Code:TMapInt2Float::iterator it = mapTest( 2);
Code:TMapInt2Float::iterator it = mapTest.find(2);
cool !
Btw, i am wondering what's the complexity for STL's map?
for find() , the complexity is O(logn)
Usually it is implemented as a red-black tree.
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
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).
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?
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.Quote:
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 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.
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