C++의 STD::배열
배열은 동종 객체의 모음이며 이 배열 컨테이너는 일정한 크기의 배열 또는 (정적 크기)에 대해 정의됩니다. 이 컨테이너는 고정 크기 배열을 감싸며 포인터에 선언될 때 크기 정보가 손실되지 않습니다.
배열을 활용하려면 배열 헤더를 포함해야 합니다.
#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>또는1{{3, 4, 5, 1, 2}};> > array <> int> , 5>ar2 = {1, 2, 3, 4, 5};> > array ar3 = {{string(> 'a'> ),> '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>도착={> 'G'> ,> 'f'> ,> 'G'> };> > cout < ' ' < return 0; }> |
산출
G G
b) front( ) 및 back( ) 기능: 이러한 메서드는 배열의 첫 번째 요소와 마지막 요소에 직접 액세스하는 데 사용됩니다.
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>도착={> 'G'> ,> 'f'> ,> 'G'> };> // ASCII val of 'G' =71> > cout < ' ' < return 0; }> |
산출
71 71
c) 스왑( ) 함수: 이 스왑 기능은 두 배열의 내용을 바꾸는 데 사용됩니다.
전:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>도착={> '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) 비어 있음( ) 함수: 이 함수는 선언된 STL 배열이 비어 있는지 여부를 확인하는 데 사용됩니다. 비어 있으면 true, 그렇지 않으면 false를 반환합니다.
전:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 3>도착={> '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>도착={> '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) 채우기() 함수: 이는 배열의 모든 인덱스를 유사한 값으로 초기화하거나 채우는 데 특별히 사용됩니다.
전:
C++
#include> #include> using> namespace> std;> int> main() {> > array <> int> , 5>도착;> > arr.fill(1);> > for> (> int> i: arr)> > cout < ' '; return 0; }> |
산출
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>arr;> > 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>도착;> > memcpy> (arr.data(),str,13);> > cout < < arr.data() < <> '
'> ;> > return> 0;> }> |
techcodeview.com
I) cbegin( ) 및 cend( ): 이 gfg 링크로 이동합니다. 클릭 해주세요