开发者

how to implement a queue of char array

开发者 https://www.devze.com 2023-01-28 13:41 出处:网络
I want to use a container of the STL to use it as a buffer, so I defined something like this: typedef queue<char*> CHARQUEUE;

I want to use a container of the STL to use it as a buffer, so I defined something like this:

typedef queue<char*> CHARQUEUE;

then

CHARQUEUE p;

then i did some operations like:

p.push("mouse");
p.push("horse");

but what I want is a queue of char array of 256 bytes in order to hold data. Can we define something like this:

typedef unsigned char newtype[256]
queue<newtype> newqueue;

I'm using an unsigned char in order to hold some data that I don't want to change. Just to put it in the 开发者_运维知识库buffer and retrieve it again.

Otherwise if anyone has another implementation I'll be thankful.


for your queue of raw data things, just define

typedef unsigned char Byte;
typedef std::vector<Byte> ByteVector;
typedef std::queue< ByteVector > DataQueue;

for your queue of character strings, the std::queue< std::string > that others have suggested is fine

by the way, ALL_UPPERCASE_NAMES are best reserved for macros

cheers & hth.,


Use

std::queue<std::string>

EDIT:

Some alternatives.

std::queue<std::vector<char>>
std::queue<std::array<char, 256>>


Arrays don't fulfill the requirement of stdlib containers (Assignable, etc) - use e.g. boost's array type (or the one from tr1/c++1x).


Use queue<string> CHARQUEUE;

Edit:

If you want to store just pointers to some external buffers, then use queue<const char*> CHARQUEUE;, but note that this won't copy the data.

If you want to make copies, use queue< vector<char> > CHARQUEUE;.


You could use queue<boost::shared_array<const char>> (see here) to do this, if you want the contents to be constant.

queue<boost::shared_array<const char>> queueArray;
const char mouse[] = "mouse";
boost::shared_array<const char> element(mouse);
queueArray.push(element);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号