c++ - 如何检查 vector 中的两个元素是否按一个顺序排列?

标签 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 个数字的 vector ,现在我需要搜索该 vector 以查找它们是否按精确顺序 +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;
}

最佳答案

因为您已经对 vector 进行了排序,您可以检查(假设 vector 中没有重复项)第一个元素和最后一个元素之间的差异是否为 4:

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

关于c++ - 如何检查 vector 中的两个元素是否按一个顺序排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302532/

相关文章:

javascript - Emscripten Uncaught RangeError : Source is too large, 多个 Float32Arrays

c++ - Visual C++ - 找到一个或多个多次定义的符号

c++ - 为什么迭代器没有在这段代码中初始化

c++ - 关于二维 vector 的一些问题,C++

multithreading - oracle序列上的多线程。线程是否有可能从其他线程获得相同的序列值?

c++ - C++11 中的 char16_t 和 char32_t 类型

c++ - 遍历 Vector 中结构的成员时出错

r - 在 R 中生成具有交替增量的序列?

hadoop - 如何读取 Hadoop Sequentil 文件作为 Hadoop 作业的输入?

c++ - Qt 生成 Key Press(keyboard) to the system(OF) 一种跨平台的方式