@@ -73,6 +73,11 @@ int a = 5, b = 10;
73
73
std::swap (a, b);
74
74
// 输出: a=10, b=5
75
75
std::cout << "a=" << a << ", b=" << b;
76
+
77
+ // 整数交换的奇技淫巧
78
+ (x ^= y), (y ^= x), (x ^= y);
79
+ // 注意! 以下操作会造成 undefined behavior
80
+ x ^= y ^= x ^= y;
76
81
```
77
82
78
83
### 注释
@@ -108,13 +113,13 @@ for (int i = 0; i < 10; i++) {
108
113
109
114
``` cpp
110
115
#include < iostream>
111
-
116
+
112
117
void hello (); // 声明
113
-
118
+
114
119
int main () { // 主函数
115
120
hello (); // 执行函数
116
121
}
117
-
122
+
118
123
void hello () { // 定义
119
124
std::cout << "Hello Quick Reference!\n";
120
125
}
@@ -155,7 +160,7 @@ using namespace ns1;
155
160
using namespace std;
156
161
int main()
157
162
{
158
- cout << val();
163
+ cout << val();
159
164
}
160
165
```
161
166
@@ -237,7 +242,7 @@ for (int i = 0; i < 2; ++i) {
237
242
std::cout << x[i][j] << " ";
238
243
}
239
244
}
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
241
246
```
242
247
243
248
C++ 条件
@@ -475,7 +480,7 @@ for (char c: hello)
475
480
{
476
481
std::cout << c << " ";
477
482
}
478
- // 输出: Q u i c k R e f . M E
483
+ // 输出: Q u i c k R e f . M E
479
484
```
480
485
481
486
### 中断语句
@@ -502,6 +507,15 @@ for (int i = 0, j = 2; i < 3; i++, j--){
502
507
// 输出: i=0,j=2;i=1,j=1;i=2,j=0;
503
508
```
504
509
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
+
505
519
C++ 函数
506
520
------------
507
521
@@ -510,10 +524,10 @@ C++ 函数
510
524
``` cpp
511
525
#include < iostream>
512
526
int add (int a, int b) {
513
- return a + b;
527
+ return a + b;
514
528
}
515
529
int main() {
516
- std::cout << add(10, 20);
530
+ std::cout << add(10, 20);
517
531
}
518
532
```
519
533
@@ -538,7 +552,7 @@ void fun(int a) {
538
552
``` cpp
539
553
#include < iostream>
540
554
#include < cmath> // 导入库
541
-
555
+
542
556
int main () {
543
557
// sqrt() 来自 cmath
544
558
std::cout << sqrt(9);
@@ -599,16 +613,16 @@ auto func = []() -> return_type { };
599
613
``` cpp
600
614
int val1 = 123 , val2 = 456 ;
601
615
string str1 ("123"), str2(456);
602
-
616
+
603
617
auto func1 = [=, &str1]() -> int
604
618
{
605
- return val1 == std::stoi(str1)
619
+ return val1 == std::stoi(str1)
606
620
? val1 : val2;
607
621
};
608
-
622
+
609
623
auto func2 = [&, val1]() -> int
610
624
{
611
- return str1 == std::to_string(val1)
625
+ return str1 == std::to_string(val1)
612
626
? str1 : str2;
613
627
};
614
628
```
@@ -622,11 +636,11 @@ auto func = []() -> return_type { };
622
636
623
637
``` cpp
624
638
// 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(),
627
641
[ ] (int& ele) -> void
628
642
{
629
- std::cout << ele
643
+ std::cout << ele
630
644
<< " ";
631
645
});
632
646
```
@@ -665,17 +679,17 @@ class Entry
665
679
666
680
Entry entry;
667
681
// 调用operator()()
668
- std::thread my_thread_1 (entry);
682
+ std::thread my_thread_1 (entry);
669
683
// 调用Entry::entry_function
670
- std::thread my_thread_2(&Entry::entry_function, &entry);
684
+ std::thread my_thread_2(&Entry::entry_function, &entry);
671
685
```
672
686
673
687
以lambda表达式作为线程入口函数:
674
688
675
689
```cpp
676
- std::thread my_thread([]() -> void
690
+ std::thread my_thread([]() -> void
677
691
{
678
- // ...
692
+ // ...
679
693
});
680
694
```
681
695
@@ -693,13 +707,13 @@ my_thread.detach();
693
707
694
708
``` cpp
695
709
// 获取当前线程ID
696
- std::this_thread::get_id ();
710
+ std::this_thread::get_id ();
697
711
// 使当前线程休眠一段指定时间
698
- std::this_thread::sleep_for ();
712
+ std::this_thread::sleep_for ();
699
713
// 使当前线程休眠到指定时间
700
714
std::this_thread::sleep_until ();
701
715
// 暂停当前线程的执行,让别的线程执行
702
- std::this_thread::yield ();
716
+ std::this_thread::yield ();
703
717
```
704
718
705
719
### 锁
@@ -753,7 +767,7 @@ std::lock_guard<std::mutex> lock(m);
753
767
```cpp
754
768
// 手动上锁
755
769
m.lock();
756
- std::lock_guard<mutex> lock(m,
770
+ std::lock_guard<mutex> lock(m,
757
771
std::adopt_lock);
758
772
```
759
773
@@ -773,7 +787,7 @@ std::unique_lock<mutex> lock(m);
773
787
```cpp
774
788
// 手动上锁
775
789
m.lock();
776
- std::unique_lock<mutex> lock(m,
790
+ std::unique_lock<mutex> lock(m,
777
791
std::adopt_lock);
778
792
```
779
793
@@ -782,7 +796,7 @@ std::unique_lock<mutex> lock(m,
782
796
尝试上锁,可以通过` std::unique_lock<Mutex>::owns_lock() ` 查看状态
783
797
784
798
``` cpp
785
- std::unique_lock<mutex> lock (m,
799
+ std::unique_lock<mutex> lock (m,
786
800
std::try_to_lock);
787
801
if (lock.owns_lock())
788
802
{
@@ -867,12 +881,12 @@ cond.wait(lock, predicate);
867
881
#### 创建异步任务
868
882
869
883
``` cpp
870
- double func (int val);
884
+ double func (int val);
871
885
872
886
// 使用std::async创建异步任务
873
887
// 使用std::future获取结果
874
888
// future模板中存放返回值类型
875
- std::future<double > result =
889
+ std::future<double > result =
876
890
std::async(func, 5);
877
891
```
878
892
@@ -910,7 +924,7 @@ int val = result.get();
910
924
``` cpp
911
925
extern double foo (int val) {}
912
926
913
- std::future<double > result =
927
+ std::future<double > result =
914
928
async(foo, 5);
915
929
916
930
//返回值类型
@@ -930,10 +944,10 @@ status = result.wait_for(
930
944
931
945
```cpp
932
946
// 返回值已经准备好
933
- if (status ==
947
+ if (status ==
934
948
std::future_status::ready)
935
949
{
936
-
950
+
937
951
}
938
952
// 超时:尚未准备好
939
953
else if (status ==
0 commit comments