本文是我的C++笔记的第一篇 My First C++ Note;
一些例子
1
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <iostream>
using namespace std;
int main() { string x{"222333"}; char *y = "222333"; string xx{x, 3}; string yy{y, 3}; cout << xx << endl; cout << yy << endl; }
|
2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| void pp(int x) { cout<<"int"; }
void pp(int* x) { cout<<"int*"; }
int main() { pp(nullptr); pp(NULL); }
|
一些疑问
1 2 3 4 5 6 7 8 9
| int s[2][2]{ {1, 2}, {3, 4}}; for (int *i : s) { for (int j{}; j < 2; ++j) { cout << i[j]; } }
|
这段代码为何生效?s的类型不应该是int()2吗? 解释:s的类型为int2,但是非常容易decay,因此大多数时候会提示为int()2,例如你用char x{s};试图从报错中得到s的类型的时候就会提示为int()2.此处从s中取出的应该是int2,然后被decay为int 追问:那有没有办法让i的类型为int2 解答:写成auto& i 或者 int(&x)2即可 完整代码为 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| int s[2][2]{ {1, 2}, {3, 4}}; for (int(&i)[2] : s) { for (int j : i) { cout << j; } } for (auto &i : s) { for (auto j : i) { cout << j; } } for (int j : (*s)) { cout << j; }
|