Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STD library iterator that never ends?

Is there a string buffer class that provides an iterator that allocates a new buffer if necessary on incrementing (*++itr = 'x'), or am I stuck with pre-allocating buffers first?

like image 900
Qix - MONICA WAS MISTREATED Avatar asked Jun 16 '14 02:06

Qix - MONICA WAS MISTREATED


2 Answers

There is something called std::back_inserter(), which calls push_back() every time you assign to it. Some example code:

int main() {
    string s = "abc";
    auto it = std::back_inserter(s);
    it = 'd';
    cout << s << endl;
    return 0;
}

Will print out: abcd

like image 184
yizzlez Avatar answered Oct 06 '22 00:10

yizzlez


You can use std::ostringstream with std::ostream_inserter, something like this (untested):

std:::ostringstream stream;
auto itr = ostream_inserter<char>(stream);
like image 21
John Zwinck Avatar answered Oct 06 '22 00:10

John Zwinck