문제

I've been working on a basic log file class which truncates by 20% once it reaches as set size limit (e.g. 10Mb), I decided to store the file into a stringstream buffer and then store it log by log into a std::deque (used a std::deque instead of std::vector so I could easily pop from the top of the structure and not have to sort etc) the loading and storing is shown below:

    std::deque<std::string> vLogs;
    std::stringstream ssBuffer;
    std::string sLog;

    ssBuffer << in.rdbuf();
    in.close();

    while(getline(ssBuffer,sLog))
        vLogs.push_back(sLog);

the log file is designed so it truncates when it reaches a set size (e.g. 10Mb) so what I really want to do is check how large the std::string's are within vLogs - which I can do in the following way:

    int nSize = 0;
    for(auto it = vLogs.begin(); it != vLogs.end(); ++it)
        nSize += it->size();

    do{
        nSize -= vLogs.front().size();
        vLogs.pop_front();
    }while(nSize > (MAX_SIZE * 0.8));

The question I'm asking is whether there is a more efficient way on getting the actual size taken up by the std::strings instead of doing it manually like I am now.

thanks - any more information just ask.

도움이 되었습니까?

해결책

Do your own class that will have a std::deque and a memory size counter and monitor insertions (e.g., add size of memory inserted to the counter) and extractions (e.g., subtract size of memory extracted from the counter).

다른 팁

Right now, you're adding up the sizes many many times. This isn't necessary.

You can either

  • add up all the sizes, then adjust the total down each time you remove an item

or

  • add up the sizes in reverse starting from the tail, and once you reach the threshold, remove all items from that point to the beginning
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top