Skip to content

Commit c0ebe19

Browse files
authored
doc: update docs/cpp.md (#611)
1 parent ebcae6d commit c0ebe19

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

docs/cpp.md

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ int a = 5, b = 10;
7373
std::swap(a, b);
7474
// 输出: a=10, b=5
7575
std::cout << "a=" << a << ", b=" << b;
76+
77+
// 整数交换的奇技淫巧
78+
(x ^= y), (y ^= x), (x ^= y);
79+
// 注意! 以下操作会造成 undefined behavior
80+
x ^= y ^= x ^= y;
7681
```
7782
7883
### 注释
@@ -108,13 +113,13 @@ for (int i = 0; i < 10; i++) {
108113

109114
```cpp
110115
#include <iostream>
111-
116+
112117
void hello(); // 声明
113-
118+
114119
int main() { // 主函数
115120
hello(); // 执行函数
116121
}
117-
122+
118123
void hello() { // 定义
119124
std::cout << "Hello Quick Reference!\n";
120125
}
@@ -155,7 +160,7 @@ using namespace ns1;
155160
using namespace std;
156161
int main()
157162
{
158-
cout << val();
163+
cout << val();
159164
}
160165
```
161166

@@ -237,7 +242,7 @@ for (int i = 0; i < 2; ++i) {
237242
std::cout << x[i][j] << " ";
238243
}
239244
}
240-
// 输出: 1 2 3 4 5 6 6 5 4 3 2 1
245+
// 输出: 1 2 3 4 5 6 6 5 4 3 2 1
241246
```
242247

243248
C++ 条件
@@ -475,7 +480,7 @@ for (char c: hello)
475480
{
476481
std::cout << c << " ";
477482
}
478-
// 输出: Q u i c k R e f . M E
483+
// 输出: Q u i c k R e f . M E
479484
```
480485

481486
### 中断语句
@@ -502,6 +507,15 @@ for (int i = 0, j = 2; i < 3; i++, j--){
502507
// 输出: i=0,j=2;i=1,j=1;i=2,j=0;
503508
```
504509

510+
### auto
511+
```cpp
512+
std:: string s = "hello world";
513+
for(auto c: s){
514+
std:: cout << c << " ";
515+
}
516+
// 输出: h e l l o w o r l d
517+
```
518+
505519
C++ 函数
506520
------------
507521

@@ -510,10 +524,10 @@ C++ 函数
510524
```cpp
511525
#include <iostream>
512526
int add(int a, int b) {
513-
return a + b;
527+
return a + b;
514528
}
515529
int main() {
516-
std::cout << add(10, 20);
530+
std::cout << add(10, 20);
517531
}
518532
```
519533
@@ -538,7 +552,7 @@ void fun(int a) {
538552
```cpp
539553
#include <iostream>
540554
#include <cmath> // 导入库
541-
555+
542556
int main() {
543557
// sqrt() 来自 cmath
544558
std::cout << sqrt(9);
@@ -599,16 +613,16 @@ auto func = []() -> return_type { };
599613
```cpp
600614
int val1 = 123, val2 = 456;
601615
string str1("123"), str2(456);
602-
616+
603617
auto func1 = [=, &str1]() -> int
604618
{
605-
return val1 == std::stoi(str1)
619+
return val1 == std::stoi(str1)
606620
? val1 : val2;
607621
};
608-
622+
609623
auto func2 = [&, val1]() -> int
610624
{
611-
return str1 == std::to_string(val1)
625+
return str1 == std::to_string(val1)
612626
? str1 : str2;
613627
};
614628
```
@@ -622,11 +636,11 @@ auto func = []() -> return_type { };
622636

623637
```cpp
624638
// vec中包含1, 2, 3, 4, 5
625-
std::vector<int> vec({1, 2, 3, 4, 5});
626-
std::for_each(vec.begin(), vec.end(),
639+
std::vector<int> vec({1, 2, 3, 4, 5});
640+
std::for_each(vec.begin(), vec.end(),
627641
[](int& ele) -> void
628642
{
629-
std::cout << ele
643+
std::cout << ele
630644
<< " ";
631645
});
632646
```
@@ -665,17 +679,17 @@ class Entry
665679

666680
Entry entry;
667681
// 调用operator()()
668-
std::thread my_thread_1(entry);
682+
std::thread my_thread_1(entry);
669683
// 调用Entry::entry_function
670-
std::thread my_thread_2(&Entry::entry_function, &entry);
684+
std::thread my_thread_2(&Entry::entry_function, &entry);
671685
```
672686
673687
以lambda表达式作为线程入口函数:
674688
675689
```cpp
676-
std::thread my_thread([]() -> void
690+
std::thread my_thread([]() -> void
677691
{
678-
// ...
692+
// ...
679693
});
680694
```
681695

@@ -693,13 +707,13 @@ my_thread.detach();
693707

694708
```cpp
695709
// 获取当前线程ID
696-
std::this_thread::get_id();
710+
std::this_thread::get_id();
697711
// 使当前线程休眠一段指定时间
698-
std::this_thread::sleep_for();
712+
std::this_thread::sleep_for();
699713
// 使当前线程休眠到指定时间
700714
std::this_thread::sleep_until();
701715
// 暂停当前线程的执行,让别的线程执行
702-
std::this_thread::yield();
716+
std::this_thread::yield();
703717
```
704718

705719
###
@@ -753,7 +767,7 @@ std::lock_guard<std::mutex> lock(m);
753767
```cpp
754768
// 手动上锁
755769
m.lock();
756-
std::lock_guard<mutex> lock(m,
770+
std::lock_guard<mutex> lock(m,
757771
std::adopt_lock);
758772
```
759773

@@ -773,7 +787,7 @@ std::unique_lock<mutex> lock(m);
773787
```cpp
774788
// 手动上锁
775789
m.lock();
776-
std::unique_lock<mutex> lock(m,
790+
std::unique_lock<mutex> lock(m,
777791
std::adopt_lock);
778792
```
779793

@@ -782,7 +796,7 @@ std::unique_lock<mutex> lock(m,
782796
尝试上锁,可以通过`std::unique_lock<Mutex>::owns_lock()`查看状态
783797

784798
```cpp
785-
std::unique_lock<mutex> lock(m,
799+
std::unique_lock<mutex> lock(m,
786800
std::try_to_lock);
787801
if (lock.owns_lock())
788802
{
@@ -867,12 +881,12 @@ cond.wait(lock, predicate);
867881
#### 创建异步任务
868882

869883
```cpp
870-
double func(int val);
884+
double func(int val);
871885

872886
// 使用std::async创建异步任务
873887
// 使用std::future获取结果
874888
// future模板中存放返回值类型
875-
std::future<double> result =
889+
std::future<double> result =
876890
std::async(func, 5);
877891
```
878892
@@ -910,7 +924,7 @@ int val = result.get();
910924
```cpp
911925
extern double foo(int val) {}
912926

913-
std::future<double> result =
927+
std::future<double> result =
914928
async(foo, 5);
915929

916930
//返回值类型
@@ -930,10 +944,10 @@ status = result.wait_for(
930944
931945
```cpp
932946
// 返回值已经准备好
933-
if (status ==
947+
if (status ==
934948
std::future_status::ready)
935949
{
936-
950+
937951
}
938952
// 超时:尚未准备好
939953
else if (status ==

0 commit comments

Comments
 (0)