2017-08-08 8 views
0

とbasic_ostream :: basic_ostream 私がしようとしてきたが、私はできませんのstd ::私は、STDのパラメータを挿入することができますどのように知っていただきたいと思いパラメータ

私はこれを選択し、パラメータを挿入する必要がありますアリスタからの値私は、パラメータを使用すると、標準にカスタム行動状態を追加することができる方法がありますが条件

template <typename charT> 
friend std::basic_ostream<charT> &operator << (
    std::basic_ostream<charT>& out, Familia &familia 
    ) { 
    out << "\t Relaciones\n"; 
    for (Vertice<cedula, relacion> &vertice : familia) { 
     int per = vertice.getFuente(); 
     for (Arista<cedula, relacion> &arista : vertice) { 
      out << per << "->"; 
      out << arista.getDestino() << " es" << " " << arista.getValor() << "\n"; 
     } 
    } 
    return out; 
} 
+0

あなたは何を求めているのかはっきりしません。あなたはちょうど良いostreamに挿入されているようです。間違いはありますか?正確な問題は何ですか? – bolov

+2

おそらく、必要な情報のみを含む 'Familia'オブジェクトのインスタンスを返すフィルタ関数を作成しますか?または、[標準マニピュレータ](http://en.cppreference.com/w/cpp/io/manip)のような['setw'](http://en.cppreference.com/w/cpp)/io/manip/setw)が動作し、それに基づいてソリューションをモデル化しますか? –

+0

ああ、今私はそれを得る。ここでは、これらを見て:https://stackoverflow.com/questions/799599/c-custom-stream-manipulator-that-c​​hanges-next-item-on-stream、https://stackoverflow.com/questions/ 15053753/custom-stream-classのためのマニピュレータの作成、https://stackoverflow.com/questions/1328568/custom-stream-manipulator-for-class – bolov

答えて

2

場合はそれだけであるので、次のステップは簡単で挿入したら を印刷したいですストリームマニピュレータを介したストリームクラス。

しかし、私は個人的にはこれがあまりにもオーバーヘッドであると感じています。それは当然のFamiliaの友人クラスでなければならないであろう

class FormattedFamilia { 
    Familia const& _to_print; 
    int _parameter; 
public: 
    FormattedFamilia(int parameter, Familia const& to_print) 
    : _parameter(parameter), _to_print(to_print) 
    {} 

    template <typename charT> 
    friend std::basic_ostream<charT> &operator << (
    std::basic_ostream<charT>& out, FormattedFamilia const & ff 
) { 
    if(_parameter > 0) { 
     // do printing using out. 
    } 
    } 
}; 

:私は何を示唆することで、パラメータとFamilia参照を受け入れ、その後、印刷に進み、新しい型を定義することです。それを使用すると、次のように簡単になります。

cout << FormattedFamilia(7, familia); 
関連する問題