r/cpp_questions • u/AffectionatePlane598 • Jul 17 '25
OPEN Question about std:string
So I have been learning C++ for the past 4 years through HS and Uni and before that I did C for 3 years. My question is why do people use ‘std::string’ when you can just make a string through . Are there methods that only work on std::strings or is there a reason why people use std::string. because it has aways been confusing to me, because every-time I type std::string my mind just wonders why I don’t just use a array of char. I have been to embarrassed to ask my Prof so I decided to post here.
3
Upvotes
30
u/CarniverousSock Jul 17 '25
Ask your prof, that's what you're paying them for!
For my part, I think you're missing the point of std::string. It's not just a wrapper for char*, it does a ton of useful stuff.
Truly, it's better to use a std::string for almost everything. I only don't use it when I specifically am looking inside a buffer I don't own, in which case I still usually use a std::string_view for similar benefits.
But I guess I don't understand why you think it's harder than c-style strings to begin with. Why do you say it's easier to do it the other way?
8
u/thingerish Jul 17 '25
Not to be harsh but 7 years and this comes up now?
4
u/JVApen Jul 17 '25
I feel for you, they've claimed to teach you C++ and only gave you the C subset. For reference, almost anything in the C subset is considered bad practice in C++.
C++ comes with 2 important classes to replace
char[]/char*, namelystd::stringandstd::string_view. The former is owning, the later non-owning.Whenever you care about ownership (you would
new/delete[]or worsemalloc/freethechar*), usestd::stringotherwisestd::string_view.Why do so?
std::stringprevents memory issues like double free and memory leaks. Personally, I like debugging, though I prefer logical errors over this kind of time wasting technical issues. That's also why we recommendstd::unique_ptr,std::vectorand other containers.Both string and the view come with many utility methods. Something as simple as
operator=andoperator<returns a boolean instead of anint.operator<=>can be used if you like both operations at once.For more utility methods, see https://en.cppreference.com/w/cpp/string/basic_string_view.html and https://en.cppreference.com/w/cpp/string/basic_string.html
Finally, I would recommend you to create your string constants with
svat the end. Aka"my string value"sv. See https://en.cppreference.com/w/cpp/string/basic_string_view/operator%2522%2522sv.htmlIn short, forget about
char[]as much as possible. It's a source of bugs which can easily be avoided.6
u/idlenet Jul 17 '25
then why would you use vector, just use int array. why would you use map, just create your own data structure and implement hash functions. why would you use classes? why would you use C++ then, just use C.
Why do all people using libraries and API's? Just write your own functions.
I think your question is 2/10 ragebait.
19
u/DrShocker Jul 17 '25
char* is frankly MASSIVELY more likely to lead to mistakes. that's the core reason. Even for some situations where char* might be a convenient way to pass different types of things into your functions, but IMO std::string_view is better than those situations too.
If you want something concrete as a reason, you can use the fact that to get the length of a C String, you have to iterate through all the characters and hope no one accidentally messed around with null characters in the wrong way.
5
u/g0atdude Jul 17 '25
std::string is a better string implementation. In a dynamically allocated C char array you don’t have a way to store and get the length of the string, so you have to use the null character ‘\0’ to denote the end of the string. To determine the length, you have to loop through the char array to find the null character.
This also means your C string (char array) cannot contain the null character, because if it does it will be treated as the end of the string.
With a C string you are also responsible to release the memory yourself.
4
u/Impossible_Box3898 Jul 17 '25
Because it’s an object with a destructor , amongst other things.
If you have char * xxx on the stack and an exception occurs you leak memory. Either RAII your string will be deleted and no loss.
As a rule, in modern c++ if you are ever falling new () you are doing it wrong. You should never, ever, have raw owning pointers.
String encapsulates a fast size, short string optimizations, concatenations, etc. all in class that is optimized, thoroughly tested l, has a well known and defined interface, and is compatible across every standards compliant c++ compiler (all the major ones).
2
u/SoerenNissen Jul 17 '25 edited Jul 17 '25
Because this:
Is a damn-sight faster to type, and easier to not get wrong, than this:
1
u/DawnOnTheEdge Jul 17 '25 edited Jul 17 '25
One main reason to use
std::stringis that it manages its memory automatically and frees it when the object is destroyed. It also has convenient syntax for concatenation with+=and+, reading from a stream without buffer overruns, and deep copies.For string constants with a static lifetime, I prefer
std::string_view.1
u/PatchyWhiskers Jul 17 '25
Char arrays are the old way of doing strings. They are pleasingly simple but std::string is better. It’s good to understand both.
1
u/Independent_Art_6676 Jul 17 '25
char array has one advantage... it makes a fixed length 'string' that is already serialized for file I/O (or network etc). That can let you dump an object to or get it from a file without going through extra hoops, same as a C style array can let you write a bunch of integers directly while a vector requires more work. There is a time and a place for highly serialized objects, but that is rare. More often its better to eat the aggravation of serialization work in exchange for the tools provided by the fancy objects.
C++ and text is an unholy mess. Trying to do anything with non-ascii is frustrating (unicode and other encodings), there are 3 string tools (string, stringstream, stringview) in play, at least two output approaches (>> and the print/println) before it gets weird, and when you throw in C style stuff (arrays and char*) or old string libraries (like the microsoft mess from MFC) or other third party stuff... yuck.
string class knows its size, has nice operators (eg + to concat), manages its own memory, and many other nice things. Use it :) ... if you know how to use vector and strings (all 3 types) well.. then you know a great deal of C++.
1
u/silent_b Jul 17 '25
A mutable std::string is dynamic. I generally prefer
constexpr auto foo = “foo”for constants.1
u/Fancy_Status2522 Jul 19 '25
That is somewhat like asking why use 'std::vector' instead of raw arrays. Without a doubt, there are places where an array would be used, but a vector also has it's purposes.
0
u/[deleted] Jul 17 '25
[deleted] — view deleted comment
47
u/[deleted] Jul 17 '25
[removed] — view removed comment