输出继承的结构化列表
Print out inherited structured list c++
如果我有这样的代码
struct Unit{
int coef; //coefficient
int exp; //exponent
};
class Func: private std::list<Unit>{
public:
Func();
friend std::ostream& operator<<(std::ostream &, Func);
};
怎么打印出来?我试着从这里使用概念:http://www.cplusplus.com/forum/beginner/5074/但是没有成功:
ostream& operator<<(ostream &output, Func &pol)
{
list<Unit>::iterator i;
for( i = pol.begin(); i != pol.end(); ++i)
{
Unit test = *i;
output << test.coef << " ";
}
return output;
}
初始化正确吗?
Func::Func()
{
Unit test;
test.coef = 0;
test.exp = 0;
Func::push_back(test);
}
抱歉。这是关于继承的新知识。虽然这并不难,当它是关于我自己的类。
更新代码:
struct Unit{
int coef; //coefficient
int exp; //exponent
Unit():coef(0),exp(0){}
};
class Func : public std::list<Unit>{
public:
Func();
friend std::ostream& operator<<(std::ostream &, const Func &);
};
Func::Func()
{
Unit test;
Func::push_back(test);
}
ostream& operator <<(std::ostream &output, const Func& pol)
{
for (list<Unit>::const_iterator i = pol.begin(); i != pol.end(); output << i->coef << " " << i->exp << " ", ++i);
return output;
}
我不清楚你想做什么。这是从STL列表继承的要求吗?我不会这么做的。
但这至少是一个解决方案。
struct Unit{
int coef; //coefficient
int exp; //exponent
};
std::ostream& operator<<(std::ostream &os, Unit const& v)
{
os << v.coef << " " << v.exp << std::endl;
return os;
}
int main()
{
std::list<Unit> myList;
Unit element;
element.coef = 0;
element.exp = 0;
myList.push_back(element);
std::ostringstream os;
for (std::list<Unit>::const_iterator it = myList.begin(); it != myList.end(); ++it)
{
os << *it;
}
std::cout << os.str() << std::endl;
}
在c++ 11中,这可以实现得更好,但我不知道你使用的是什么编译器。到目前为止,我还没有编译它,只是把它砍下来;很抱歉语法错误
首先,关于打印。您可以通过多种方式实现,最可靠的方法是为每种类型定义自由操作符。如:
struct Unit{
int coef; //coefficient
int exp; //exponent
};
std::ostream& operator <<(std::ostream& os, const Unit& unit)
{
os << unit.coef << "X^" << unit.exp;
return os;
}
函数稍微复杂一点。最好将列表用作成员变量,并为该类提供流插入操作符。如:
#include <iostream>
#include <iterator>
#include <list>
#include <cstdlib>
struct Unit
{
int coef; //coefficient
int exp; //exponent
Unit(int coef, int exp=0)
: coef(coef), exp(exp)
{}
friend std::ostream& operator <<(std::ostream&, const Unit&);
};
std::ostream& operator <<(std::ostream& os, const Unit& unit)
{
os << unit.coef << "X^" << unit.exp;
return os;
}
class Func
{
public:
std::list<Unit> units;
friend std::ostream& operator <<(std::ostream&, const Func&);
};
std::ostream& operator <<(std::ostream& os, const Func& func)
{
std::copy(func.units.begin(), func.units.end(),
std::ostream_iterator<Unit>(os, " "));
return os;
}
int main()
{
Func func;
func.units.push_back(Unit(3, 2));
func.units.push_back(Unit(2, 1));
func.units.push_back(Unit(1, 0));
std::cout << func << endl;
return EXIT_SUCCESS;
}
3X^2 2X^1 1X^0
注意:I did NOT正确地隐藏成员,提供访问器等。我把这个问题留给您,但是关于如何提供输出流操作符的一般思路应该是显而易见的。您可以通过以下方式显著地进一步增强' Unit的输出操作符:
- 不打印
1
指数 - 仅打印
0
指数的系数(无X), - 不打印指数大于
0
的1
系数 - 对于
0
系数项不打印任何内容。
我把这些任务留给你。
相关文章:
- 复制列表初始化的隐式转换的等级是多少
- 在基于范围的for循环中使用结构化绑定声明
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 使用结构化绑定'Reflection'
- 为什么结构化绑定不使用"auto&"返回对结构成员的引用,而是返回成员本身
- C++11 中的混合列表初始化
- 为什么 boost::comb 对结构化绑定的支持缺少结构化绑定机制对 boost::tuples::cons 的适应?
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 我可以列表初始化 std::vector 并完美转发元素吗?
- 使用初始值设定项列表初始化结构,对于 std::array 失败
- 导致IAR ARM中出现错误的成员结构位字段元素的Initializer列表初始化
- 为什么 lambda 表达式的捕获列表无法使用结构化绑定分解
- 是使用特定于编译器的变量对新结构{}进行列表初始化
- 使用构造函数初始化列表初始化结构时出现问题
- 正在使用大括号括起来的初始值设定项列表初始化结构向量
- 如果结构继承,则无法使用初始值设定项列表初始化结构?
- 冒泡排序在结构化列表中,由csv文件填充
- 用大括号括起来的初始化列表初始化结构体时出错
- 输出继承的结构化列表