assign

提供:cppreference.com
移動: 案内, 検索


文法:

    #include <list>
    void assign( size_type 数, const T&);
    void assign( input_iterator start, input_iterator end );

assign()関数は現在のリストに対して、startからendまでの値、あるいは"値"のコピーを"数"個分、値を設定します。

この関数は、それまで持っていたリストの値をすべて破棄します。

例えば、次のコードのサンプルでは、assing()を使って42という数値のコピーを10個、リストに設定しています:

   list<int> l;
   l.assign( 10, 42 );
   for( list<int>::size_type i = 0; i < l.size(); i++ ) {
     cout << l[i] << " ";
   }
   cout << endl;

上記のコードを実行すると、次のように表示されます:

   42 42 42 42 42 42 42 42 42 42

次のサンプルは、assign()を使って他のリストのコピーを行う方法です:

   list<int> l1;
   for( int i = 0; i < 10; i++ ) {
     l1.push_back( i );
   }
 
   list<int> l2;
   l2.assign( l1.begin(), l1.end() );
 
   for( list<int>::size_type i = 0; i < l2.size(); i++ ) {
     cout << l2[i] << " ";
   }
   cout << endl;

上記のコードを実行すると、次のように表示されます:

   0 1 2 3 4 5 6 7 8 9


関連トピック: insert, push_back, push_front

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語