Later on I found that I can simply use std::string str(text); and it works fine and I should probably use it. But I cant seem to understand why the previous code didn't work. Any ideas?
Actually it's not the std::copy line that is the problem, it is the str.reserve() line that is the problem. The reserve() method only increases the capacity of the string it doesn't actually change the size of the string. You need to use resize() instead.
1 2
str.resize(text_lenght);
std::copy(text, text + text_lenght, str.begin());
Also you don't really need the &text[0], all you really need is the name.
you can use stl algorithms with "normal" arrays.
The algorithms need iterators and in many cases a pointer can be used as an iterator. I am not sure if there are exceptions.
the iterator would call `.pushback()' on the container, so don't have to `.resize()'
( `.reserve()' would be only an optimization to avoid reallocation, but can be omited)