C++ 为字符缓冲区实现istream get?
我想实现下面的代码,它有一个字符缓冲区,我尝试移植的代码将这个字符缓冲区放入一个流中,然后执行如下的getC++ 为字符缓冲区实现istream get?,c++,stream,C++,Stream,我想实现下面的代码,它有一个字符缓冲区,我尝试移植的代码将这个字符缓冲区放入一个流中,然后执行如下的get char *buffer; //this is initialized int bufferSize; //this is initlized std::istringstream inputStream (std::string(buffer, bufferSize)); int getVal = inputStream.get(); EDIT:上述代码是最优的,其中对于ge
char *buffer; //this is initialized
int bufferSize; //this is initlized
std::istringstream inputStream (std::string(buffer, bufferSize));
int getVal = inputStream.get();
EDIT:上述代码是最优的,其中对于getVal,您将整个缓冲区复制到流中,然后对流执行get
如何从缓冲区本身获取getVal值。我不认为它是最佳的,因为构造std::string可能会导致整个缓冲区上的副本。然而,istringstream的使用看起来不错 要直接从缓冲区获取,可以执行以下操作:
int bufferPos = 0;
char getFromBuffer ()
{
if (bufferPos < bufferSize)
{
return buffer[bufferPos++];
}
else
{
return 0;
}
}
int-bufferPos=0;
char getFromBuffer()
{
if(bufferPos
不过,您可能希望在此基础上提供更好的界面。也许有更好的方法可以使用char*构造istringstream,但我在快速浏览文档时没有看到它。我不认为这是最佳的,因为构造std::string可能会导致整个缓冲区上出现副本。然而,istringstream的使用看起来不错 要直接从缓冲区获取,可以执行以下操作:
int bufferPos = 0;
char getFromBuffer ()
{
if (bufferPos < bufferSize)
{
return buffer[bufferPos++];
}
else
{
return 0;
}
}
int-bufferPos=0;
char getFromBuffer()
{
if(bufferPos
不过,您可能希望在此基础上提供更好的界面。也许有更好的方法可以用char*构造istringstream,但我在快速浏览文档时没有看到它。次要挑剔:EOF上不应该get()return-1?次要挑剔:EOF上不应该get()return-1?