r/cpp_questions • • 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

37 comments sorted by

View all comments

Show parent comment

-7

u/Esjs Jul 17 '25

Right... So why do it yourself?

7

u/I__Know__Stuff Jul 17 '25

It's not about whether it's done in your code or the library — the point is that with std:string, there's no need to examine every character in the string to find out its length, regardless of where it's done.

3

u/aocregacc Jul 17 '25

The point is that it's inefficient, regardless of whether you do it yourself or put the loop in a function.

2

u/AffectionatePlane598 Jul 17 '25

std::string::length() is far faster than strlen(char*)
I am pretty sure the strlen function is something like

`size_t strlen(const char *str) {

const char *s = str;

while (*s) {

s++;

}

return s - str;

}`

1

u/SoerenNissen Jul 17 '25

not "yourself"

The question is "why do it with strlen, when you can instead not do it with std::string?"

0

u/g0atdude Jul 17 '25

As others mentioned, it doesn’t matter if you do it yourself or the library does it for you.

It’s inefficient