What is this syntax: "ACGT" [(int) qrand ()% 4]

I am considering a Qt-specific C ++ solution for a typical producer / consumer problem. Here is the code for the manufacturer:

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();
        }
    }
};

I cannot understand the second line in the loop for, namely. "ACGT"[*]. What is he doing exactly? Is this Qt specific or is it a C ++ syntax that I don't know about?

PS: Full source code here

+4
source share
2 answers

It generates a random characteristic from: A, C, G, T.

Literal "ACGT"is an array of type char const [5], then it [(int)qrand() % 4]is a random index ranging from 0 to 3, including.

+5
source

"ACGT" [*] . ?

qrand() % 4 - 0 3. "ACGT ". , A, C, G T.

+4

Source: https://habr.com/ru/post/1540330/


All Articles