C ++ vector or Queue to create large Qs in memory and speed

I create a large message queue and use only PUSH and POP, so it will be more efficient (vector or queue) to maintain large data at maximum speed

struct MQStruct { wchar_t *serviceName; int durability; int msgType; int msgHeader; wchar_t *msgId; wchar_t *payload; int payloadSize; int ttl; int priority; }MQStructObj; vector<MQStruct> MQvector; queue<MQStruct> MSQ; int SendMessage(wchar_t *serviceName, int durability, int msgType, int msgHeader, wchar_t *msgId, wchar_t *payload, int payloadSize, int ttl, int priority) { MQStructObj.serviceName=serviceName; MQStructObj.durability=durability; MQStructObj.msgType=msgType; MQStructObj.msgHeader=msgHeader; MQStructObj.msgId=msgId; MQStructObj.payload=payload; MQStructObj.payloadSize=payloadSize; MQStructObj.ttl=ttl; MQStructObj.priority=priority; //Which one is better (Vector or Queue) in term of memory, speed and why MSQ.push(MQStructObj); //OR MQvector.push_back(MQStructObj); return 0; } 
+6
source share
2 answers

With std::vector you cannot effectively simulate a queue - you can only PUSH and POP on one side of the container. If you need to implement a queue message, then use the queue for what it is intended.

+3
source

There is a lot of discussion regarding queue / vector / list on SO, you can search and reuse a resource. In short:

  • Use std::queue : if you need a quick insert and delete at the beginning and after its end;
  • Use std::vector if you need random access to elements
  • When there are frequent insertions and deletions from the middle of the sequence you need std::list

Since you only use push / pop for the container, std::queue is the path instead of std::vector .

You can get more details: http://en.cppreference.com/w/cpp/container

+13
source

All Articles