template <class OutputIterator, class T> class raw_storage_iterator;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class OutputIterator, class T>
class raw_storage_iterator :
public iterator<output_iterator_tag,void,void,void,void>
{
protected:
OutputIterator iter_;
public:
explicit raw_storage_iterator (OutputIterator x) : iter_(x) {}
raw_storage_iterator<OutputIterator,T>& operator* ()
{ return *this; }
raw_storage_iterator<OutputIterator,T>& operator= (const T& element)
{ new (static_cast<void*>(&*iter_)) T (element); return *this; }
raw_storage_iterator<OutputIterator,T>& operator++ ()
{ ++iter_; return *this; }
raw_storage_iterator<OutputIterator,T> operator++ (int)
{ raw_storage_iterator<OutputIterator,T> tmp = *this; ++iter_; return tmp; }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// raw_storage_iterator example
#include <iostream>
#include <memory>
#include <vector>
#include <string>
int main () {
std::vector<std::string> myvector;
myvector.push_back ("first");
myvector.push_back ("second");
myvector.push_back ("third");
std::pair<std::string*,std::ptrdiff_t> result = std::get_temporary_buffer<std::string>(3);
std::string* pstr=result.first;
std::raw_storage_iterator<std::string*,std::string> raw_it (pstr);
copy (myvector.begin(), myvector.end(), raw_it);
for (int i=0; i<3; i++)
std::cout << pstr[i] << ' ';
std::cout << '\n';
std::return_temporary_buffer(pstr);
return 0;
}
first second third