類模板
<memory>

std::raw_storage_iterator

template <class OutputIterator, class T>  class raw_storage_iterator;
原始儲存迭代器
此類迭代器操作未初始化的記憶體塊。

常規迭代器操作已經構造好的特定型別的物件。一個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; }
};

模板引數

OutputIterator
底層迭代器型別。
T
將在每個元素位置上構造的物件型別。

成員函式

建構函式
raw_storage_iterator物件是從迭代器構造的。
operator*
無操作。返回物件的引用。
operator=
在迭代器指向的位置構造型別為 T 的新物件,並用作右側運算元的副本初始化其值。
operator++
增加迭代器位置。

示例

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 


另見