C++ の STD::array
配列は同種のオブジェクトのコレクションであり、この配列コンテナーは一定サイズの配列または (静的サイズ) に対して定義されます。このコンテナは固定サイズの配列をラップしており、ポインタとして宣言してもそのサイズの情報は失われません。
配列を利用するには、配列ヘッダーを含める必要があります。
#include
例を見てみましょう。
CPP
// CPP program to demonstrate working of array> #include> #include> #include> #include> #include> using> namespace> std;> int> main() {> > // construction uses aggregate initialization> > // double-braces required> > array <> int> , 5>or1{{3, 4, 5, 1, 2}};>> > array <> int> , 5>ar2 = {1, 2, 3, 4, 5};>>' ),> 'b'> }};> > cout < <> 'Sizes of arrays are'> < < endl;> > cout < < ar1.size() < < endl;> > cout < < ar2.size() < < endl;> > cout < < ar3.size() < < endl;> > > cout < <> '
Initial ar1 : '> ;> > for> (> auto> i : ar1)> > cout < < i < <> ' '> ;> > // container operations are supported> > sort(ar1.begin(), ar1.end());> > cout < <> '
sorted ar1 : '> ;> > for> (> auto> i : ar1)> > cout < < i < <> ' '> ;> > // Filling ar2 with 10> > ar2.fill(10);> > cout < <> '
Filled ar2 : '> ;> > for> (> auto> i : ar2)> > cout < < i < <> ' '> ;> > // ranged for loop is supported> > cout < <> '
ar3 : '> ;> > for> (> auto> &s : ar3)> > cout < < s < <> ' '> ;> > return> 0;> }> |
出力:
Sizes of arrays are 5 5 2 Initial ar1 : 3 4 5 1 2 sorted ar1 : 1 2 3 4 5 Filled ar2 : 10 10 10 10 10 ar3 : a b
この C++ STL 配列は一種のシーケンシャル コンテナであり、通常のプログラミングや競技プログラミングではあまり使用されませんが、そのメンバー関数は、日常生活で使用する通常の通常の配列よりも優れたエッジを提供することがあります。したがって、このような種類の配列で使用される重要なメンバー関数のいくつかについて説明します。
配列テンプレートのメンバー関数は次のとおりです。
構文: 配列 arr_name;
a) [ ] 演算子 : これは通常の配列に似ており、インデックス ‘i’ の要素ストアにアクセスするために使用します。
元:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> char> , 3>arr={>> 'G'> ,> 'f'> ,> 'G'> };> > cout < ' ' < return 0; }> |
出力
G G
b) フロント( ) およびバック( ) 関数: これらのメソッドは、配列の最初と最後の要素に直接アクセスするために使用されます。
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>arr={>> 'G'> ,> 'f'> ,> 'G'> };> // ASCII val of 'G' =71> > cout < ' ' < return 0; }> |
出力
71 71
c) swap() 関数: このスワップ関数は、2 つの配列の内容を交換するために使用されます。
元:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>arr={>> 'G'> ,> 'f'> ,> 'G'> };> // ASCII val of 'G' =71> > array <> int> , 3>arr1={>> 'M'> ,> 'M'> ,> 'P'> };> // ASCII val of 'M' = 77 and 'P' = 80> > arr.swap(arr1);> // now arr = {M,M,P}> > cout < ' ' < return 0; }> |
出力
77 80
d) empty( ) 関数: この関数は、宣言された STL 配列が空かどうかをチェックするために使用されます。空の場合は true、それ以外の場合は false を返します。
元:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>arr={>> 'G'> ,> 'f'> ,> 'G'> };> // ASCII val of 'G' =71> > array <> int> , 3>arr1={>> 'M'> ,> 'M'> ,> 'P'> };> // ASCII val of 'M' = 77 and 'P' = 80> > bool> x = arr.empty();> // false ( not empty)> > cout < return 0; }> |
出力
false
e) at( ) 関数: この関数は、特定の場所に格納されている要素にアクセスするために使用されます。配列サイズの範囲外の要素にアクセスしようとすると、例外がスローされます。
元:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>arr={>> 'G'> ,> 'f'> ,> 'G'> };> // ASCII val of 'G' =71> > array <> int> , 3>arr1={>> 'M'> ,> 'M'> ,> 'P'> };> // ASCII val of 'M' = 77 and 'P' = 80> > cout < < arr.at(2) < <> ' '> < < arr1.at(2);> > //cout < < arr.at(3); // exception{Abort signal from abort(3) (SIGABRT)}> > return> 0;> }> |
出力
71 80
f) fill() 関数: これは、配列のすべてのインデックスを初期化するか、同様の値で埋めるために特に使用されます。
元:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 5>ああ;>>' |
出力
1 1 1 1 1
g) size( ) または max_size( ) および sizeof( ) 関数: size( ) または max_size( ) は両方とも配列内のインデックスの最大数を取得するために使用され、sizeof( ) は配列の合計サイズをバイト単位で取得するために使用されます。
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 10>ああ、>> > cout < '
'; // total num of indexes cout < '
'; // total num of indexes cout < |
出力
10 10 40
h) データ( ): この関数は、配列オブジェクトの最初の要素へのポインタを返します。配列内の要素は連続したメモリ位置に格納されるためです。この data( ) 関数は、string/char 型オブジェクトのベース アドレスを返します。
元:
C++
#include> #include> #include> using> namespace> std;> int> main ()> {> > const> char> * str => 'techcodeview.com'> ;> > array <> char> ,13>ああ;>>' ;> > return> 0;> }> |
techcodeview.com
I) cbegin( ) と cend( ): この gfg リンクに移動します。 クリックミー