Can std :: back_insert_iterator be used for std :: ostream?
As per the title question.
I guess the answer is "No, because the object std::back_insert_iterator
calls push_back()
in the container."
If the answer is really no, then is there some template class iterator I can use in my template function to add to std::string
, append to, std::vector<char>
and write to std::ostream
?
+3
source to share
1 answer
This is what std::ostream_iterator
for:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
char c[] = { 'a', 'b', 'c', 'd' };
std::vector<char> v;
std::string s;
std::copy(c, c+4, std::back_inserter(v));
std::copy(c, c+4, std::back_inserter(s));
std::copy(c, c+4, std::ostream_iterator<char>(std::cout));
}
+5
source to share