我有一个std::vector<T>
类型的多项式和一个std::vector<std::vector<T>>
类型的矩阵,它们都有类。每个类的输出(在我的情况下为void print() - function)工作正常。现在我创建了一个由多个多项式like this组成的矩阵,我不完全确定如何为它创建输出。
假设我有一个多项式p1:Polynomial<T> p1{0,1,2}
。我的Polynomial :: print函数正确解释它并返回:x^2+x+1
。
对于正常的&#39;类型Matrix<T> m1(2,2,0)
的矩阵,填充0&#39;的2x2矩阵,使用Matrix :: print()以及:
0, 0
0, 0
现在,假设我想要一个多项式矩阵m1:Matrix<Polynomial<T>> mp(2,2,p1)
,一个2x2矩阵填充的多项式p1。代码被接受,但现在我想使用matrix :: print()打印矩阵,以便得到:
x^2+x+1, x^2+x+1
x^2+x+1, x^2+x+1
最小的工作示例:
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
class Polynomial;
template <typename T>
class Matrix
{
public:
Matrix(std::vector<std::vector<T> > ma);
Matrix(int rows, int cols, T const & init);
Matrix(const Matrix & m);
~Matrix();
Matrix ();
void print();
friend void Polynomial<T>::print();
private:
std::vector<std::vector<T>> Ma;
};
template <typename T>
Matrix<T>::Matrix(std::vector<std::vector<T>> ma)
:Ma(ma)
{}
template <typename T>
Matrix<T>::Matrix(int rows, int cols, T const & init)
{
std::vector<std::vector<T>> b(rows, std::vector<T>(cols, init));
Ma=b;
}
template <typename T>
Matrix<T>::~Matrix()
{}
template <typename T>
Matrix<T>::Matrix() :
Matrix(1,1,0)
{}
template <typename T>
Matrix<T>::Matrix(const Matrix & m)
{
Ma = m.Ma;
}
template <typename T>
void Matrix<T>::print()
{
for (auto i = 0; i < Ma.size(); i++)
{
for (auto j = 0; j < Ma[i].size(); j++)
if ( j == Ma.size()-1 )
{
cout << Ma[i][j]; //This causes problems
}
else
cout << Ma[i][j] << ", \t";
cout << endl;
}
}
template <typename T>
class Polynomial
{
public:
Polynomial(std::vector<T> const& coef);
Polynomial(std::initializer_list<T> const& coef);
void print();
const int getdeg();
const T getKoeff(int index) const;
friend class Matrix<T>;
Polynomial ();
friend void print();
private:
std::vector<T> coefficient;
};
template <typename T>
Polynomial<T>::Polynomial(std::vector<T> const& coef) :
coefficient(coef)
{}
template <typename T>
Polynomial<T>::Polynomial(std::initializer_list<T> const& coef) :
coefficient(coef)
{}
template <typename T>
Polynomial<T>::Polynomial ()
{
coefficient = new std::vector<T> [coefficient.getdeg()];
coefficient[0]=0;
}
template <typename T>
void Polynomial<T>::print() //Reduced version for demonstration purposes, but
{
for ( int i = getdeg(); i >= 0; i-- )
{
cout << coefficient[i] << "x^" << i; //the output is always of the type cout << xyz
}
}
template <typename T>
const int Polynomial<T>::getdeg()
{
int g = coefficient.size()-1;
return g;
}
int main()
{
typedef double T;
Polynomial<T> p1{0,1,2};
p1.print();
Matrix<Polynomial<T>> mp(2, 3, Polynomial<T>{0});
// mp.print(); //When this is commented out chaos ensues
return 0;
}
返回 2x^21x^10
(注意:+&amp; - 从符号中排除,因为代码太长了。)
由于cout
在处理多项式的结果时遇到问题,matrix :: print()可能会导致问题。
有人知道如何使matrix :: print()为多项式矩阵提供有用的结果吗?提前谢谢。
答案 0 :(得分:1)
在Matrix<T>::print()
你试图打电话
cout << Ma[i][j];
当Ma[i][j]
为Polynomial<double>
时,输出运算符没有匹配的重载。您可以改为拨打Ma[i][j].print()
,但之后Matrix
无法使用int
。 Long story short:您不必编写自己的print()
,而是最好为您的类型重载operator<<(std::ostream&,const T&)
。
Polynomial
对于#include<iostream>
#include<vector>
template <typename T> struct Polynomial { std::vector<T> data; };
template <typename T>
std::ostream& operator<<(std::ostream& o,const Polynomial<T>& pol) {
auto power = [](int n){
return (n==0) ? "" : (n==1) ? "x" : "x^" + std::to_string(n);
};
auto sign = [&](int i){
return (pol.data[i]<0 || i==pol.data.size()-1) ? "" : "+";
};
// DO THIS ONLY AT HOME !!! --------v
for (size_t i = pol.data.size()-1; i < pol.data.size(); i-- ) {
o << sign(i) << pol.data[i] << power(i);
}
return o;
}
int main() {
Polynomial<double> p1{{0,1,2,-3,4}};
std::cout << p1 << "\n";
}
来说就是这样(我允许自己去掉你的代码并添加&#34; +&#34;):
std::vector
打印
4倍^ 4-3x ^ 3 + 2×^ 2 + 1×0 +
您不需要编写编译器可以为您编写的析构函数/构造函数。实际上它不需要任何构造函数,因为伪装只是一个裸0
。请注意,我试图保持简短,但不一定是干净的,例如我在任何严重的代码中都不会依赖unsigned overflow来获取循环条件,并且大量使用ternary并不能真正提高可读性。但是,我非常认真地使用lambdas。它们有助于保持本地化。如果它是std::vector<T>
;),我会留给你打印最后一个系数的内容。
要打印矩阵的行,您可以为template <typename T> struct Matrix {
using element_type = T;
using row_type = std::vector<element_type> ;
using data_type = std::vector<row_type> ;
Matrix(int rows, int cols, T const & init) :
data(data_type(rows,row_type(cols,init)))
{}
data_type data;
};
template <typename T>
std::ostream& operator<<(std::ostream& o,const std::vector<T>& v) {
for (auto& x : v) o << x << "\t";
return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o,const Matrix<T>& mat) {
for (const auto& row : mat.data) o << row << "\n";
return o;
}
提供重载,这将是以下内容:
int main() {
using Matrix = Matrix<Polynomial<double>>;
Polynomial<double> p1{{1,2}};
Matrix mp(2,3,p1);
std::cout << mp;
return 0;
}
现在打印矩阵就像这个
一样简单{{1}}
打印
2x + 1 2x + 1 2x + 1
2x + 1 2x + 1 2x + 1
另见:https://ideone.com/n00bqn (是想试图取笑我或者是什么?:)
答案 1 :(得分:1)
您希望使用Map[i][j]
打印Polynomial<double>
(operator<<
)的内容,但未定义作为参数Polynomial<double>
的运算符。所以你应该添加声明
template<class U>
friend std::ostream& operator<<(std::ostream&,const Polynomial<U>&);
在您的Polynomial
模板中,并在课堂外定义为
template<class T>
std::ostream& operator<<(std::ostream& out, const Polynomial<T>& poly)
{
poly.print();
return out;
}
然后你可以打电话
cout << Ma[i][j] << ", ";
在Matrix<T>::print
方法中。