스터디/C++
유니폼 초기화, initializer_list (이니셜라이져 리스트),
kcrong
2015. 5. 13. 19:41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int a = 10; int b(10); // add int c = { 10 }; // int ! int d{ 10 }; // int ! auto a = 10; auto b(10); // add auto c = { 10 }; // int ? auto d{ 10 }; // int ? | cs |
Initializer_list !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> #include <vector> #include <map> int main() { // 10,20,30,40,50 std::vector<int> vInt; vInt.push_back(10); vInt.push_back(20); vInt.push_back(30); vInt.push_back(40); vInt.push_back(50); // sooooo long ㅜㅜ // 10,20,30,40,50 std::stack<int> sInt; sInt.push(10); sInt.push(20); sInt.push(30); sInt.push(40); sInt.push(50); } | cs |
위처럼 너무 코드가 길어진다 ㅜㅜ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> #include <vector> #include <map> int main() { // 10,20,30,40,50 std::vector<int> vInt; vInt.push_back(10); vInt.push_back(20); vInt.push_back(30); vInt.push_back(40); vInt.push_back(50); // sooooo long ㅜㅜ std::vector<int> vInt = { 10,20,30,40,50 }; // 10,20,30,40,50 std::stack<int> sInt; sInt.push(10); sInt.push(20); sInt.push(30); sInt.push(40); sInt.push(50); } | cs |
그래서 17 번째 줄처럼 한번에 vector 를 초기화 할 수 있다!!!
(하지만 스택에는 없나보다..)
1 | std::initializer_list<int> a = { 1,2,3,4,5,6}; |
Initializer_list 에는 갯수 제한이 없음
이니셜라이즈 리스트는 이질적이던 초기화를 통일시킴.