5 ms·
to beat the STL objects in terms of efficiency string s = "string literal"; vector<int> v = {1, 2, 3}; // C++0x feature Any language with built-in string/arr
by mojuba 19y ago
to beat the STL objects in terms of efficiency
string s = "string literal";
vector<int> v = {1, 2, 3}; // C++0x feature
Any language with built-in string/array support will beat STL in initializing things, to start with. STL does dynamic allocation in these examples, in case you didn't know.
- aston 19y agoAre you sure about that? I don't really know the internals, but I would assume the implementation looks something like: Let the compiler put the array (of characters or numbers) somewhere where I can get them, and I'll take that pointer and keep it in my stack-allocated STL data structure. Any references that say you're right?
- mojuba 19y agoThe best reference is the source code. Let's look at the string. In C/C++ there is no reliable and portable way for your program to find out whether some (char*) points to a constant string literal or not. So the only oprion for a dynamic string implementation is to always copy any data the constructor receives to somewhere else, normally to the dynamic memory. As a result, any initialization involves dynamic allocation and copying of data in C++. At the same time languages that have built-in support for dynamic structures avoid this by creating proper structures in the constant segment. So a statement like string s = "abc" would do nothing but assign a pointer. This is a huge problem in C++ and even C++0x doesn't solve it unfortunately.
- jey 19y agoC++0x introduces move constructors. http://www.google.com/search?q=move+constructors+c%2B%2B0x http://www.google.com/search?q=move+constructors+c%2B%2B0x
- jsjenkins168 19y agoYou are certainly right, but I was simply comparing creating custom data structures in C vs. STL in C++ since that is what Linus was comparing. You are arguing that other languages which have built in static string/array support will be faster than C/C++ and I dont argue with that.
- mojuba 19y agoI think even in C or with C-ish programming with C++ you can achieve better performance (in return for some verbosity, of course).