Point P0(0,0), P1(3, 4), P2(-50,-3), P3(2,0); //Input Points (fine)
std::vector<Point> Points(P0,P1, P2 ,P3); (not fine)
这似乎不起作用。我该如何将点向量初始化为上述值?或者有更简单的方法吗?
Point P0(0,0), P1(3, 4), P2(-50,-3), P3(2,0); //Input Points (fine)
std::vector<Point> Points(P0,P1, P2 ,P3); (not fine)
这似乎不起作用。我该如何将点向量初始化为上述值?或者有更简单的方法吗?
如果你正在使用c++11,你可以使用大括号来内联声明向量。
std::vector<Point> Points {P0, P1, P2, P3};
尝试使用以下代码(未经测试):
Point P0(0,0), P1(3, 4), P2(-50,-3), P3(2,0); //Input Points (fine)
std::vector<Point> Points;
Points.push_back(P0);
Points.push_back(P1);
Points.push_back(P2);
Points.push_back(P3);
不需要定义Point类型的对象来定义向量。你可以这样写:
std::vector<Point> Points{ { 0, 0 }, { 3, 4 }, { -50,-3 }, { 2, 0 } };
#include <vector>
#include <iterator>
//,,,
Point a[] = { Point( 0, 0 ), Point( 3, 4 ), Point( -50,-3 ), Point( 2, 0 ) };
std::vector<Point> Points( std::begin( a ), std::end( a ) ) ;
这段代码将被 MS VC++ 2010 编译。
std::begin
和 std::end
都是 C++11 的特性,可能在旧编译器中不被完全支持。 - Edward