「std::vector::vector」の版間の差分

提供: C++入門
移動: 案内検索
 
(同じ利用者による、間の2版が非表示)
行1: 行1:
[[std::vector::vector]]は、[[std::vector]] のコンストラクタです。[[std::vector]]の初期化をします。
+
[[std::vector::vector]]は、[[C++]]の[[STL]] [[コンテナ]]の[[std::vector]]の[[コンストラクタ]]です。[[std::vector]]の初期化をします。
  
 +
'''読み方'''
 +
;[[std::vector::vector]]:えすてぃーでぃー べくたー べくたー
 
__TOC__
 
__TOC__
  
 
== 概要 ==
 
== 概要 ==
[[std::vector::vector]] は、[[std::vector]]のコンストラクタです。[[std::vector::vector]] の使い方をいくつか示します。 [[C++11]]では、コンテナの初期化もできるようになりました。
+
[[std::vector::vector]] は、[[std::vector]]のコンストラクタです。[[std::vector::vector]] の使い方をいくつか示します。 [[C++11]]では、[[コンテナ]]の初期化もできるようになりました。
  
 
[[std::vector::vector]]以外の初期化の方法としては、以下の方法があります。
 
[[std::vector::vector]]以外の初期化の方法としては、以下の方法があります。
行12: 行14:
 
* [[std::fill]]で各要素に同じ値を代入する。
 
* [[std::fill]]で各要素に同じ値を代入する。
 
* [[for]]文で[[std::vector::push_back]]で1つ1つプッシュする。
 
* [[for]]文で[[std::vector::push_back]]で1つ1つプッシュする。
 
  
 
[[C++11]]の場合
 
[[C++11]]の場合
行18: 行19:
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
//default (1)
 
//default (1)
 
 
explicit vector (const allocator_type& alloc = allocator_type());
 
explicit vector (const allocator_type& alloc = allocator_type());
  
 
//fill (2)
 
//fill (2)
 
 
explicit vector (size_type n);
 
