[C++] Use map::operator[] for inserting

Started by
16 comments, last by T1Oracle 18 years, 6 months ago
Quote:Original post by T1Oracle
What if the "lazily" copied string goes out of scope?

link <-Slow STL string performance. I'm sure I found it somewhere else though, but this is all I have right now.

*edit* Also, I only use my class for STL maps and the printf feature, after that I usually convert it to a STL string. I'm most concerned about performance in the STL map since I use those heavily in my code.


If the lazily copied string goes out of scope, typically the copy gains control of the memory used to store the string.

This is a small implementation detail - copy-on-write is a tried and tested idiom for reducing wasted cpu usage and works very well.


If you are so concerned about speed, I suggest you use a hash table rather than std::map.
Advertisement
Quote:Original post by T1Oracle
link <-Slow STL string performance. I'm sure I found it somewhere else though, but this is all I have right now.


I just checked that link, most of those results are pretty much useless just as VC++ 6.0 is altogether a useless C++ compiler. VC++ 6.0 has poor standard compliance and a mediocre implementation of the C++ standard library, is almost 8-10 years old technology, it's development/release predates C++ ISO standardization what do you expect?.

Quote:Original post by T1Oracle
Also, I only use my class for STL maps and the printf feature, after that I usually convert it to a STL string.


Be careful with variable argument lists in C++ you can only use POD-types with them. Consider string streams or boost::lexical_cast (which uses string streams internally) and boost::format for printf style format string.

Quote:Original post by T1Oracle
I'm most concerned about performance in the STL map since I use those heavily in my code.


Well use a modern, update C++ compiler and profile first. Also note that all standard library containers are parameterized by allocator type, usually the last template type parameter which defaults to use std::allocator this is even the case for std::basic_string. You could use a pooled allocator for both std::map and std::basic_string better yet use it with hash_map/std::tr1::unordered_map if your compiler supports those std lib extensions.
Thats the most retarted article I've seen in a while.

std::map<int, sometype>
vs
std::map<char*, sometype>
vs
std::map<string, sometype>

aren't even doing the same thing. attempting to perform a lookup using a char* as the key sounds to be like its asking for trouble, because a char* could be pointing to the same string value but the pointers would have different values, resulting in a failed find(). There really isnt much difference in using int and char* as the key unless some compiler magic is turning it into a strcmp as a char* specialization or something(is it?).

If you really demand a string key you would be better off hashing the key and storing the numeric hash in the map as the key.
Quote:Original post by snk_kid
I just checked that link, most of those results are pretty much useless just as VC++ 6.0 is altogether a useless C++ compiler. VC++ 6.0 has poor standard compliance and a mediocre implementation of the C++ standard library, is almost 8-10 years old technology, it's development/release predates C++ ISO standardization what do you expect?.

I didn't realise at first, but it's even worse than that. Check the sourcecode of the profiled program. [lol]

Enigma

Quote:Original post by Enigma
I didn't realise at first, but it's even worse than that. Check the sourcecode of the profiled program. [lol]


I think you've said enough for me to not need to look [grin]. It just goes to show always take online resources with a very fine grain of salt.
Interesting, although hashes have the problem of their unpredictable collisions. I also like the simplicity of being able to do this:
SOMEHASHMAP_t myMap;const char key[]="some key";myMap[key]; // returns the value stored at that key

I guess now I have to do some more research to find a good way of achieving that. I just want fast, safe, associative arrays in C++.
Programming since 1995.
Quote:Original post by T1Oracle
... I also like the simplicity of being able to do this:
*** Source Snippet Removed ***
...


A hash_map would likely give you better performance for large key/value lookups with a string key.

The problem AFAIK with a char* as the key is that the actual pointer value is the lookup, which has little to do with the string that is being pointed to. With string pooling by the compiler this may work in many cases, especially if your lookups are hard coded in the code such as your example.

Your code snippet would work fine if your key is an std::string, because it will create an std::string from your char * to do the lookup with. If more speed is what you need try a stdext::hash_map.
Thanks, I found the hash_map and tried it with "const char*" and it didn't work. Now I know why, and now I have it fixed.
Programming since 1995.

This topic is closed to new replies.

Advertisement