2017-01-18 6 views
0

私は、異なる原子(名前、記号、質量など)に関する情報を含む約20行の.datファイルを読み込んで追加できるプログラムを作成しました私はAtomと呼ばれるクラス型のベクトルにすべてを渡しました。ベクトルに格納されたクラスオブジェクトのプライベートメンバ変数の比較 - C++

質量が最大の原子を見つける関数を書くにはどうすればよいですか?私はアトムを見つける関数を記述することができるようにしたい

class Atom 
{ 
    string element, symbol; 
    float number; 
    float mass; 
public: 
    Atom(string e, string s, float n, float m){ 
     element = e; symbol = s; number = n; mass = m; 
    } 
    string getElement(); 
    string getSymbol(); 
    float getNumber(); 
    float getMass(); 
    float ratio(); 
    friend ostream& operator<<(ostream& os, Atom c); 
}; 

情報は、次の文

ifstream fin("atoms.dat"); 

    string E, S; 
    float M, N; 

    vector <Atom> periodic; 

    while(!fin.eof()){ 
     fin >> E >> S >> M >> N; 
     Atom Atom(E, S, M, N); 
     periodic.push_back(Atom); 
    } 

をベクトルに追加されます。ここでは

は私のクラスであります最高の質量を持っている、私はmax_element関数を使用してみましたが、私はエラーを取得し続けます。ベクトルに格納されたクラスオブジェクトのメンバ変数を簡単に比較する方法はありますか?

私は現在、コースに必要なC++ 98を使用しています。

おかげ

+0

ここでは、http:// fusharblogという素晴らしいブログです。com/3-ways-to-define-comparison-functions-in-cpp/ – NathanOliver

+0

max_elementを使った方法があなたの質問の鍵です。なぜあなたはそれをそれから離しましたか? – DeiDei

+0

「私はmax_elementを使ってみました。」次に、あなたが書いたことを示して、正しい使い方を説明することができます。そうでなければ、ちょうど見て[ここ](http://en.cppreference.com/w/cpp/algorithm/max_element)。 – kebs

答えて

2

あなたはそれを試してみたものを提供していないように私は、あなたがstd::max_elementと間違って何をしたか分かりません。

struct CompareAtomMass 
{ 
    bool operator()(const Atom& lhs, const Atom& rhs) { 
     return lhs.getMass() < rhs.getMass(); 
    } 
}; 

、その後:

vector <Atom> periodic; 
Atom max_atom = *max_element(periodic.begin(), periodic.end(), CompareAtomMax()); 

struct CompareAtomMassは、関数オブジェクトと呼ばれています。それはboolを返すようにオーバーロードされたoperator()のクラスです。 std::max_elementは、あなたのAtomを比較する方法が必要なので、max要素を吐き出すのにこのような関数オブジェクトが必要です。

EDIT: 彼らはクラスの内部状態を変更しないので、あなたはconstとしてあなたゲッター機能をマークする必要があります。

string getElement() const; 
string getSymbol() const; 
float getNumber() const; 
float getMass() const; 

これはあなただけで上記の関数オブジェクトとしてのタイプAtomconstオブジェクトからそれらを呼び出すことができますが(const Atom&)が必要です。 DeiDeisの答えの

+0

std :: max_elementは参照ではなくイテレータを返します – RyanP

+0

@ RyanPありがとう。一定。 – DeiDei

+0

私は今このエラーが発生しています: 'float Atom :: getMass()'の 'this'引数として 'const Atom'を渡すと修飾子を破棄[-fpermissive] return lhs.getMass() Daniel

0

バリエーション:あなたは一箇所のみこれを行うと、CompareAtomMass機能クラスを維持する必要性を感じていない場合は、ラムダを使用することができます:C++ 14で

const auto maxIt = max_element(periodic.begin(), periodic.end(), 
    [](const Atom& lhs, const Atom& rhs) { 
    return lhs.getMass() < rhs.getMass(); 
)); 
if(maxIt != periodic.end()){ 
    // use *maxIt ; 
} 

そして後であなたもラムダでautoを使用することができます。

const auto maxIt = max_element(periodic.begin(), periodic.end(), 
    [](const auto& lhs, const auto& rhs) { 
    return lhs.getMass() < rhs.getMass(); 
)); 
+0

..私は何を欠場したのですか? –

0

あなたのメンバ関数constを作るためには良いアイデアです。 これにより、このコードが許可されます。それ以外の場合は、すべて私のコードからconstを削除してください。 あなたのベクトルが空の場合、あなたはnullポインタを取得します。

struct AtomMassComparator 
{ 
    bool operator()(const Atom& lhs, const Atom& rhs) 
    { 
     return lhs.getMass() < rhs.getMass(); 
    } 
}; 

const Atom* getAtomWithHighestMass(const vector<Atom>& v) 
{ 
    vector<Atom>::const_iterator it = max_element(
     v.begin(), v.end(), AtomMassComparator()); 
    return v.end() == it ? 0 : &*it; 
} 
関連する問題