English 中文(简体)
C++: 如何高效获取/投入多个要素?
原标题:C++: Queue with efficient get/put of multiple elements?
  • 时间:2011-03-28 21:32:48
  •  标签:
  • c++
  • stl
  • queue

So, I feel like there should be a good, built-in solution for this in C++, but I m not sure what it is.

我需要一个能够高效地处理有色人种——允许不同大小的读写/仪式的问答(最好有读写能力,但我可以同步总结一下。

so, the interface looks like e.g.

//removes the first bytesToRead elements from the front of the queue and places them in array; returns the actual number of bytes dequeued
int dequeue(unsigned char *array, int bytesToRead) 
//Adds bytesToWrite elements from array to the end of the queue; does nothing and returns 0 if this would exceed the queue s max size
int enqueue(unsigned char *array, int bytesToWrite)

我可以不遇到太多困难就给我写信,但似乎像这一样,应该很容易地在大陆架外做事。

The best thing in the STL looks like it might be a stringbuf - I d have to pair calls to sgetc/pubseekoff by hand, but it seems like it would work.

我期望这样做,以取代目前的执行问题,即业绩问题;在执行工作中,正在读的是O(N)关于问题中的数据数量。 (这种执行非常冷静,每个频率都导致在座标点的其余数据的阵列本。)

Additional requirements (I can implement these in a wrapper if need be): -I need to be able to specify a maximum size of the buffer -Read operations should retrieve all available data if less data is available than was requested -Write operations should do nothing if the requested write would exceed the maximum size and return a failure indicator

So, my questions: 1) is stringbuf sufficient? Are the read/write operations O(1) relative to the amount of data in the buffer, assuming no resizing is necessary? (obviously, they d potentially be O(n) on the number of items requested.)

2) 是否还有其他几类人认为这还不够?

Thanks in advance!

问题回答

Hmmm. 你们试图证明:

class queue { 
      std::deque<unsigned char> data;
public:
    int enqueue(unsigned char *array, int bytesToWrite) { 
        data.insert(data.end(), array, array+bytesToWrite);
    }

    int dequeue(unsigned char *array, int bytesToRead) { 
        std::copy(data.begin(), data.begin()+bytesToRead, array);
        // or, for C++11: std::copy_n(data.begin(), bytesToRead, array);

        data.erase(data.begin(), data.begin()+bytesToRead);
    }
};

Sorry, I m not feeling quite ambitious enough at the moment to add locking and the return values you asked for, but neither should be terribly difficult. Instead of fiddling with your return values, however, I d change the interface to use iterators (or, if you really insist, a reference to a vector).

这保证与所插入/移走的内容数量一致。

如果你希望得到真正快速的执行,我就会有一个简单的循环缓冲执行,这意味着你能够以一或二份复印件(取决于你是否重新总结一下你的缓冲)。 这使你能够使用大张,在我的经验中,几乎总是通过许多要素来复制。

If the performance is less critical though I d go with Jerry s answer.

Could you use std::stringstream where you push into the queue with write and pop off the queue with read?

As suggested, std::stringstream is probably the easiest and best solution.

Another alternative is std::deque, it will give you the efficiency you want (constant amortised for all read / write from either end of the queue, and generally much less than O(N) of reallocation if the capacity is exhausted). The only drawback is that std::deque does not support pointer arithmetic (because all elements are not necessarily contiguous (in chunks)), so you won t be able to do a block read/write operation, you will have to iterate, as follows:

std::deque<unsigned char> buf;

int dequeue(unsigned char *array, int bytesToRead) {
  int result = std::min(bytesToRead, buf.size());
  std::copy(buf.begin(), buf.begin() + result, array);
  buf.erase(buf.begin(), buf.begin() + result);
  return result;
}; 

int enqueue(unsigned char *array, int bytesToWrite) {
  buf.insert(buf.end(), array, array + bytesToWrite);
  return bytesToWrite;
};

You should probably make the latter implementation check if the maximum capacity is reached and adjust the resulting value consequently.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签