对初始化向量
vector of pair initialization
谁能帮我理解这段代码是如何工作的?
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
int n = sizeof(a)/sizeof(a[0]);
vector<Pair> arr(a, a + n);
(Pair是一个有两个整数a和b的结构体)
据我所知,它将每一对放在一个单独的数组中,但我以前从未见过这种声明。
class 模板 std::vector
有构造函数
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
因此在此声明中
vector<Pair> arr(a, a + n);
使用了这个构造函数。 a
和 a + n
指定范围 [a, a + n )
。此范围内的元素用于初始化向量。
至于这个声明
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
那么它是一个数组的声明,每个元素都是用大括号列表初始化的。似乎用户定义的类型 Pair
是一个聚合或具有接受两个参数的构造函数。
这是一个演示程序
#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;
}
它的输出是
{ 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);
你可以写
std::vector<Pair> arr( std::begin( a ), std::end( a ) );
这是另一个演示程序
#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;
}
其输出与上图相同
谁能帮我理解这段代码是如何工作的?
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
int n = sizeof(a)/sizeof(a[0]);
vector<Pair> arr(a, a + n);
(Pair是一个有两个整数a和b的结构体)
据我所知,它将每一对放在一个单独的数组中,但我以前从未见过这种声明。
class 模板 std::vector
有构造函数
template <class InputIterator>
vector(InputIterator first, InputIterator last, const Allocator& = Allocator());
因此在此声明中
vector<Pair> arr(a, a + n);
使用了这个构造函数。 a
和 a + n
指定范围 [a, a + n )
。此范围内的元素用于初始化向量。
至于这个声明
Pair a[] = {{5, 29}, {39, 40}, {15, 28}, {27, 40}, {50, 90}};
那么它是一个数组的声明,每个元素都是用大括号列表初始化的。似乎用户定义的类型 Pair
是一个聚合或具有接受两个参数的构造函数。
这是一个演示程序
#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;
}
它的输出是
{ 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);
你可以写
std::vector<Pair> arr( std::begin( a ), std::end( a ) );
这是另一个演示程序
#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;
}
其输出与上图相同