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

提供: C++入門
移動: 案内検索
(Daemon がページ「Std::vector::vector」を「std::vector::vector」に移動しました)
 
(同じ利用者による、間の9版が非表示)
行1: 行1:
<!--
+
[[std::vector::vector]]は、[[C++]]の[[STL]] [[コンテナ]]の[[std::vector]]の[[コンストラクタ]]です。[[std::vector]]の初期化をします。
vim: filetype=mediawiki
+
-->
+
 
+
[[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]]以外の初期化の方法としては、以下の方法があります。
[[std::vector::vector]] の使い方をいくつか示します。
+
* [[std::iota]]で各要素に連番の値を代入する。
 +
* [[std::generate]]で各要素に値を代入する。
 +
* [[std::for_each]]で各要素に値を代入する。
 +
* [[std::fill]]で各要素に同じ値を代入する。
 +
* [[for]]文で[[std::vector::push_back]]で1つ1つプッシュする。
  
[[C++11]]
+
[[C++11]]の場合
  
 
<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,
行26: 行27:
  
 
//range (3)
 
//range (3)
 
 
template <class InputIterator>
 
template <class InputIterator>
 
   vector (InputIterator first, InputIterator last,
 
   vector (InputIterator first, InputIterator last,
行32: 行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());
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
== 初期化リストで初期化する ==
 +
[[C++11]]では、[[std::initializer_list]]が実装され、初期化リストで[[コンテナ]]が初期化できるようになりました。
 +
<syntaxhighlight lang="cpp">
 +
vector<int> v{1,2,3};
 +
</syntaxhighlight>
 +
このコードをコンパイルするには、[[C++11]]に対応した[[g++]]などのコンパイラが必要です。
 +
コンパイルする場合には、-std=c++11などのオプションが必要です。
 +
<syntaxhighlight lang="bash">
 +
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
 +
</syntaxhighlight>
 +
詳細については、[[std::vector シンプルな例]]をご参照ください。
 +
 +
== 初期化リストでの初期化の例 ==
 +
=== ソースコード auto2.cpp ===
 +
<syntaxhighlight lang="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;
 +
}
 +
</syntaxhighlight>
 +
=== コンパイル ===
 +
<syntaxhighlight lang="bash">
 +
g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
 +
-Wl,-rpath=/usr/local/lib/gcc49  auto2.cpp -o auto2
 +
</syntaxhighlight>
 +
=== 実行例 ===
 +
<syntaxhighlight lang="bash">
 +
% ./auto2
 +
1
 +
2
 +
3
 +
</syntaxhighlight>
 
== std::vectorをコンストラクタで初期化する例 ==
 
== std::vectorをコンストラクタで初期化する例 ==
 
 
=== ソースコード vector_constructor_1.cpp ===
 
=== ソースコード vector_constructor_1.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行75: 行112:
  
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++  -I/usr/local/include vector_constructor_1.cpp -o vector_constructor_1
 
g++  -I/usr/local/include vector_constructor_1.cpp -o vector_constructor_1
行81: 行117:
  
 
=== 実行例 ===
 
=== 実行例 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./vector_constructor_1
 
% ./vector_constructor_1
行88: 行123:
 
100
 
100
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
 
== イテレータを使用した初期化の例 ==
 
== イテレータを使用した初期化の例 ==
 
 
=== ソースコード vector_constructor_2.cpp ===
 
=== ソースコード vector_constructor_2.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行118: 行149:
  
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++  -I/usr/local/include vector_constructor_2.cpp -o vector_constructor_2
 
g++  -I/usr/local/include vector_constructor_2.cpp -o vector_constructor_2
行124: 行154:
  
 
=== 実行例 ===
 
=== 実行例 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./vector_constructor_2
 
% ./vector_constructor_2
行131: 行160:
 
100
 
100
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== ほかのstd::vectorをコンストラクタでコピーする例 ==
 
== ほかのstd::vectorをコンストラクタでコピーする例 ==
 
 
=== ソースコード vector_constructor_3.cpp ===
 
=== ソースコード vector_constructor_3.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行161: 行187:
  
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
g++  -I/usr/local/include vector_constructor_3.cpp -o vector_constructor_3
 
g++  -I/usr/local/include vector_constructor_3.cpp -o vector_constructor_3
行167: 行192:
  
 
=== 実行例 ===
 
=== 実行例 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./vector_constructor_3
 
% ./vector_constructor_3
行176: 行200:
  
 
== 配列を利用した初期化の例 ==
 
== 配列を利用した初期化の例 ==
 
 
=== ソースコード vector_constructor_4.cpp ===
 
=== ソースコード vector_constructor_4.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行201: 行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">
 
% ./vector_constructor_4
 
% ./vector_constructor_4
行216: 行234:
 
3
 
3
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 関連項目 ==
 
== 関連項目 ==
 
* [[std::vector]]
 
 
* [[std::vector::assign]]
 
* [[std::vector::assign]]
 
* [[BOOST_FOREACH]]
 
* [[BOOST_FOREACH]]
 +
* [[コンテナ]]
 +
{{vector}}
 +
* [[配列]]
 +
<!-- 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で追加された、コンテナサイズを領域に合わせます。メモリの解放に利用します。