Reputation: 38
In my program I need to check the first character of a std::string
, and use something like this:
if(string.front() == '-')
I wonder, does using the code below instead make any difference?
if(string[0] == '-')
Upvotes: 2
Views: 616
Reputation: 1180
Both front()
and operator[]
returns reference to element, so no difference;
If you needs bounds checking you can use std::string::at
, which does the boundary checking and throws std::out_of_range
exception.
Upvotes: 0