I would be appreciate if someone could help me!
I implement kmeans algorithm in c++.
-The points that I have to split in groups are known.
-I want to make 3 clusters of them.
-But the clusters have to be instantiated randomly in the first time.
When I try it, the following message appears and I can not solve it.
Unhandled exception at 0x00007FF92B484E20 (MengeCore.dll) in menge.exe: 0xC0000005: Access violation writing location 0x0000000000000000.
The problem is in lines when I try to instantiate clusters.
Thank you in advance!!!
#include <cassert>
#include <limits>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <list>
#include <ostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <stdio.h>
using namespace std;
class Point{
private:
double x;
double y;
public:
Point::Point(double x, double y) : x(x), y(y) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
Point::Point() {}
bool Point::operator==(const Point &rhs) const {
return x == rhs.x &&
y == rhs.y;
}
bool Point::operator!=(const Point &rhs) const {
return !(rhs == *this);
}
friend std::ostream &operator<<(std::ostream &os, const Point &point) {
os << "(" << point.x << "," << point.y << ")";
return os;
}
double getDistance(Point &p) {
return sqrt(pow(p.x - this->x, 2) + pow(p.y - this->y, 2));
}
};
class Cluster {
public:
Point centroid;
vector<int> points;
Cluster::Cluster(const Point ¢roid, const vector<int> &points) : centroid(centroid), points(points) {}
Cluster::Cluster() {
}
string getPoints() {
string s = "";
for (int p : points) {
s += to_string(p + 1) + " ";
}
return s;
}
Cluster::Cluster(const Point ¢roid) : centroid(centroid) {}
};
vector<Point> points{ { 9, 9 },
{ 1, 1 },
{ -1, -1 },
{ 3, 3 },
{ 10, 10 },
{ -2, -2 },
{ 7, 8 },
{ 0.2, 0 },
{-1, 0},
{ 6, 10 } };
vector<Cluster> clusters{};
int main() {
int K=2;
for (int i = 0; i < K; i++)
{
srand(time(NULL));
int RandIndex = rand() % 10; //generates a random number between 0 and 9
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//HERE IS THE PROBLEM
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
clusters[i].centroid = points[RandIndex];
}
return 0;
}
}
you should did not initialize the size for the cluster vector.
int main()
{
...
int K=2;
clusters.resize(K);
for (int i = 0; i < K; i++)
...
}
Related
I have a function that takes 2d-vector and outputs a 2d-vector. For some reason, the function is not getting called.
Here is the link to reproduce the issue: Google Colab.
In the link to check for correctness, I have added another code that uses the exact same function but doesn't take a 2d-vector array as an argument instead it runs on static input.
mycode.cpp:
#include <vector>
#include "geomutils.h"
#include "mycode.h"
#include <iostream>
using namespace std;
vector< vector<double> > customComputeConvexHull(vector< vector<double> > i_matrix){
cout <<"\nDone1.1";
Polygon custompts, customhull;
for (int r = 0; r < i_matrix.size(); r++){
custompts.push_back(Point(i_matrix[r][0], i_matrix[r][1]));
}
computeConvexHull(custompts, customhull);
// vector< vector<double> > res;
vector<vector<double>> res( customhull.size() , vector<double> (2));
for(int i = 0;i < customhull.size();i ++) {
res[i][0] = customhull[i].x;
res[i][1] = customhull[i].y;
}
return res;
}
void print_polygon(Polygon &h, int name){
std::cout << "\nHull in "<< name << ": \n"<<"[";
for(int i = 0;i < h.size();i ++) {
std::cout << "("<< h[i].x<< ", "<< h[i].y<<"), ";
}
std::cout <<"]\n";
}
void get_convex_hull_custom(){
Polygon custompts;
Polygon customhull;
custompts.push_back(Point(0,0));
custompts.push_back(Point(4.58,7.14));
custompts.push_back(Point(0,7.14));
computeConvexHull(custompts, customhull);
print_polygon(customhull, -99999);
}
int main()
{
// Create an empty vector
vector< vector<double> > mat,mat2;
vector<double> myRow1(0,0);
mat.push_back(myRow1);
vector<double> myRow2(7.61,9.48);
mat.push_back(myRow2);
vector<double> myRow3(0,9.48);
mat.push_back(myRow3);
cout <<"Done1\n";
get_convex_hull_custom();
mat2 = customComputeConvexHull(mat);
cout <<"Done2";
return 0;
}
mycode.h:
#ifndef _code
#define _code
#include <vector>
std::vector< std::vector<double> > customComputeConvexHull (std::vector< std::vector<double> > i_matrix);
#endif
geomutils.cpp:
#include "geomutils.h"
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
void computeConvexHull(Polygon &pts, Polygon &chull) {
chull.clear();
if(pts.size() == 1) {
chull.push_back(pts[0]);
chull.push_back(pts[0]);
return;
} else if(pts.size() == 2) {
chull.push_back(pts[0]);
chull.push_back(pts[1]);
chull.push_back(pts[0]);
return;
}
typedef boost::tuple<double, double> point;
typedef boost::geometry::model::multi_point<point> mpoints;
typedef boost::geometry::model::polygon<point> polygon;
mpoints mpts;
for(int i = 0;i < pts.size();i ++) {
boost::geometry::append(mpts,point(pts[i].x,pts[i].y));
}
polygon hull;
// Polygon is closed
boost::geometry::convex_hull(mpts, hull);
for(auto pt : hull.outer()) {
chull.push_back(Point(pt.get<0>(), pt.get<1>()));
}
}
geomutils.h:
#ifndef GEOMUTILS_H
#define GEOMUTILS_H
#include <vector>
struct Point {
double x,y;
Point(){}
Point(double x, double y):x(x),y(y){}
};
typedef std::vector<Point> Polygon;
void computeConvexHull(Polygon &pts, Polygon &chull);
#endif // GEOMUTILS_H
When I compile the code and try to run it.
Only Done1 gets printed on the console. It neither gives any error nor any message.
Output:
Done1
Hull in -99999:
[(0, 0), (0, 7.14), (4.58, 7.14), (0, 0), ]
There's some issue on your code. First of all in main to correctly initialize the vector you have to use the {} syntax. Further in customComputeConvexHull you are setting values inside the res vector which are not yet present. You have to use push_back to populate res. Below a version of your code which works (I put everything into one cpp file for semplicity.
#include <vector>
//#include "geomutils.h"
//#include "mycode.h"
#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
using namespace std;
struct Point {
double x, y;
Point() {}
Point(double x, double y) :x(x), y(y) {}
};
typedef std::vector<Point> Polygon;
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);
void computeConvexHull(Polygon& pts, Polygon& chull) {
chull.clear();
if (pts.size() == 1) {
chull.push_back(pts[0]);
chull.push_back(pts[0]);
return;
}
else if (pts.size() == 2) {
chull.push_back(pts[0]);
chull.push_back(pts[1]);
chull.push_back(pts[0]);
return;
}
typedef boost::tuple<double, double> point;
typedef boost::geometry::model::multi_point<point> mpoints;
typedef boost::geometry::model::polygon<point> polygon;
mpoints mpts;
for (int i = 0; i < pts.size(); i++) {
boost::geometry::append(mpts, point(pts[i].x, pts[i].y));
}
polygon hull;
// Polygon is closed
boost::geometry::convex_hull(mpts, hull);
for (auto pt : hull.outer()) {
chull.push_back(Point(pt.get<0>(), pt.get<1>()));
}
}
vector< vector<double> > customComputeConvexHull(vector< vector<double> > i_matrix) {
cout << "\nDone1.1";
Polygon custompts, customhull;
for (int r = 0; r < i_matrix.size(); r++) {
custompts.push_back(Point(i_matrix[r][0], i_matrix[r][1]));
}
computeConvexHull(custompts, customhull);
vector< vector<double> > res;
for (int i = 0; i < 3; i++)
{
vector<double> v1{ customhull[i].x, customhull[i].y };
res.push_back(v1);
}
return res;
}
int main()
{
// Create an empty vector
vector< vector<double> > mat, mat2;
vector<double> myRow1{0, 0};
mat.push_back(myRow1);
vector<double> myRow2{ 7.61, 9.48 };
mat.push_back(myRow2);
vector<double> myRow3{ 0, 9.48 };
mat.push_back(myRow3);
cout << "Done1";
mat2 = customComputeConvexHull(mat);
cout << "Done2";
return 0;
}
class enemy{
....
}
std::vector<std::unique_ptr<enemy> > enemies1;
for (unsigned i = 0; i < 3; ++i)
enemies1.emplace_back(...);
for (int i = 0; i < enemies1.size(); i++) {
std::cout << i <<"x: " << enemies1[i]->rect.getPosition().x << std::endl;
}
output:
100
200
400
How could I get the minimum coordinate value from multiple enemies in the vector? I want to detect the nearest enemy from the player, eg the player's coordinate is 50 and enemies are at 100, 200, 400, as you see in the above example. I want to detect the nearest enemy in the vector.
You can use min_element from
#include <algorithm>
#include <iostream>
#include <vector>
struct enemy_t
{
explicit enemy_t(const double d) :
distance{ d }
{
}
double distance;
};
int main()
{
// create a vector of enemies using the constructor with one double
std::vector<enemy_t> enemies{ 100.0,400.0,200.0,10.0 };
// the last argument to min_element is a lambda function
// it helps you define by what condition you want to find your element.
auto enemy = std::min_element(enemies.begin(), enemies.end(), [](const enemy_t& lhs, const enemy_t& rhs)
{
return lhs.distance < rhs.distance;
});
std::cout << "The minimum distance found = " << enemy->distance << "\n";
return 0;
}
For finding out the minimum you can use :
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
The above example will print out the position of the closest(minimum) enemy as you want. You just need to add the above two statements into your program.
To confirm that this works(compile and gives the expected result), below i have given an example whose output can be seen here.
#include <vector>
#include <iostream>
#include <algorithm>
struct Rectangle
{
int getPosition() const
{
return position;
}
Rectangle(int p):position(p)
{
}
int position = 0;
};
struct enemy
{
Rectangle rect;
enemy(int p): rect{p}
{
}
};
int main()
{
std::vector<enemy*> enemies;
enemy e1(600),e2(200),e3(400),e4(300), e5(100);
enemies.push_back(&e1);
enemies.push_back(&e2);
enemies.push_back(&e3);
enemies.push_back(&e4);
enemies.push_back(&e5);
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
return 0;
}
I have the following problem with my search function.
It expects 3 parameters, and one of them is something like const rational_t* v. I want to pass a vector through that parameter but it doesnt seems to work..
Code:
#include <iostream>
#include <cmath>
#include <vector>
#include "rational_t.hpp"
using namespace std;
bool search(const rational_t* v, const int n, const rational_t& x)
{
for(int i = 0; i < n; i++) {
if(v[i].value() == x.value()) {
return true;
} else {
return false;
}
}
};
int main()
{
rational_t a(1, 2), b(3), c, d(1, 2);
vector<rational_t> v;
v.push_back(a);
v.push_back(b);
v.push_back(c);
cout << "a.value()= " << a.value() << endl;
cout << "b.value()= " << b.value() << endl;
cout << "c.value()= " << c.value() << endl;
cout << search(v, v.size(), d); // Problem here
return 0;
}
I´ve also tried cout << search(v&, v.size(), d); with the reference &.
Any ideas? Thank You.
The class :
#pragma once
#include <iostream>
#include <cassert>
#include <cmath>
#define EPSILON 1e-6
using namespace std;
class rational_t
{
int num_, den_;
public:
rational_t(const int = 0, const int = 1);
~rational_t() {}
int get_num() const
{
return num_;
}
int get_den() const
{
return den_;
}
void set_num(const int n)
{
num_ = n;
}
void set_den(const int d)
{
assert(d != 0), den_ = d;
}
double value(void) const;
rational_t opposite(void) const;
rational_t reciprocal(void) const;
bool equal(const rational_t &r, const double precision = EPSILON) const;
bool greater(const rational_t &r, const double precision = EPSILON)
const;
bool less(const rational_t &r, const double precision = EPSILON) const;
bool cero_equal(const double precision) const;
void write(ostream &os = cout) const;
void read(istream &is = cin);
};
The first argument of search should be a rational_t* but you're passing a vector<rational_t>.
You want
search(v.data(), v.size(), d)
instead of
search(v, v.size(), d)
But I'd write this like this which is cleaner IMO:
bool search(vector<rational_t> & v, const rational_t& x)
{
for (int i = 0; i < v.size(); i++) {
if (v[i].value() == x.value()) {
return true;
}
else {
return false;
}
}
}
...
cout << search(v, d);
I am trying to create multiple instances of a static c++ library I wrote, but I can't create multiple instances of it... When I create two instances and write different data to them, I read the same data from both of the instances. Here is my code:
.cpp file:
// MathFuncsLib.cpp
// compile with: cl /c /EHsc MathFuncsLib.cpp
// post-build command: lib MathFuncsLib.obj
/*
DECLARING VECTORS
|3,6,4|
|9,1,5|
|2,0,2|
|5,3,6|
Should be inputted as:
int a[] = {3,6,4,9,1,5,2,0,2,5,3,6} with x = 3 and y = 4
Inputting training vals:
|0.1 (inp1),0.1 (inp2) ,0.1 (targeted output)| depends on the number of inputs and outputs
|9,1,5|
|2,0,2|
|5,3,6|
*/
//#include "stdafx.h"
#include "vector.h"
#include "iostream"
#define DEBUG
#include <stdexcept>
//using namespace std;
double* vectorLib::arrayPtr;
int vectorLib::x;
int vectorLib::y;
vectorLib::vectorLib(int xInp, int yInp) {
vectorLib::arrayPtr = new double[xInp*yInp];
vectorLib::x = xInp;
vectorLib::y = yInp;
//return 0;
}
double vectorLib::sigmoid(double inp) {
return 1 / (1 + exp(-inp));
}
double* vectorLib::getArrayPtr() {
return vectorLib::arrayPtr;
}
double vectorLib::read(int xInp, int yInp) {
#ifdef DEBUG
if (xInp >= vectorLib::x) {
std::cout << "X_OUT_OF_BOUNDS_VECTOR_READ\n";
while (1);
}
if (yInp >= vectorLib::y) {
std::cout << "X_OUT_OF_BOUNDS_VECTOR_READ\n";
while (1);
}
#endif // DEBUG
return *(arrayPtr + xInp + vectorLib::x*yInp);
}
void vectorLib::write(int xInp, int yInp, double data) {
#ifdef DEBUG
if (xInp >= vectorLib::x) {
std::cout << "X_OUT_OF_BOUNDS_VECTOR_WRITE\n";
while (1);
}
if (yInp >= vectorLib::y) {
std::cout << "X_OUT_OF_BOUNDS_VECTOR_WRITE\n";
while (1);
}
#endif // DEBUG
vectorLib::arrayPtr[xInp + vectorLib::x*yInp] = data;
}
void vectorLib::writeArr(double* inpArr) {
int i;
for (i = 0; i < vectorLib::x*vectorLib::y; i++) {
vectorLib::arrayPtr[i] = *(inpArr + i);
}
}
void vectorLib::sigmoidVect() {
int yy;
int xx;
for (yy = 0; yy < vectorLib::y; yy++) {
for (xx = 0; xx < vectorLib::x; xx++) {
write(xx, yy, sigmoid(read(xx, yy)));
}
}
write(0, vectorLib::y - 1, 1);
}
int vectorLib::getX() {
return vectorLib::x;
}
int vectorLib::getY() {
return vectorLib::y;
}
int vectorLib::totalVectSize() {
return vectorLib::x * vectorLib::y;
}
void vectorLib::printVector() {
int yy;
int xx;
for (yy = 0; yy < y; yy++) {
for (xx = 0; xx < x; xx++) {
std::cout << vectorLib::read(xx, yy);
if (xx + 1 != x) {
std::cout << ",";
}
}
std::cout << "\n";
}
}
vectorLib* vectorLib::vectorMult(vectorLib* vect1, vectorLib* vect2) {
#ifdef DEBUG
if (vect1->getX() != vect2->getY()) {
std::cout << "INPUTS_DONT_MATCH_VECTORMULT\n";
while (1);
}
#endif // DEBUG
vectorLib toRet(vect1->getX(), vect2->getY());
int i;
for (i = 0; i < vect2->getX(); i++) {
int p;
for (p = 0; p < vect1->getY(); p++) {
double tempOut = 0;
int q;
for (q = 0; q < vect1->getX(); q++)
{
tempOut += vect1->read(q, p) * vect2->read(i, q);
}
toRet.write(i, p, tempOut);
}
}
return &toRet;
}
.h file:
//#include "stdafx.h"
using namespace std;
class vectorLib
{
//int x, y;
public:
static double* arrayPtr;
static int x;
static int y;
//Constructor takes x and y of the vector
vectorLib(int xInp, int yInp);
//The pointer to the array that holds all the doubles in the vector
static double* getArrayPtr();
//Read the vector at a specified x and y
static double read(int xInp, int yInp);
//Write one double to a specific location
static void write(int xInp, int yInp, double data);
//Write the array inside the vector class
static void writeArr(double* inpArr);
//Takes sigmoid of whole vector
static void sigmoidVect();
//Returns x of vector
static int getX();
//Returns y of vector
static int getY();
//Returns total size of vector
static int totalVectSize();
//Returns a vector pointer to the multiplication result
static vectorLib* vectorMult(vectorLib* vect1, vectorLib* vect2);
//Prints vector
static void printVector();
private:
static double sigmoid(double inp);
};
Main file:
#define DEBUG
#include "stdafx.h"
#include "vector.h"
#include "iostream"
using namespace std;
int main()
{
vectorLib testVectLol(1, 3);
vectorLib testVect(3, 4);
double vectInp[] = { 1,1,1,
1,1,1,
1,1,1,
1,1,1};
double vectInp2[] = { 0.5,0.5,0.5 };
testVect.writeArr(vectInp);
testVectLol.writeArr(vectInp2);
testVect.printVector();// Both print 0.5, 0.5, 0,5
testVectLol.printVector();// Both print 0.5, 0.5, 0,5
while (1);
return 0;
}
Thanks in advance! I've been struggling with this for hours. I would really appreciate any help!
Jasper
This question already has answers here:
What is The Rule of Three?
(8 answers)
Closed 8 years ago.
I made a minimal example:
#include <iostream>
#include <conio.h>
using namespace std;
// skipped getters and setters and bounds checking for brevity
struct Vertex {
int x,y;
Vertex() {
}
Vertex(int x, int y) {
this->x = x;
this->y = y;
}
};
struct Polygon {
Vertex *vertexlist;
Polygon() {
}
Polygon(Vertex *v) {
vertexlist = new Vertex[4]; //hard coded 4 vertices for example brevity
for(int i=0;i<4;i++) {
vertexlist[i] = v[i];
}
}
Vertex& getVertex(int index) const {
return this->vertexlist[index];
}
};
struct PolyList {
Polygon *polylist;
int lastpoly;
PolyList() {
polylist = new Polygon[10]; //hard coded 10 for example brevity
lastpoly = 0;
}
void add(const Polygon& p) {
polylist[lastpoly++] = p;
}
};
ostream& operator<<(ostream& o, Vertex& v) {
return o << "(" << v.x << ", " << v.y << ")";
}
ostream& operator<<(ostream& o, const Polygon& p) {
for(int i=0;i<4;i++) {
o << p.getVertex(i) << ", ";
}
return o;
}
ostream& operator<<(ostream& o, PolyList& pl) {
for(int i=0;i<pl.lastpoly;i++) {
o << pl.polylist[i] << endl;
}
return o;
}
int someFunc() {
Vertex *vl = new Vertex[4];
PolyList pl;
vl[0] = Vertex(1,2);
vl[1] = Vertex(3,4);
vl[2] = Vertex(5,6);
vl[3] = Vertex(7,8);
pl.add(Polygon(vl)); // this Polygon goes out of scope after this line
cout << pl << endl;
}
int main() {
someFunc();
}
(So tl;dr, Polygon is a list of 4x Vertex, and PolyList is a list of Polygon:s. Polygon:s are add()ed to PolyList by instantiating a temporary Polygon)
Now, this leaks memory, because the Vertices in Polygon are never freed. However, if I add a destructor:
Polygon::~Polygon () {delete [] vertices}
then
cout << pl << endl;
will not work because the Polygon has gone out of scope and the destructor frees the vertices.
I could have the PolyList destructor call a Polygon->free() function. Alternatively, I could have the Polygon::Polygon(Vertex *v) deep copy all the vertices in v. Then PolyList::PolyList(Polygon &p) could deep copy p.
I could also make a PolyList::createPolygon(int x1, int y1, int x2, int y2...) but that flies in the face of OO.
What is the proper way to handle this kind of situation in C++? Never mind my actual example where a memory leak would not be a problem, I'm talking in principle. If I make an hierarchical object tree, I want to copy the pointers, not deep copy the objects.
EDIT: I'm trying to learn C++ on a deep level, so this is not about using vector<> or another "canned solution"; that is not what I'm after here, though I'm sure that is a good solution if the above example was an actual problem I was having. The example above is just the briefest example I could think of to explain my question.
You could use smart pointers and STL containers (mainly std::vector as suggested by PaulMcKenzie).
They will help a lot.
Your example using std::vector
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;
// skipped getters and setters and bounds checking for brevity
struct Vertex {
int x, y;
Vertex() {
}
Vertex(int x, int y) {
this->x = x;
this->y = y;
}
};
typedef vector<Vertex> vertex_list_t;
struct Polygon {
vertex_list_t vertexlist;
Polygon() {
}
Polygon(vertex_list_t v) {
//hard coded 4 vertices for example brevity
for (int i = 0; i<4; i++) {
vertexlist.push_back(Vertex(i, i));
}
}
Vertex getVertex(int index) const {
return vertexlist[index];
}
};
typedef vector<Polygon> polygon_list_t;
ostream& operator<<(ostream& o, Vertex& v) {
return o << "(" << v.x << ", " << v.y << ")";
}
ostream& operator<<(ostream& o, const Polygon& p) {
for (auto v: p.vertexlist) {
o << v << ", ";
}
return o;
}
ostream& operator<<(ostream& o, polygon_list_t& pl) {
for (auto &p : pl) {
o << p << endl;
}
return o;
}
int someFunc() {
vertex_list_t vl = {
Vertex(1, 2),
Vertex(3, 4),
Vertex(5, 6),
Vertex(7, 8)
};
polygon_list_t pl;
pl.push_back(Polygon(vl)); // this Polygon goes out of scope after this line
cout << pl << endl;
return 0;
}
int main() {
someFunc();
}
What's the real deal?
In the line
pl.add(Polygon(vl)); // this Polygon goes out of scope after this line
you pass the polygon as a temporary and:
$12.2/3- "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception."
change that line by:
Polygon p1(vl);
pl.add(p1); // this Polygon NOT goes out of scope after this line
You can use shared_ptrs as the solution. i.e.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <memory>
#include <list>
#include <vector>
using namespace std;
struct Vertex
{
int x,y;
Vertex() : x(0), y(0)
{
}
Vertex(int _x, int _y)
{
x = _x;
y = _y;
}
};
struct Polygon
{
vector<Vertex> vertexes;
Polygon()
{
}
Polygon(Vertex *v)
{
const int ELEMS_COUNT = 4;
vertexes.reserve(ELEMS_COUNT);
vertexes.insert(vertexes.end(), v, v + ELEMS_COUNT);
}
Vertex getVertex(int index) const
{
return vertexes[index];
}
};
typedef shared_ptr<Polygon> PolygonPtr;
struct PolyList
{
std::list<PolygonPtr> polylist;
void add(PolygonPtr p)
{
polylist.push_back(p);
}
};
ostream& operator<<(ostream& o, const Vertex& v) {
return o << "(" << v.x << ", " << v.y << ")";
}
ostream& operator<<(ostream& o, const Polygon& p) {
for (auto& p : p.vertexes)
{
o << p << ", ";
}
return o;
}
ostream& operator<<(ostream& o, PolyList& pl) {
for(auto& p : pl.polylist)
{
o << *p << endl;
}
return o;
}
int someFunc() {
Vertex vl[] = {Vertex(1, 2), Vertex(3, 4), Vertex(5, 6), Vertex(7, 8)};
PolyList pl;
pl.add(PolygonPtr(new Polygon(vl)));
cout << pl << endl;
return 0;
}
int main()
{
someFunc();
return 0;
}