我正在查看 Qt 特定的 C++ solution对于典型的生产者/消费者问题。这是生产者的代码:
class Producer : public QThread
{
public:
void run()
{
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
for (int i = 0; i < DataSize; ++i) {
freeBytes.acquire(); // (1)
buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; // (2)
usedBytes.release();
}
}
};
我无法理解 for
循环中的第二行,即。 "ACGT"[*]
语法。它具体做什么?这是 Qt 特定的还是我不知道的 C++ 语法?
PS:完整源代码here
最佳答案
它生成一个随机字符:A、C、G、T。
Literal "ACGT"
是 char const [5]
类型的数组,然后 [(int)qrand() % 4]
是0 到 3 范围内的随机索引,包括。
关于c++ - 这是什么语法 : "ACGT"[(int)qrand() % 4],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624999/