C++11在G++ 4.6上不支持,4.7则支持,但是需要编译的时候加上选项’-std=c++11’
即‘g++ -std=c++11 test.cpp -o test’
void func(int z){ cout << "int"; }
void func(int *z){ cout << "int*"; }
void sumf(int &x){ cout << x; }
class foo{
public:
virtual int bar() final { cout << "foo:bar"; return 1; }
};
class foo2: public foo{
public:
int bar() const { cout << "foo2:bar"; return 1; }
};
void main(void)
{
//auto
auto i = 0;
vector<int> x;
auto j = x.begin();
//nullptr
int * p = NULL;
int * q = nullptr;
cout << (p == q) << endl;
// in test, if func(int *)&func(int) both defined, use func(int)
// if just define one of them,compile ok too.
//but for func(nullptr), it only compile ok in func(int *);
func(0);
func(nullptr);
func(NULL);
// auto in for
int ar[] = { 1, 2, 3 };
for (auto p : ar)
{
cout << p << endl;
}
//lamda
int sum = 0;
for_each(ar, ar + 3, [&sum](int &x){sum += x; });
cout << sum<<endl;
for_each(ar, ar + 3, sumf);
for (auto i : ar)
{
sum += i;
}
cout << sum;
cout << "test lamda------------------" << endl;
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for_each(a, a + 9, [](int &x){x *= 2; });// x = x *2
for_each(a, a + 9, [](int &x){cout << x << endl; });//cout x
int sum2 = 0;
for_each(a, a + 9, [&](int &x){sum2 += x; });// get sum, sum is change bcz '[&]', it means all para outside are passed by ref.
cout << sum2 << endl;
for_each(a, a + 9, [](int x){x*=2; });// x is not changed, bcz '(int x)', x is passed by value.
for_each(a, a + 9, [](int x){cout << x << endl; });
// initial list
cout << "test initial list-----------------" << endl;
int arr2[3] = { 1, 2, 3 };
int arr3[3] {1, 2, 3};
for_each(arr3, arr3 + 3, [](int &x){cout << x << endl; });// 1 2 3
int arr4[3] {1};
for_each(arr4, arr4 + 3, [](int &x){cout << x << endl; });// 1 0 0 . if set it as 1 2 3 4 , will compile failed bxz too many initial paras(vs2013).
// override final
//'override' to indicate that the function want override its parent function, compiler? will check it.if it can't match override rules, compile failed.
//'final' to indicate that we don't want the function to be override by its child class..if there is a child class want to override it ,compile failed.
cout << "override final......................." << endl;
foo *pfoo = new foo2;
pfoo->bar();
delete pfoo;
cout << "begin,end................................." << endl;
int arr5[] = { 1, 2, 3, 4, 5 };
for_each(arr5, arr5 + 5, [](int x){cout << x << endl; });
for_each(begin(arr5), end(arr5), [](int x){cout << x << endl; });// 'begin' , 'end' can be set as the first and last-next iterator of a container. and it ONLY can be set to a container.
cout << "static_assert................................." << endl;
auto var = 0;
static_assert(0<3, "aaaaaaaaaaaaaaaa");
system("pause");
}