3
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) 

This does not seem to work. How do I initialize points vector to the values above? Or is there an easier way to do this?

1

3 Answers 3

5

If you are using c++11, you can use braces to declare vectors inline.

std::vector<Point> Points {P0, P1, P2, P3}; 
Sign up to request clarification or add additional context in comments.

4 Comments

unfortunately, i do not have c++11
i got it.. data.push_back(Point(1,1));
Then you'll have to add them separately. You probably do have c++11 though, unless you are using an ancient compiler.
Helpful link about VS and C++ 11
4

Try the following code (not tested):

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); 

Comments

3

There is no need to define objects of type Point that to define the vector. You could write

std::vector<Point> Points{ { 0, 0 }, { 3, 4 }, { -50,-3 }, { 2, 0 } }; 

provided that your compiler supports the brace initialization. Or you could define an array of Point and use it to initialize the vector. For example

#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 ) ) ; 

This code will be compiled by MS VC++ 2010.

3 Comments

Brace-enclosed initializer lists and std::begin and std::end are all features of C++11 which may not be fully supported in older compilers.
@Edward I wrote clear that MS VC++ 2010 supports the last example in my post.
indeed you did. I was adding to your answer by naming the specification which added these features to the language so that others with different compilers will be able to understand what they need to be able to make use of your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.