[英]Comparing the private member variables of class objects stored in a vector - C++
So I have created a program which can read a .dat file of about 20 lines containing info about different atoms (name, symbol, mass etc) and added them all to a vector of class type I made called Atom. 因此,我创建了一个程序,该程序可以读取大约20行的.dat文件,其中包含有关不同原子(名称,符号,质量等)的信息,并将它们全部添加到我制作的称为Atom的类类型的向量中。
How would I write a function to find the atom with the highest mass? 我将如何编写函数以找到质量最高的原子?
Here is my class: 这是我的课:
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);
};
and the information is added to a vector with the following statements 并使用以下语句将信息添加到向量中
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);
}
I want to be able to write a function which finds which atom has the highest mass, I've tried using a max_element function but I keep getting errors. 我希望能够编写一个查找质量最高的原子的函数,我尝试使用max_element函数,但始终出错。 Is there a quick way of comparing the member variables of class objects stored in a vector?
有没有一种比较存储在向量中的类对象的成员变量的快速方法?
I'm currently using C++ 98 as it is what my course requires. 我目前正在使用C ++ 98,因为这是我的课程要求。
Thanks 谢谢
I don't know what you did wrong with std::max_element
, as you haven't provided what you've tried with it. 我不知道您对
std::max_element
做了什么错,因为您没有提供尝试过的内容。
struct CompareAtomMass
{
bool operator()(const Atom& lhs, const Atom& rhs) {
return lhs.getMass() < rhs.getMass();
}
};
and then: 接着:
vector <Atom> periodic;
Atom max_atom = *max_element(periodic.begin(), periodic.end(), CompareAtomMax());
struct CompareAtomMass
is called a function object. struct CompareAtomMass
称为函数对象。 It's a class with operator()
overloaded to return a bool
. 这是一个带有
operator()
重载以返回bool
。 std::max_element
requires just such a function object to spit out the max element as it needs a way to compare your Atom
s. std::max_element
只需要一个这样的函数对象就可以吐出max元素,因为它需要一种比较Atom
的方法。
EDIT: You should mark your getter functions as const
since they don't change the inner state of the class. 编辑:您应该将您的getter函数标记为
const
因为它们不会更改类的内部状态。
string getElement() const;
string getSymbol() const;
float getNumber() const;
float getMass() const;
This will allow you to call them from a const
object of type Atom
just as the above function object requires ( const Atom&
). 就像上面的函数对象需要的那样(
const Atom&
),这将允许您从Atom
类型的const
对象中调用它们。
Variation of DeiDeis answer: If you only do this at one place, and don't feel a need to keep a CompareAtomMass function class, you can use a lambda: DeiDeis答案的一种变化:如果只在一个地方执行此操作,并且不需要保留CompareAtomMass函数类,则可以使用lambda:
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 ;
}
in C++14 and later you can also use auto
in lambdas: 在C ++ 14和更高版本中,您还可以在lambdas中使用
auto
:
const auto maxIt = max_element(periodic.begin(), periodic.end(),
[](const auto& lhs, const auto& rhs) {
return lhs.getMass() < rhs.getMass();
));
It is a good idea to make your member functions const
. 使成员函数
const
是一个好主意。 This will allow this code. 这将允许此代码。 Otherwise just remove all
const
from my code. 否则,只需从我的代码中删除所有
const
。 In case your vector is empty, you will get null pointer. 如果向量为空,则将获得空指针。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.