doc: update docs/cpp.md (#611)

This commit is contained in:
YuRuiH 2024-04-16 16:10:24 +08:00 committed by GitHub
parent ebcae6d5cd
commit c0ebe199d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -73,6 +73,11 @@ int a = 5, b = 10;
std::swap(a, b); std::swap(a, b);
// 输出: a=10, b=5 // 输出: a=10, b=5
std::cout << "a=" << a << ", b=" << b; std::cout << "a=" << a << ", b=" << b;
// 整数交换的奇技淫巧
(x ^= y), (y ^= x), (x ^= y);
// 注意! 以下操作会造成 undefined behavior
x ^= y ^= x ^= y;
``` ```
### 注释 ### 注释
@ -502,6 +507,15 @@ for (int i = 0, j = 2; i < 3; i++, j--){
// 输出: i=0,j=2;i=1,j=1;i=2,j=0; // 输出: i=0,j=2;i=1,j=1;i=2,j=0;
``` ```
### auto
```cpp
std:: string s = "hello world";
for(auto c: s){
std:: cout << c << " ";
}
// 输出: h e l l o w o r l d
```
C++ 函数 C++ 函数
------------ ------------