explicit vector (size_type n);
 
         vector (size_type n, const value_type& val,
 
         vector (size_type n, const value_type& val,
行28: 行27:
  
 
//range (3)
 
//range (3)
 
 
template <class InputIterator>
 
template <class InputIterator>
 
   vector (InputIterator first, InputIterator last,
 
   vector (InputIterator first, InputIterator last,
行34: 行32:
  
 
//copy (4)
 
//copy (4)
 
 
vector (const vector& x);
 
vector (const vector& x);
 
vector (const vector& x, const allocator_type& alloc);
 
vector (const vector& x, const allocator_type& alloc);
  
 
//move (5)
 
//move (5)
 
 
vector (vector&& x);
 
vector (vector&& x);
 
vector (vector&& x, const allocator_type& alloc);
 
vector (vector&& x, const allocator_type& alloc);
  
 
//initializer list (6)
 
//initializer list (6)
 
 
vector (initializer_list<value_type> il,
 
vector (initializer_list<value_type> il,
 
       const allocator_type& alloc = allocator_type());
 
       const allocator_type& alloc = allocator_type());
行50: 行45:
  
 
== 初期化リストで初期化する ==
 
== 初期化リストで初期化する ==
[[C++11]]では、[[std::initializer_list]]が実装され、初期化リストでコンテナが初期化できるようになりました。
+
[[C++11]]では、[[std::initializer_list]]が実装され、初期化リストで[[コンテナ]]が初期化できるようになりました。
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
vector<int> v{1,2,3};
 
vector<int> v{1,2,3};
行91: 行86:
 
3
 
3
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== std::vectorをコンストラクタで初期化する例 ==
 
== std::vectorをコンストラクタで初期化する例 ==
 
=== ソースコード vector_constructor_1.cpp ===
 
=== ソースコード vector_constructor_1.cpp ===
行129: 行123:
 
100
 
100
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== イテレータを使用した初期化の例 ==
 
== イテレータを使用した初期化の例 ==
 
=== ソースコード vector_constructor_2.cpp ===
 
=== ソースコード vector_constructor_2.cpp ===
行167: 行160:
 
100
 
100
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== ほかのstd::vectorをコンストラクタでコピーする例 ==
 
== ほかのstd::vectorをコンストラクタでコピーする例 ==
 
=== ソースコード vector_constructor_3.cpp ===
 
=== ソースコード vector_constructor_3.cpp ===
行231: 行223:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++  -I/usr/local/include vector_constructor_4.cpp -o vector_constructor_4
 
g++  -I/usr/local/include vector_constructor_4.cpp -o vector_constructor_4
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== 実行例 ===
 
=== 実行例 ===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
行245: 行234:
 
3
 
3
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 関連項目 ==
 
== 関連項目 ==
 
* [[std::vector::assign]]
 
* [[std::vector::assign]]
 
* [[BOOST_FOREACH]]
 
* [[BOOST_FOREACH]]
 +
* [[コンテナ]]
 
{{vector}}
 
{{vector}}
<!-- vim: filetype=mediawiki -->
+
* [[配列]]
 +
<!-- vim: filetype=mediawiki
 +
-->

2016年1月10日 (日) 20:38時点における最新版

std::vector::vectorは、C++STL コンテナstd::vectorコンストラクタです。std::vectorの初期化をします。

読み方

std::vector::vector
えすてぃーでぃー べくたー べくたー

概要

std::vector::vector は、std::vectorのコンストラクタです。std::vector::vector の使い方をいくつか示します。 C++11では、コンテナの初期化もできるようになりました。

std::vector::vector以外の初期化の方法としては、以下の方法があります。

C++11の場合

//default (1)	
explicit vector (const allocator_type& alloc = allocator_type());
 
//fill (2)	
explicit vector (size_type n);
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());
 
//range (3)	
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());
 
//copy (4)	
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
 
//move (5)	
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
 
//initializer list (6)	
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

初期化リストで初期化する

C++11では、std::initializer_listが実装され、初期化リストでコンテナが初期化できるようになりました。

vector<int> v{1,2,3};

このコードをコンパイルするには、C++11に対応したg++などのコンパイラが必要です。 コンパイルする場合には、-std=c++11などのオプションが必要です。

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  vector_for_int.cpp -o vector_for_int

詳細については、std::vector シンプルな例をご参照ください。

初期化リストでの初期化の例

ソースコード auto2.cpp

#include <iostream>
#include <vector>
using namespace std;
 
int
main(int argc, char const* argv[])
{
        std::vector<int> v{1,2,3};
        for (auto x: v) {
                cout << x << endl;
        }
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  auto2.cpp -o auto2

実行例

% ./auto2
1
2
3

std::vectorをコンストラクタで初期化する例

ソースコード vector_constructor_1.cpp

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
using namespace std;
void
dump (vector<int> &v) {
        BOOST_FOREACH(int x, v) {
                cout << x << endl;
        }
}
 
int
main(int argc, char const* argv[])
{
 
        vector<int>     v(3, 100);
        dump (v);
 
        return 0;
}

コンパイル

g++  -I/usr/local/include vector_constructor_1.cpp -o vector_constructor_1

実行例

% ./vector_constructor_1
100
100
100

イテレータを使用した初期化の例

ソースコード vector_constructor_2.cpp

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
using namespace std;
void
dump (vector<int> &v) {
        BOOST_FOREACH(int x, v) {
                cout << x << endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        vector<int>     v1(3, 100);
        vector<int>     v2(v1.begin(), v1.end());
        dump (v2);
 
        return 0;
}

コンパイル

g++  -I/usr/local/include vector_constructor_2.cpp -o vector_constructor_2

実行例

% ./vector_constructor_2
100
100
100

ほかのstd::vectorをコンストラクタでコピーする例

ソースコード vector_constructor_3.cpp

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
using namespace std;
void
dump (vector<int> &v) {
        BOOST_FOREACH(int x, v) {
                cout << x << endl;
        }
}
 
int
main(int argc, char const* argv[])
{
 
        vector<int>     v1(3, 100);
        vector<int>     v2(v1);
        dump (v2);
 
        return 0;
}

コンパイル

g++  -I/usr/local/include vector_constructor_3.cpp -o vector_constructor_3

実行例

% ./vector_constructor_3
100
100
100

配列を利用した初期化の例

ソースコード vector_constructor_4.cpp

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
using namespace std;
void
dump (vector<int> &v) {
        BOOST_FOREACH(int x, v) {
                cout << x << endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        int     array[] = {1,2,3};
        vector<int>     v(array, array + (sizeof(array)/sizeof(int)) );
        dump (v);
 
        return 0;
}

コンパイル

g++  -I/usr/local/include vector_constructor_4.cpp -o vector_constructor_4

実行例

% ./vector_constructor_4
1
2
3

関連項目

std::vector
メンバ 意味
constructor vectorのコンストラクタ。vectorを初期化します。
destrctor デストラクタ
operator= operator=
reserve std::vectorのキャパシティを予約します。
capacity 割り当てられているストレージキャパシティのサイズを返します。
size サイズを返します。
resize サイズをリサイズします。
shrink_to_fit コンテナのキャパシティにフィットしたサイズまで縮小します。
push_back std::vector の最後に新しい要素を追加します。要素は、コピー、もしくは、move されます。
std::vector の最後の要素を取り除きます。コンテナのサイズが1つ小さくなります。
insert std::vector に新しい要素を追加します。vector同士を連結できます。
erase std::vector の1つの要素、または、要素のレンジを削除します。
clear std::vector の要素をすべて削除します。
empty std::vector が空であれば、trueを返し、そうでなければ、falseです。
begin std::vector の最初の要素のイテレータを返します。
front std::vector の最初の要素の参照を返します。
back std::vector の最後の要素の参照を返します。
at std::vector の n 番目の要素の参照を返します。無効な要素にアクセスしたとき、 std::out_of_range の例外を送出します。 operator[] は、境界チェックをしません。
operator[] std::vector の n 番目の要素の参照を返します。 operator[] は、境界チェックをしません。
std::vector::shrink_to_fit C++11で追加された、コンテナサイズを領域に合わせます。メモリの解放に利用します。