【发布时间】:2017-04-25 19:00:50
【问题描述】:
假设我有一些课程。
Class Example
{
int amount;
string name;
}
//constructors
...........
.....
int geAmount()
{
return amount;
}
然后我创建对象向量。
Vector <Example> vector1;
如何找到数量大于的所有元素 20(例如)? 我想打印它们..
例如我有 3 个对象。
Name=abc amount 5
Name=bcd amount 25
Name=dcg amount 45
所以我只想打印最后两个对象。
【问题讨论】:
-
向量中的每个元素都有一个大于、小于或等于 20 的
amount。 -
您对输出的期望是什么?一个仅包含数量大于 20 的元素的新向量,作为一组索引,...?
-
@NathanOliver,看起来 OP 想要查找大于
20(for example)的元素。我想这些很难找到。 -
std::copy_if是你的朋友。 -
@RSahu 或者
std::count_if。如果需要的话,索引会有些困难。
标签: c++