STL에서는 가변 크기 배열로 vector와 deque을 지원합니다.
이것으로 대부분은 C의 배열을 대체할 수 있습니다.
특히 vector는 C 배열과 동일한 메모리 구조를 가지고 있기 때문에
기존의 C API의 인자로 그대로 넘길 수 있다는 장점이 있지요.
하지만, 역시 가변 크기 기능이 필요없다면 쓸데없는 오버헤드가 있기 마련입니다.
또한
int data[] = { 1, 2, 3, 4 };
처럼 컴파일 타임에 원소를 초기화하는 기능도 없지요.
반면에 C 배열은 STL 컨테이너가 아니기 때문에 여러모로 불편합니다.
고정 크기 배열 STL 컨테이너가 필요하다면,
boost::array 를 써보실 것을 권합니다.
다음 예제를 보세요.
#include <iostream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
int main()
{
array<int, 6> coll = {{ 5, 6, 2, 4, 1, 3 }};
// 템플릿의 인자로 배열의 크기가 고정되어 컴파일 됩니다.
// 또한 컴파일 타임에 원소를 초기화할 수 있는데, {} 대신 {{}}를 사용해야 합니다.
// 다음은 array에 STL 알고리듬을 적용하는 예제입니다.
// STL 컨테이너이므로 배열에 비해 STL 알고리듬을 적용하기가 훨씬 편리합니다.
// square all elements
transform (coll.begin(), coll.end(), // first source
coll.begin(), // second source
coll.begin(), // destination
multiplies<int>()); // operation
// sort beginning with the second element
sort (coll.begin() + 1, coll.end());
// print all elements
copy (coll.begin(), coll.end(),
ostream_iterator<int>(cout," "));
cout << endl;
}
실행 결과는 다음과 같습니다.
25 1 4 9 16 36
두번째 예제입니다.
다른 시퀀스 컨테이너인 vector, deque과 동일한 이름의 멤버함수를 가집니다.
#include <iostream>
#pragma hdrstop
#include <numeric>
#include <iterator>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
int main()
{
// define special type name
typedef array<int, 6> Array;
// create and initialize an array
Array a;
iota(a.begin(), a.end(), 42);
// use some common STL container operations
cout << "size: " << a.size()
<< "\nempty: " << (a.empty() ? "true" : "false") // 연산자 우선 순위를 주의하세요.
<< "\nmax_size: " << a.max_size()
<< "\nfront: " << a.front()
<< "\nback: " << a.back()
<< "\nelems: ";
// iterate through all elements
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
// check copy constructor and assignment operator
Array b(a), c;
c = a;
cout << (a == b && a == c ?
"\ncopy construction and copy assignment are OK.\n" :
"\ncopy construction and copy assignment FAILED.\n");
}
실행 결과는 다음과 같습니다.
size: 6
empty: false
max_size: 6
front: 42
back: 47
elems: 42 43 44 45 46 47
copy construction and copy assignment are OK
세번째 예제입니다.
array에 다양한 STL 알고리듬을 적용해봅니다.
#include <iostream>
#pragma hdrstop
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
int main()
{
// create and initialize array
array<int, 10> a;
iota(a.begin(), a.begin() + 5, 1);
fill(a.begin() + 5, a.end(), 1);
ostream_iterator<int> out(cout, " ");
copy(a.begin(), a.end(), out); // print elements
cout << endl;
// modify elements using STL algorithm
transform(a.begin(), a.begin() + 5, // source
a.begin(), // destination
bind2nd(plus<int>(), 1)); // operation
copy(a.begin(), a.end(), out); // print elements
cout << endl;
// change order using STL algorithm
reverse(a.begin(),a.end());
copy(a.begin(), a.end(), out); // print elements
cout << endl;
// negate elements using STL algorithm
transform(a.begin(), a.end(), // source
a.begin(), // destination
negate<int>()); // operation
copy(a.begin(), a.end(), out); // print elements
cout << endl;
}
실행 결과는 다음과 같습니다.
1 2 3 4 5 1 1 1 1 1
2 3 4 5 6 1 1 1 1 1
1 1 1 1 1 6 5 4 3 2
-1 -1 -1 -1 -1 -6 -5 -4 -3 -2
|