2013-04-30 37 views
0

如果我有這樣的代碼打印出來繼承結構列表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; 
} 
+2

爲什麼你在這裏使用繼承?不要,只需使用代表列表的私有成員變量。另外,向我們展示您的(最小的!!!)代碼,否則我們無法真正幫助您。 – 2013-04-30 18:01:33

+0

顯示你有什麼。這可能是一個比沒有更好的起點。 – 2013-04-30 18:05:37

+0

好的。更新它。 – user2180833 2013-04-30 18:09:07

回答

0

首先,關於您的打印。您可以採取多種方式,最強大的是爲每種類型定義免費運營商。如:

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 

注:我做不正確隱藏成員,提供存取等那我離開你,但如何提供的總體思路輸出流操作符應該是顯而易見的。你可以顯著進一步提高輸出操作的`單位:

  • 不是1
  • 打印指數只打印爲0(NOx)的指數係數,
  • 不打印1係數任何指數大於0的項
  • 不打印任何東西條款與0係數。

我給你的這些任務。

+0

太棒了。現在一切正常。將編寫代碼作爲問題的更新。希望儘管我可以感謝你和菲利克斯,因爲我是來自兩個人。 – user2180833 2013-04-30 19:11:45

1

目前尚不清楚對我,你想做的事做。是否需要從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這可以實現更好,但我不知道你使用什麼編譯器。我到目前爲止沒有編譯它,只是把它砍掉了;所以很抱歉語法錯誤。

+0

Hei Felix 謝謝。但不幸的是,我不得不使用在類中繼承的STL列表。必須用它作爲乘法,加法,減法等操作。我也不這樣做,但我們必須這樣做來學習/理解這個概念。 – user2180833 2013-04-30 18:19:16