私はキュー(FIFO)とQtの新機能です。 Qtでunsigned char配列のQueueを作成したいと思います。どうやってするの?あなたはQtのAPIを使用する場合は、あなたがQQueueクラスを使用することができますQtで符号なし文字配列のキューを作成するには?
unsigned char buffer[1024];
私はキュー(FIFO)とQtの新機能です。 Qtでunsigned char配列のQueueを作成したいと思います。どうやってするの?あなたはQtのAPIを使用する場合は、あなたがQQueueクラスを使用することができますQtで符号なし文字配列のキューを作成するには?
unsigned char buffer[1024];
を助けてください -
QQueue<unsigned char> queue;
queue.enqueue(65);
queue.enqueue(66);
queue.enqueue(67);
while (!queue.isEmpty())
cout << queue.dequeue() << endl;
自分でキューを構築したい場合は、私はあなたが宣言することができますねこのようなQueue
クラス - あなたがunsigned char
配列のキューを維持したい場合は
class Queue
{
private:
enum{SIZE=1024, EMPTY=0};
unsigned char buffer[SIZE];
int readHead, writeHead;
public:
Queue()
{
readHead = writeHead = EMPTY;
}
void push(unsigned char data);
unsigned char pop();
unsigned char peek();
bool isEmpty();
};
void Queue::push(unsigned char data)
{
if((readHead - writeHead) >= SIZE)
{
// You should handle Queue overflow the way you want here.
return;
}
buffer[writeHead++ % SIZE] = data;
}
unsigned char Queue::pop()
{
unsigned char item = peek();
readHead++;
return item;
}
unsigned char Queue::peek()
{
if(isEmpty())
{
// You should handle Queue underflow the way you want here.
return;
}
return buffer[readHead % SIZE];
}
bool Queue::isEmpty()
{
return (readHead == writeHead);
}
、その後、あなたがのキューを維持する必要がありますのポインタ -
QQueue<unsigned char *> queue;
unsigned char *array1 = new unsigned char[10]; // array of 10 items
array1[0] = 65;
array1[1] = 66;
queue.enqueue(array1);
unsigned char *array2 = new unsigned char[20]; // an array of 20 items
queue.enqueue(array2);
unsigned char *arr = queue.dequeue();
qDebug() << arr[0] << ", " << arr[1];
注は:あなたがこのキューで行われた後、あなたは、メモリのクリーンアップの世話をする必要があります。 IMHO、あなたはこのタイプのデザインを避けることをお勧めします。
自分でキューを構築したいのですか、キューにQt APIを使用しますか? –
私はQt APIを使用するのが好きです – indira