【发布时间】:2012-03-07 09:42:13
【问题描述】:
我遇到并且无法解决的问题是这样的。我有两个班级:
class1
{
private:
int identifier;
double value;
public:
setters,getters,etc...
}
class2
{
private:
vector<class1> objects;
vector<int> some_value;
vector<double> other_value;
...
}
问题是我需要通过 class1 对象中的标识符(来自 class2 的成员函数)来搜索第二类对象中的对象向量。我试过类似的东西:
int getObj(const int &ident, double &returnedValue, double &returnedOther_value)
{
int p;
p = find(objects.begin()->getIdentifier(),objects.end()->getIdentifier(),ident);
..
.. 然后我希望找到一种方法从两个类的对应(非 const)成员变量 value 和 other_value 的找到的迭代器值中返回,但到目前为止的代码无法编译,因为我可能做的搜索都错了。有没有办法可以使用 find(或任何其他算法)来做到这一点,还是应该坚持我以前的工作实现而不使用算法?
【问题讨论】:
-
您不需要将
ints 作为常量引用传递。它们是按值传递的(因此您不能更改原始值),并且按值传递 int 没有任何开销。
标签: class stl vector find member