【发布时间】:2022-02-02 03:40:45
【问题描述】:
所以我有一个Card 类,其中有_suit 和_rank 成员:
class Card {
private:
char _suit;
int _rank;
public:
Card(char, int); //Constructor
char suit() const; //Returns the _suit value
int rank() const; //Returns the _rank value
}
我还有一个矢量vector<Card> Deck。我想检查的是Deck 是否有Cards 具有相同的_rank 值。我尝试过使用std::adjecent_find,但这仅适用于原始数据类型(?)。当然可以使用嵌套的 for 循环,但有没有更有效的替代方案?
【问题讨论】:
-
"我尝试过使用
std::adjecent_find,但这仅适用于原始数据类型(?)" - 实际上,它被重载以允许您指定自定义谓词进行比较元素。 -
您的意图是只找到连续的等效值,还是试图找到满足您条件的任何?
std::adjacent_find用于查找两个比较相等的连续元素(或BinaryPredicate返回true)。