explicit vector(const allocator_type& alloc = allocator_type()); // C++11
vector(); // C++14
explicit vector(const allocator_type& alloc); // C++14
: 요소가 없는 빈 컨테이너를 생성합니다.
<예시 코드>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
return 0;
}
explicit vector(size_type n); // C++11
vector(size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
explicit vector(size_type n, const allocator_type& alloc = allocator_type()); // C++14
: n개의 요소를 가진 컨테이너를 생성합니다.
val가 제공된 경우 n개의 val 요소를 가진 컨테이너를 생성합니다.
<예시 코드>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1(10);
vector<int> vec2(5, 1);
for (const int& i : vec1) {
cout << i << ' ';
}
cout << endl;
for (const int& i : vec2) {
cout << i << ' ';
}
return 0;
}
결과
template<class InputIterator>
vector(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
: [fisrt, last) 범위만큼의 요소를 포함하는 컨테이너를 생성합니다. (동일한 순서)
<예시 코드>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1(5, 3); // {3, 3, 3, 3, 3}
vector<int> vec2(vec1.begin(), vec1.end());
for (const int& i : vec2) {
cout << i << ' ';
}
return 0;
}
결과
vector(const vector& x);
vector(const vector& x, const allocator_type& alloc);
: x의 각 요소를 동일한 순서로 복사한 컨테이너를 생성합니다.
<예시 코드>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1(5, 3); // {3, 3, 3, 3, 3}
vector<int> vec2(vec1);
for (const int& i : vec2) {
cout << i << ' ';
}
return 0;
}
결과
vector(vector&& x);
vector(vector&& x, const allocator_type& alloc);
: x의 요소를 가져오는 컨테이너를 생성합니다.
alloc이 제공된 경우 alloc을 사용하여 x의 내용을 이동시킵니다.
<예시 코드>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1(5, 3); // {3, 3, 3, 3, 3}
vector<int> vec2(move(vec1));
for (const int& i : vec1) {
cout << i << ' ';
}
cout << endl;
for (const int& i : vec2) {
cout << i << ' ';
}
return 0;
}
결과
vector(initializer_list<value_type> il, const allocator_type& alloc = allocator_type());
: il의 각 요소로 컨테이너를 생성합니다.
<예시 코드>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = { 1, 2, 3, 4 };
for (const int& i : vec) {
cout << i << ' ';
}
return 0;
}
결과