以下は、http://www.sgi.com/tech/stl/copy.htmlによるcopyの定義です。STLコピーの実装
template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
while (first != last) *result++ = *first++;
return result;
}
次のコードを書きました。
vector<int> v;
set<int> s;
s.insert(7);
s.insert(11);
s.insert(27);
//copy elements from the set into the vector
copy(s.begin(), s.end(), v.begin());
なぜ上記のコピーを呼び出すと、実行時エラーは発生しますが、コンパイルエラーは発生しません。私は、ベクトルが空であるという事実、v.begin()== v.end()と関係があると仮定しています。しかし、なぜ?
また、コードを次のように変更して修正しました。
copy(s.begin(), s.end(), back_inserter(v));
機能back_inserterは、タイプback_insert_iterator>のイテレータを返します。なぜこれは機能しますか?それは何ですか?