Strings and *Char[]s

Hi, I'll get right to the point. What's the point in using the std::string class type if it isn't supported by <string.h> header functions?

I ask because MS Visual C++ 2008 Express Edition uses the string type, and I need to use strcat on it somehow. Ideas?

Thanks,
YottaFlop.
Because char* s are far easier to screw up then std::strings. You don't need to use strcat() or and string.h functions with std::strings.
What's the point in using the std::string class type if it isn't supported by <string.h> header functions?


Let me turn that around.

What's the point of using <string.h> header functions when they don't work with the std::string class type?

I ask because MS Visual C++ 2008 Express Edition uses the string type, and I need to use strcat on it somehow


Just use the += operator:

1
2
3
4
string foo = "This is ";
foo += "an example";

// foo == "This is an example" 



std::string provides several benefits over char arrays:

- dynamic resizing
- no buffer overflow problems
- memory management (no memory leaks)
- recorded length means faster concatenation. strcat/strlen have to parse the whole string every call, whereas string's .length() and += operator don't.
and if you still need to access individual chars the [] operators for std::string work just fine.
http://www.cplusplus.com/reference/string/string/
What pleasant answers. Thanks, especially for the link. :)
Topic archived. No new replies allowed.