In C++, is it bad to use string[x] to get a char at a specific location?
Most people use string.at(x) but is there a reason why string[x] is bad?
Use std::string when you need to store a value. Use const char * when you want maximum flexibility, as almost everything can be easily converted to or from one. Save this answer.
One of the most useful data types supplied in the C++ libraries is the string. A string is a variable that stores a sequence of letters or other characters, such as "Hello" or "May 10th is my birthday!". Just like the other data types, to create a string we first declare it, then we can store a value in it.
5. String[] args. It stores Java command-line arguments and is an array of type java.
In C programming, a string is a sequence of characters terminated with a null character \0 . For example: char c[] = "c string"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character \0 at the end by default.
As far as I'm aware most people don't use string.at(). If your code is well written and well understood, you should always be working within the bounds of your string so don't need the run-time bounds checking that string.at() provides. Same goes for the other sequence containers with .at().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With