# c/c++
[新的](https://continuous-air-7f3.notion.site/program-88cf601cfc82449d98253cbe2e334bff)
# [學習程式:實踐者的方法](https://jasonblog.github.io/note/c++/xue_xi_c_ff1a_shi_jian_zhe_de_fang_fa_ff08_beta1.html)
::: warning
## [c++控制台操作](https://www.cnblogs.com/flowingwind/p/8159035.html)
## [亂數1](https://edisonx.pixnet.net/blog/post/91314418) [亂數2](http://it-life.puckwang.com/2016/03/ccrandsrand.html)
:::
::: warning
## [int *p=a和int *p=&a有什么区别?](https://zhidao.baidu.com/question/485583430.html)
int *p=&a 其实是两个语句 等价于 int *p; p=&a
:::
::: warning
## [static local arrays and automatic local arrays](https://openhome.cc/Gossip/CGossip/Scope.html)
* 靜態陣列就算沒初始化,也設為0
* 放在 main 外的變數也是
:::
::: warning
## [extern 1](https://chenhh.gitbooks.io/parallel_processing/content/cython/c_syntax.html) [extern 2](https://meetonfriday.com/posts/9514e1a0/)
如果要使用在其他原始檔中定義的全域性變數,應該使用關鍵字 extern 來宣告
:::
::: warning
## [assert](https://www.runoob.com/w3cnote/c-assert.html)
:::
::: warning
## [x++ and ++x](http://weisnote.blogspot.com/2011/11/x-x.html )
* [++x(Prefix Increment)的overload - Point& Point::operator++()](https://docs.microsoft.com/zh-tw/cpp/cpp/increment-and-decrement-operator-overloading-cpp?view=msvc-170)
* [x++(Postfix Increment)的overload - Point Point::operator++(int)](https://docs.microsoft.com/zh-tw/cpp/cpp/increment-and-decrement-operator-overloading-cpp?view=msvc-170)
x++ 就是會先與其他算式運算 之後才 x = x + 1 將x值增1
++x 就是先 x = x + 1 將x值增1 之後才與其他算式運算
可以記
10*x++ = (x先乘10)在加1
10*++x = (x先加1)在乘10
:::
:::warning
## [const / volatile](https://chenhh.gitbooks.io/parallel_processing/content/cython/c_syntax.html)
:::
:::warning
## [struct](https://opensourcedoc.com/c-programming/struct/)
:::
:::warning
## [<curses.h> 1](http://epaper.gotop.com.tw/pdf/AXP009600.pdf) [<curses.h> 2](https://www.itread01.com/content/1546827516.html)
:::
:::warning
## [linked-list的建立與刪除](https://kopu.chat/c-%E8%AA%9E%E8%A8%80%EF%BC%9A%E9%8F%88%E7%B5%90%E4%B8%B2%E5%88%97linked-list%E7%9A%84%E5%BB%BA%E7%AB%8B%E8%88%87%E5%88%AA%E9%99%A4/)
:::
:::warning
## [IEEE 754](https://medium.com/starbugs/see-why-floating-point-error-can-not-be-avoided-from-ieee-754-809720b32175)
把數字標準化,一定要leading with 1 ,leading 1不用放進fraction的格子裡,換回浮點數要記得放回去。

:::
:::warning
## [return *this和return this的區別](https://www.itread01.com/content/1547744593.html)
假如定義temp *get(){return this;},那麼返回的this就是地址,即返回一個指向物件的指標
假如定義temp get(){return *this;} 那麼返回的就是物件的克隆,是一個臨時變數
假如定義temp &get(){return *this;} 那麼返回的就是物件本身
:::
:::warning
## [friend function 1 /](https://course.ipv6.club.tw/C_Programming/991/Code/Ch07/Ex7_08.cpp) [friend function 2](https://www.section.io/engineering-education/introduction-to-friend-functions-in-c++/)
:::
:::warning
## [Pass by value vs Pass by reference](https://medium.com/@racktar7743/c-c-%E6%8C%87%E6%A8%99%E6%95%99%E5%AD%B8-%E5%9B%9B-pass-by-value-vs-pass-by-reference-ed5882802789)
[任何型態皆可用C++新提出的reference傳進function](https://www.cnblogs.com/oomusou/archive/2007/02/09/645944.html)
* array 也可以
- void func(int (&arr)[3])
- &arr一定要括號,因為[]比&優先權高
- compiler只對array的型態做檢查,就算傳進function時指定了array size,compiler也會忽略,但若使用reference,compiler則會對array size做檢查(須要將array size寫死)
:::
:::warning
## [Return by reference ](https://www.geeksforgeeks.org/return-by-reference-in-c-with-examples/)
* **能讓return的東西變成左值**
* ==**return by reference不能回傳區域變數,因為他用完之後就消失了,會造成不可預期的結果(要用到區域變數能用[new](https://openhome.cc/Gossip/CppGossip/newDelete.html))**。==
* **注意int& b = a 的意思 (會讓a,b的記憶體位置相同)**
:::success
```cpp=
int a=3;
// 比較兩個的差別
int& b =a;
int c=a;
cout << a<<b<<c << endl<<&a<< endl<<&b<< endl<<&c;
```
:::
::: warning
## [copy consturctor](https://www.itread01.com/content/1548766986.html)
:::
:::warning
## [#include < string > ](https://www.cplusplus.com/reference/string/string/find/)
### find()
* size_t find (const char* s, size_t pos, size_type n)
* If no matches were found, the function returns string::npos.
:::
:::warning
## [NULL 、 0 、 '\0' 、 “0” 、 "\0"](https://zhuanlan.zhihu.com/p/79210633)
:::
:::warning
## [指標與字串](https://openhome.cc/Gossip/CGossip/PointerAndString.html)
是唯讀的不可跟改,建議加上const
:::
## [函式指標](https://openhome.cc/Gossip/CppGossip/FunctionPointer.html)
:::warning
## [void*](https://www.itread01.com/content/1546747742.html)
void *也可以無需強制型別轉換地賦給其它型別的指標。因為“無型別”可以包容“有型別”,而“有型別”則不能包容“無型別”。
函數fun1 指定給 fun2,等效於 &fun1 指定給 function2,在指定之後,fun2 儲存了 fun1 的位址,在呼叫時,fun1(20) 等效於 (*fun1)(20)
:::
::: warning
## c++ functional
???
:::
:::warning
## [為自訂類別多載 << 運算子 /](https://docs.microsoft.com/zh-tw/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=msvc-170) [為自訂類別多載 << 運算子](https://www.coursera.org/lecture/cpp-chengxu-sheji/liu-cha-ru-yun-suan-fu-he-liu-ti-qu-yun-suan-fu-de-zhong-zai-c3tbl)
輸出資料流針對標準類型使用插入 (<<) 運算子。也可以為您的自訂類別多載 << 運算子。
:::
:::warning
## Lvalue reference variables
???
For those of you already familiar with pointers, the ampersand in this context does not mean “address of”, it means “lvalue reference to”.
:::
:::warning
## [< time.h >](https://linux.die.net/man/3/ctime)
:::
:::warning
## [pointer to pointer (**p)](http://low-understated.blogspot.com/2009/04/pointer-to-pointer.html)
**p用來儲存pointer的位置


:::warning
:::info
兩篇對著看 #可以點圖片
第一張是把num1的位置傳進setNum,*num2也指向num1,所以*num2改了原本的質也會改
[](https://medium.com/@racktar7743/c-c-%E6%8C%87%E6%A8%99%E6%95%99%E5%AD%B8-%E5%9B%9B-pass-by-value-vs-pass-by-reference-ed5882802789)
第二張*pInt和*localPtr都指向local_int,因為changePtr的pInt是localPtr的複本, 對pInt做變更, 其實並不會影響到localPtr本身,印出來的數值仍為1
[](http://low-understated.blogspot.com/2009/04/pointer-to-pointer.html)
但是如果我們要改變的不是pointer所指向的實體的值,而是pointer本身的值,那就把pointer當作一種type,再把它的address傳到funtion的double pointer。
[](http://low-understated.blogspot.com/2009/04/pointer-to-pointer.html)
印出來的數值變為gint的0
:::
:::warning
## 函式多載規則
* **多個函式定義使用相同的函式名稱**
* **函式參數的數量或類型必須有區別**
:::
:::warning
## sizeof()
* **用bite顯示**
* **重要:在64位元電腦中,任何資料型態之指標變數所佔記憶體大小均為 8 bytes。**
* [**size of struct**](https://www.geeksforgeeks.org/is-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member/)
* [class 內的成員如果加上關鍵字static則不占class的空間](https://stackoverflow.com/questions/4842056/do-static-members-of-a-class-occupy-memory-if-no-object-of-that-class-is-created)
:::
:::warning
## [class 內的成員如果加上關鍵字static則不占class的空間](https://stackoverflow.com/questions/4842056/do-static-members-of-a-class-occupy-memory-if-no-object-of-that-class-is-created)
size of A and B is exactly the same.

:::
:::warning
## [c++檔案讀寫 <fstream>](https://hackmd.io/@ndhu-programming-2021/BkZukG4jK)
:::
:::warning
## [new 與 delete](https://openhome.cc/Gossip/CppGossip/newDelete.html)
* 用來存函數內的東西好用
:::
:::danger
# 處理文本
:::
:::warning
處理string特好用
## [ <sstream> ](https://www.796t.com/content/1549657646.html)
* 用它進行string與各種內建型別資料之間的轉換
* ostringstream類向一個string插入字元。
* istringstream類從一個string物件讀取字元。
## [stream](https://www.796t.com/content/1549657646.html)
:::
:::warning
## [字串 1 /](https://mycollegenotebook.medium.com/c-%E8%AA%9E%E8%A8%80%E7%AD%86%E8%A8%98-%E5%AD%97%E4%B8%B2-strings-ffe70ee5f5b8)[字串 2](https://www.796t.com/post/OTg5NGE=.html)
ANSI C 规范定义了 stof()、atoi()、atol()、strtod()、strtol()、strtoul() 共6个可以将字符串转换为数字的函数,大家可以对比学习。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull()
# ===== 用sstream比較好 =====
:::
:::warning
## [cin 輸入錯誤遇到死循環](https://blog.csdn.net/Fdog_/article/details/107476550)
[cin.xx()在编译器下有不兼容情况]( https://codeantenna.com/a/xHXDWXHssh
)
* 一般情况下推荐使用getchar()手动清空缓存
```cpp=
if (cin.fail()) {
//让错误标识改回为0,让我们可以继续输入
cin.clear();
// 有些編譯器不兼容
//cin.sync();
//会导致输入几个错误值就循环几次
//cin.ignore();
//推荐手动
cin.get();
}
```
:::success
:::
:::warning
## [sort](https://shengyu7697.github.io/std-sort/)
* 引入標頭檔 algorithm
* 排序vector 能用函數過載 或 lambda 函式[uva11729](https://)
:::
:::warning
# [include <thread>](https://shengyu7697.github.io/std-thread/)
* [Program & Process & Thread](https://medium.com/erens-tech-book/%E7%90%86%E8%A7%A3-process-thread-94a40721b492)
* [注意!在多執行緒程式中常常會互相存取某段程式碼、某個函式、某個變數,需要對這些程式碼進行上鎖,以確保同一時間只有某一個執行緒能進行存取。](https://shengyu7697.github.io/std-mutex/)
* 在 main 主執行緒建立 t1 執行緒後,主執行緒便繼續往下執行,
如果主執行緒需要等 t1 執行完畢後才能繼續執行的話就需要使用 join,
* 如果主執行緒不想等或是可以不用等待 t1 執行緒的話,
就可以使用 detach 來讓 t1 執行緒分離,接著主執行緒就可以繼續執行,t1執行緒也在繼續執行
* 在整個程式結束前最好養成好習慣確保所有子執行緒都已執行完畢
:::
:::warning
## [cin、cin.get()、cin.getline()、getline()](https://blog.csdn.net/qq_40563761/article/details/106113123)
* cin.get():因为get不忽略分隔符,容易影響輸入
* cin.getline():cin.getline(接受字符串的字符串地址,接受个数,结束字符) ,当第三个参数省略时,系统默认为’\0’
* getline():string用的
:::
:::warning
## [memset()](https://www.itread01.com/hkhkphkpq.html)
* #include <cstring>
* memset是按位元組進行賦值
memset(a,1,sizeof(a)); // 這時候你去列印a的每一位,值都是16843009,而非你期望的1
原因:int為四個位元組,這時候每個位元組被memset置為1,而每一個元素都是一個int,即 01 01 01 01,讀取十六進位制為 0x01010101,十進位制為 16843009
不過 char型別的可以使用memset隨意賦值為自己想要的,因為 char只佔一個位元組,賦多少就是多少。
:::
:::warning
## [array, vector, deque; queue, stack](https://zhuanlan.zhihu.com/p/381045908)
:::