The class template std::vector
has constructor
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
Thus in this declaration
vector<Pair> arr(a, a + n);
there is used this constructor. a
and a + n
specify the range [a, a + n )
. Elements in this range are used to initialize the vector.
As for this declaration
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
then it is a declaration of an array each element of which is initialized by using a brace list. It seems that that the user-defined type Pair
is either an aggregate or has a constructor that accepts two arguments.
Here is a demonstrative program
#include <iostream>
#include <vector>
struct Pair
{
int x;
int y;
};
int main()
{
Pair a[] =
{
{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}
};
size_t n = sizeof( a ) / sizeof( a[0] );
std::vector<Pair> arr(a, a + n);
for ( const Pair &p : arr )
{
std::cout << "{ " << p.x
<< ", " << p.y
<< " }" << ' ';
}
std::cout << std::endl;
return 0;
}
Its output is
{ 5, 29 } { 39, 40 } { 15, 28 } { 27, 40 } { 50, 90 }
Instead of these statements
size_t n = sizeof( a ) / sizeof( a[0] );
std::vector<Pair> arr(a, a + n);
you could just write
std::vector<Pair> arr( std::begin( a ), std::end( a ) );
Here is another demonstrative program
#include <iostream>
#include <vector>
#include <iterator>
struct Pair
{
int x;
int y;
};
int main()
{
Pair a[] =
{
{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}
};
std::vector<Pair> arr( std::begin( a ), std::end( a ) );
for ( const Pair &p : arr )
{
std::cout << "{ " << p.x
<< ", " << p.y
<< " }" << ' ';
}
std::cout << std::endl;
return 0;
}
Its output is the same as shown above.