Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using string.at() or string[]

Tags:

c++

string

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?

like image 589
rasen58 Avatar asked Oct 08 '13 03:10

rasen58


People also ask

Should I use char [] or std::string?

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.

What does string [] mean in C++?

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.

What is the use of string []?

5. String[] args. It stores Java command-line arguments and is an array of type java.

What does string [] mean in C#?

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.


1 Answers

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().

like image 115
goji Avatar answered Sep 30 '22 08:09

goji