输出继承的结构化列表

Print out inherited structured list c++

本文关键字:列表 结构化 继承 输出      更新时间:2023-10-16

如果我有这样的代码

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),
  • 不打印指数大于01系数
  • 对于0系数项不打印任何内容。

我把这些任务留给你。