2

In this question (https://stackoverflow.com/questions/6926433/how-to-shuffle-a-stdvector) user703016 used the following syntax for accessing the iterators of vector cards_:

For C++98 they recommended using: cards_.begin() and cards_.end()

For C++11 they recommended using: std::begin(cards_) and std::end(cards_)

For C++14, which syntax is preferable, and is there any real difference between the two? Prior to today I've only seen the first syntax.

1 Answers1

5

As long as you're dealing with a vector, it makes no difference which you use.

std::begin() and std::end() will, however, work in some situations where .begin() and .end() won't work. The primary one is working with a built-in array. For example, this will work fine:

char foo[] = "17395";

std::sort(std::begin(foo), std::end(foo));

Of course foo.begin() and foo.end() can't possibly work, since a built-in array doesn't have any member functions.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111