Is there a difference between
vector<int> v (istream_iterator<int>(cin),istream_iterator<int>()); and
vector<int> v; copy (istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(v)); ?
Is there a difference between
vector<int> v (istream_iterator<int>(cin),istream_iterator<int>()); and
vector<int> v; copy (istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(v)); ?
The first option is preferable, because it is more concise and allows you to declare an object constant.
However, in order for it to work in the expected way, you should replace the round brackets with curly brackets:
vector<int> v {istream_iterator<int>(cin),istream_iterator<int>()}; Otherwise, the initial construction will be interpreted by the compiler as a declaration of the function v , which takes 2 arguments (one of istream_iterator<int> with name cin , the second - an unnamed pointer to a function without arguments, which returns istream_iterator<int> ) and returns vector<int> . You can read more about why this happens, for example, on the wiki on the most vexing parse page.
Given the syntax correction, there is no difference in the final result. But why make it more difficult when it is easier?
In principle, the difference is not great, the result is the same. Simply, in the first case, the copy constructor is called, and in the second case, the standard constructor is first called, and then the copy is made.
Source: https://ru.stackoverflow.com/questions/509052/
All Articles