What is the difference between using sort(str.begin(), str.end())
and using sort(std::begin(str), std::end(str))
in C ?
Both of these functions gave the same result as the sorted string but is there any difference between the two, and what is the reason for having both?
CodePudding user response:
Same thing, but if you write begin(str)
it also works with things like arrays. If str was an array then str.begin()
wouldn't work because an array isn't a class. Therefore some people like to make a habit of writing begin(str)
.
CodePudding user response:
From the c reference of std::begin
, for a non-array:
- Returns exactly c.begin(), which is typically an iterator to the beginning of the sequence represented by c. If C is a standard Container, this returns C::iterator when c is not const-qualified, and C::const_iterator otherwise.
Hence, using str.begin()
or std::begin(str)
for a std::string
str is exactly the same.