Quote:Original post by lancektQuote:Original post by Zahlman
What, because manually iterating over things, dealing with memory management on strings (granted there isn't any in the sample code, but presumably the string we're interested in "comes from somewhere" at runtime), etc. is "less complex"? Is this some kind of joke?
Any programmer alive can understand iteration over an array. There may be some who are (god forbid) unfamiliar with <algorithm> or even <string>.
Additionally, you are getting a pointer to the raw character data from the std::string in your version. The only real difference is that you have added an extra layer of complexity--the string class, which has no relevance whatever to this discussion.
See mikeman's post. I had a brainfart and missed the obvious thing, partly because of being distracted by these char*'s.
And lots of programmers "understand iteration over an array" in a completely different way. In Python, an explicit C++-style for loop would have to look like:
for i in range(len(container)): do_something_with(container)
New users of Python get friendly jeers for this sort of thing, as the experiences Pythonistas attempt to correct the damage that other imperative languages have done to their thought process. Imagine, writing something that strange (and it doesn't even make you handle the incrementing like C++ would!), when you could do:
for thing in container: do_something_with(thing)
Granted, C++ syntax can't be made quite so nice, but the library is a big step in that directly. Besides, the std::copy call reads quite naturally: "copy between str.begin() and 8 bytes after str.begin() to the reinterpreted-as-char* location of result". As opposed to "with i starting at zero until it reaches 8, incrementing each time, assign byte i of str to byte i of the reinterpreted-as-char* location of result".