C++ 是否有附加到字符串的getline(istream、string、delim)?

C++ 是否有附加到字符串的getline(istream、string、delim)?,c++,boost,std,C++,Boost,Std,这将是伟大的,以避免复制。std或boost中有什么可以做到这一点的吗?您可以改用此成员函数(): 读取数据,然后将该数据附加到字符串 例如,您可以将此功能包装在自己定义的函数中,如下所示: std::istream & getline(std::istream &in, std::string & str, char delim) { char buf[1024]; in.getline(buf, 1024, delim); str.append

这将是伟大的,以避免复制。std或boost中有什么可以做到这一点的吗?

您可以改用此成员函数():

读取数据,然后将该数据附加到字符串

例如,您可以将此功能包装在自己定义的函数中,如下所示:

std::istream & getline(std::istream &in, std::string & str, char delim)
{
    char buf[1024];
    in.getline(buf, 1024, delim);
    str.append(buf, in.gcount());
    return in;
}

std::string s = "initial string";

getline(std::cin, s, '\n'); //should append to s

由于没有现有的解决方案,因此我提出了以下建议:

istream& appendline(istream& is, string& str, char delim = '\n')
{
    size_t size = str.size();
    size_t capacity = str.capacity();
    streamsize spaceRemaining = capacity - size;

    if (spaceRemaining == 0)
    {
        capacity = max(static_cast<size_t>(8), capacity * 2);
        spaceRemaining = capacity - size;
    }

    // give getline access to all of capacity
    str.resize(capacity);

    // get until delim or spaceRemaining is filled
    is.getline(&str[size], spaceRemaining, delim);

    // gcount includes the delimiter but not the null terminator
    size_t newSize = size + is.gcount();

    // is failbit set?
    if (!is)
    {
        // if string ran out of space, expand and retry
        if (is.gcount()+1 == spaceRemaining)
        {
            is.clear();
            str.resize(newSize);
            str.reserve(capacity * 2);
            return appendline(is, str, delim);
        }
    }
    else if (!is.eof())
        --newSize;

    // resize string to fit its contents
    str.resize(newSize);
    return is;
}
istream&appendline(istream&is,string&str,char-delim='\n')
{
size_t size=str.size();
容量大小=结构容量();
streamsize SpaceerMining=容量-大小;
如果(间隔垫圈==0)
{
容量=最大值(静态容量(8),容量*2);
间隔器内衬=容量-尺寸;
}
//允许getline访问所有容量
str.resize(容量);
//直到熟食或间隔剂填满为止
is.getline(&str[size],spaceRemaining,delim);
//gcount包括分隔符,但不包括空终止符
size_t newSize=size+is.gcount();
//故障位设置好了吗?
如果(!is)
{
//如果字符串空间不足,请展开并重试
如果(is.gcount()+1==SpaceerMaining)
{
is.clear();
str.resize(newSize);
str.reserve(容量*2);
返回附录行(is、str、delim);
}
}
如果(!is.eof())
--新闻化;
//调整字符串大小以适合其内容
str.resize(newSize);
回报是;
}

只需使用全局std::getline而不是member方法

stringstream s;
s << "line1\nline2";
string str;
while(std::getline(s, str)) cout << str;
strings;

您希望避免复制,并且希望附加到另一个字符串吗?这是如何工作的?我正在从一个istream读取数据,我需要在分隔符之间运行一些逻辑。与其保留一个外部缓冲区,并在临时缓冲区上使用getline,然后将临时缓冲区复制到外部缓冲区,我宁愿附加到外部缓冲区。哦,我明白了。这似乎完全合理。如果您不反对使用stringstreams,您可能会在其中的某个地方找到一个解决方案。嗯,您的意思是将字符串作为char*传入,比如
&buffer.back()
?我认为这样可以很好地工作,但从技术上讲,这不是不正确的吗?也就是说,如果字符串实现不是连续的?它也不会自动在字符串中分配新空间,但我可以容忍。类似于
istream.getline(&buffer.back(),buffer.capacity()-buffer.length(),delim)
?@MattChambers:我给出了一个例子。现在就看。好吧,那绝对不是我想要的。我想避免抄袭!您的示例从buf复制到str。我想直接读入str。实际上,它是从istream缓冲区复制到字符串,但一个副本比两个好。:)@马特钱伯斯:你已经知道输入的大小了吗?如果没有,那么您如何知道需要多少内存来存储整个输入?在任何情况下,都无法避免复制数据。
istream& appendline(istream& is, string& str, char delim = '\n')
{
    size_t size = str.size();
    size_t capacity = str.capacity();
    streamsize spaceRemaining = capacity - size;

    if (spaceRemaining == 0)
    {
        capacity = max(static_cast<size_t>(8), capacity * 2);
        spaceRemaining = capacity - size;
    }

    // give getline access to all of capacity
    str.resize(capacity);

    // get until delim or spaceRemaining is filled
    is.getline(&str[size], spaceRemaining, delim);

    // gcount includes the delimiter but not the null terminator
    size_t newSize = size + is.gcount();

    // is failbit set?
    if (!is)
    {
        // if string ran out of space, expand and retry
        if (is.gcount()+1 == spaceRemaining)
        {
            is.clear();
            str.resize(newSize);
            str.reserve(capacity * 2);
            return appendline(is, str, delim);
        }
    }
    else if (!is.eof())
        --newSize;

    // resize string to fit its contents
    str.resize(newSize);
    return is;
}
stringstream s;
s << "line1\nline2";
string str;
while(std::getline(s, str)) cout << str;