FIFO container type - Which STL container is most suitable and why?

I was recently instructed to implement a buffer that will be used as temporary storage for the logging class. The journal class itself is a singleton, and the listener-observer pattern is used. You can expect thousands of posts to be recorded in this class.

Now the problem is here:

We have a trace tracking option that is used for degradation purposes. When this option is enabled, the number of messages / seconds increases exponentially. The release code tracking protocol is disabled, however, a buffer that can store a fixed number of messages, for example. 10000 is flushed to the IF log , an error occurs so that developers can determine the root of the problem.

If the buffer is full, the oldest message is deleted to free space for the newest message.

void Log::storeToBuffer(const LogType_E & type_in, const LogDomain_E & domain_in,const int & id_in, const char * msg_in)
{
    if(this->myEnableTraceBuffer)
    {
        if(static_cast<std::list<Message> * >(this->myRingBuffer)->size() < this->myRingBufferMaxSize)
        {
            static_cast<std::list<Message> * >(this->myRingBuffer)->push_back(Message(type_in, domain_in, id_in, msg_in));
        }
        else
        {
            //buffer full so remove oldest element and add new
            if(static_cast<std::list<Message> * >(this->myRingBuffer)->size() > 0) static_cast<std::list<Message> * >(this->myRingBuffer)->pop_front();
            static_cast<std::list<Message> * >(this->myRingBuffer)->push_back(Message(type_in, domain_in, id_in, msg_in));
        }
    }
}

I implemented this with std :: list, very simply using push_back / pop_front to use constant delete / insert time. (do not ask for the deprivation of emptiness, not for my decision).

, , ? , , /, 0. , - 1, 0, , .

, STL ?

, . , .

+5
4

- std::deque?:)
push/pop . , FIFO.

, , std::queue, .

+2

std::deque , , . std::queue, - , push, pop, .

+3

std:: list push_back/pop_front , , , , .

, std::vector, , , . std::vector, , , boost, stl-: http://www.boost.org/doc/libs/1_47_0/libs/circular_buffer/doc/circular_buffer.html

+2

, , , / , ++ - std::deque. std::vector (, , , ), . , , .

, , , reserve, . , , 10000 . , . , std::list 10000 . 10000 , , std::list .

In fact, you can also use std::queue, which by default uses std::dequeas the base type of the container and provides only a few functions needed for the FIFO queue. But in this case, you will not be able to access individual elements or iterate over all the elements that may be required for your output logic.

+2
source

All Articles