What the hell went wrong? We started with just this:
char a[100];
strcpy(a, "a string");
strcat(a, " more");
And someone said "Hey, it would be better if the programmer didn't have to declare that 100 manually... Is there some way the string itself could just resize as characters are added?"
Now, you can't really fault whoever it was for thinking that. It seems like a reasonable idea. But tell me, how the fuck did we get from that idea to this?
>>1
Oh, and don't forget, if you want your strings to also have the amazing functionality of sprintf() and sscanf(), you'll also need the ABOMINATIONS known as std::istringstream and std::ostringstream... But those probably won't do what you want, so you might as well go ahead and download boost::format(), which you can only get by installing the ENTIRE BOOST LIBRARY, which is over TEN THOUSAND separate files.
Your hard drive is now completely full, so you can't build anything, but if you could, you'd be able to do almost exactly the same neat stuff you could do with sprintf() and sscanf().
Your hard drive is now completely full
After installing a library that is not much more than 100 MB?
Ten thousand files
Did you forget that they're all header files?
Name:
Anonymous2012-05-23 13:56
>>4
Boost is just as bad as Perl. One big chunk of inter- and cyclic dependencies, some components nearly forgotten and/or badly maintained. I wouldn't touch it, not even with a 10-foot pole.
Name:
Anonymous2012-05-23 14:15
C++ turned into this huge bloated monster
so the Boost devs thought of a solution
make an even bigger bloated monster to kill the huge bloated monster
the monsters became friends
Name:
Anonymous2012-05-24 7:24
bampu pantsu
Name:
Anonymous2012-05-24 8:30
>>4
Not really, I can just use a fixed length char array with sprintf() and concatenate that onto my C++ string.
>>7 and returns the pointer (pass by reference).
0/10
Name:
Anonymous2012-05-24 19:01
>>20
In C, some people use ``pass by reference'' as a shorthand for ``stick an & in front of a variable'' as opposed to ``pass an uninitialized pointer''.
Contrast: struct faggot faggot;
init_faggot(&faggot); /* sometimes called ``pass by reference'' */
And: struct faggot *faggot_ptr;
init_faggot(faggot_ptr); /* shit, what did I blow up this time? */
init_faggot(&faggot); /* sometimes incorrectly called ``pass by reference'' */
FTFY
Name:
Anonymous2012-05-24 20:17
ALGOL 68 is older than C, B, Unix, and null-terminated strings. Not only does ALGOL 68's string change its length when needed, it also has the +, +:=, +=: and * (repeat) string operators found in most modern languages. ALGOL 68 also originated the printf function (which is type-safe and more powerful than C's), the type-safe tagged union and the void type. stringa := "a string"; a +:= " more"
In many applications you will know the exact (maximum) length of the string a priori, so you can size appropriately.
For the cases where it isn't, exponential resize is the best solution.
The C++ standards guys wanted std::basic_string to do everything everyone wanted, and that's what the result is. You might find it amusing that std::basic_string<char>::operator==() eventually calls memcmp() in a few of the STL implementations.
You might find it amusing that std::basic_string<char>::operator==() eventually calls memcmp() in a few of the STL implementations.
Why would that be surprising? Many compilers are able to optimize a call to that function very efficiently. And what do you care about how a method is implemented?
In any case, std::string is awfully bloated. But it was added to C++ before STL, because everybody made an incompatible and buggy string implementation of their own. When the standard containers were later added, the string class was augmented to support iterators and ranges, and what's found in <algorithm>.
>>22
There's a difference between ``how'' and ``what''. When you see the function
int factorial(int n)
{
return n == 0 ? 1 : n * factorial(n - 1);
}
you don't think of a function that takes an integer, passed by value, which depending on it being zero or not returns either a constant or its value multiplied with the result of a recursive call. That's just the ``how''. Instead, when you see the name of the function, and a quick glance of the body you immediately think of what it does, namely the factorial.
Pass-by-reference is not a language feature. It's a semantic property of a procedure.
Name:
Anonymous2012-05-25 9:53
why didnt you just do something like
char * new_string(char *old_str, char *str_to_add)
{
char * n_str = (char *)malloc(sizeof(old_str) + sizeof(str_to_add) + 1);