Как проинициализировать и вывести в консоль вектор пользовательского типа
Необходимо создать шаблон функции принимающей вектор, инициализирующий его произвольными значениями и выводящую в консоль. Подскажите, почему не получается проинициализировать вектор.
Шаблон функции.
#include <iostream>
#include <array>
#include <vector>
template <typename T> void Func (std::vector <T> Vec)
{
if (Vec.size()==0)
{
for (int i = 0; i < 5; i++)
{
Vec.push_back(rand() % 10);
std::cout << Vec[i] << " ";
}
}
else if (Vec.size() < 6)
{
int a = Vec.size();
for (int i = 0; i < a; i++)
{
Vec.push_back(rand() % 10);
std::cout << Vec[i] << " ";
}
}
std::cout << "\n";
std::cout << "size =" << Vec.size() << "\n";
std::cout << "capacity =" << Vec.capacity() << "\n";
std::cout << "max_size =" << Vec.max_size() << "\n";
}
Мой класс
#include <iostream>
class Point
{
private:
int m_x;
int m_y;
public:
Point();
Point(int x, int y);
Point(const Point& other);
int Get_X();
int Get_Y();
void Set_X(int x);
void Set_Y(int y);
bool operator == (const Point& A1) const;
bool operator != (const Point& A);
bool operator < (const Point& A);
friend std::ostream& operator << (std::ostream& os, const Point& a);
};
std::ostream& operator << (std::ostream& os, const Point& a);
#include "Point.h"
Point::Point(int x, int y)
{
m_x = x;
m_y = y;
}
Point::Point()
{
m_x = 0;
m_y = 0;
}
Point::Point(const Point& other)
{
m_x = other.m_x;
m_y = other.m_y;
}
int Point::Get_X()
{
return m_x;
}
int Point::Get_Y()
{
return m_y;
}
void Point::Set_X(int x)
{
m_x = x;
}
void Point::Set_Y(int y)
{
m_y = y;
}
bool Point::operator==(const Point& A) const
{
return (m_x == A.m_x && m_y == A.m_y);
}
bool Point::operator!=(const Point& A)
{
return !(m_x == A.m_x && m_y == A.m_y);
}
bool Point::operator<(const Point& A)
{
if (this->m_x < A.m_x && this->m_y < A.m_y)
{
return true;
}
else { return false; }
}
std::ostream& operator<<(std::ostream& os, const Point& a)
{
os << a.m_x << " " << a.m_y;
return os;
}
main
vector <Point> vPoint1(3);
Ответы (1 шт):
Автор решения: George
→ Ссылка
c template:
#include <iostream>
#include <array>
#include <vector>
class Point
{
private:
int m_x;
int m_y;
public:
Point();
Point(int x, int y);
Point(const Point& other);
int Get_X();
int Get_Y();
void Set_X(int x);
void Set_Y(int y);
bool operator == (const Point& A1) const;
bool operator != (const Point& A);
bool operator < (const Point& A);
friend std::ostream& operator << (std::ostream& os, const Point& a);
};
std::ostream& operator << (std::ostream& os, const Point& a);
Point::Point(int x, int y)
{
m_x = x;
m_y = y;
}
Point::Point()
{
m_x = 0;
m_y = 0;
}
Point::Point(const Point& other)
{
m_x = other.m_x;
m_y = other.m_y;
}
int Point::Get_X()
{
return m_x;
}
int Point::Get_Y()
{
return m_y;
}
void Point::Set_X(int x)
{
m_x = x;
}
void Point::Set_Y(int y)
{
m_y = y;
}
bool Point::operator==(const Point& A) const
{
return (m_x == A.m_x && m_y == A.m_y);
}
bool Point::operator!=(const Point& A)
{
return !(m_x == A.m_x && m_y == A.m_y);
}
bool Point::operator<(const Point& A)
{
if (this->m_x < A.m_x && this->m_y < A.m_y)
{
return true;
}
else { return false; }
}
Point gen_func(){
return Point(rand() % 10,rand() % 10)
}
std::ostream& operator<<(std::ostream& os, const Point& a)
{
os << a.m_x << " " << a.m_y;
return os;
}
template<typename T>void Func (std::vector <T> Vec)
{
if (Vec.size()==0)
{
for (int i = 0; i < 5; i++)
{
Vec.push_back(gen_func());
std::cout << Vec[i] << " ";
}
}
else if (Vec.size() < 6)
{
int a = Vec.size();
for (int i = 0; i < a; i++)
{
Vec.push_back(gen_func());
std::cout << Vec[i] << " ";
}
}
std::cout << "\n";
std::cout << "size =" << Vec.size() << "\n";
std::cout << "capacity =" << Vec.capacity() << "\n";
std::cout << "max_size =" << Vec.max_size() << "\n";
}
int main(){
std::vector <Point> vPoint1(3);
Func<Point>(vPoint1);
std::cout << " ";
return 0;
}