mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
revision #1: 更新第二章中已维护的代码
This commit is contained in:
59
code/2/2.4.initializer.list.cpp
Normal file
59
code/2/2.4.initializer.list.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// 2.4.initializer.list.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
class Foo {
|
||||
public:
|
||||
int value_a;
|
||||
int value_b;
|
||||
Foo(int a, int b) : value_a(a), value_b(b) {}
|
||||
};
|
||||
|
||||
class MagicFoo {
|
||||
public:
|
||||
std::vector<int> vec;
|
||||
MagicFoo(std::initializer_list<int> list) {
|
||||
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
|
||||
vec.push_back(*it);
|
||||
}
|
||||
}
|
||||
void foo(std::initializer_list<int> list) {
|
||||
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
|
||||
vec.push_back(*it);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
// before C++11
|
||||
int arr[3] = {1, 2, 3};
|
||||
Foo foo(1, 2);
|
||||
std::vector<int> vec = {1, 2, 3, 4, 5};
|
||||
|
||||
// after C++11
|
||||
MagicFoo magicFoo = {1, 2, 3, 4, 5};
|
||||
magicFoo.foo({6,7,8,9});
|
||||
Foo foo2 {3, 4};
|
||||
|
||||
std::cout << "arr[0]: " << arr[0] << std::endl;
|
||||
std::cout << "foo:" << foo.value_a << ", " << foo.value_b << std::endl;
|
||||
std::cout << "vec: ";
|
||||
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
|
||||
std::cout << *it << std::endl;
|
||||
}
|
||||
std::cout << "magicFoo: ";
|
||||
for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
|
||||
std::cout << *it << std::endl;
|
||||
}
|
||||
std::cout << "foo2: " << foo2.value_a << ", " << foo2.value_b << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user