如何检查向量中的两个元素是否按顺序排列?

时间:2017-06-01 08:37:51

标签: c++ vector sequence poker

说我有这个

vector<int>SequentialByOne{1, 2, 3, 4, 5} //would like to search each element
//in the vector and if it is sequential a boolean value returns true


vector<int>notSequential{1, 6, 5, 8, 9} // This would return false


vector<int>notSequentialButOrdered{1, 3, 6, 9, 20} // This would return false


// So far I can get it to return true if the next number
// is bigger than the previous but can't figure out
// how to check that the next number is only one bigger.

这是我正在为一个学校项目工作的扑克手电梯。我有一个有序的5个数字的向量,现在我需要搜索该向量来查找它们是否按照确切的顺序+1。

这是我到目前为止所做的事情

sort(hand.begin(), hand.end()); // This is the vector name

int a;
{
    for(int i = 0; i < 4; i++)
    {
        if(hand[i] < hand[i + 1] - 1)
            a++;
    }
}

bool has_straight
{
    if(a == 5)
        return true;
}

1 个答案:

答案 0 :(得分:2)

由于你已经对vector进行了排序,你可以检查(假设向量中没有重复),如果第一个和最后一个元素之间的差异是4:

hand.back() - hand.front() == 4