"std::endl" vs "\n"
基本上兩者在換行的效果沒有不同,唯一的不同是 std::endl 會 flush output buffer。若你不想頻繁地 flush output 用 "\n",若你想看所有 output ( 程式不穩定 ) 則用 std::endl 。
參考資料 :
int a = 5;
int &b = a;
std::cout << "a value\t\t" << a << "\n";
std::cout << "a address\t" << &a << "\n";
std::cout << "b value\t\t" << b << "\n";
std::cout << "b address\t" << &b << "\n";
int c = 50;
b = c;
std::cout << "a value\t\t" << a << "\n";
std::cout << "a address\t" << &a << "\n";
std::cout << "b value\t\t" << b << "\n";
std::cout << "b address\t" << &b << "\n";
std::cout << "c value\t\t" << c << "\n";
std::cout << "c address\t" << &c << "\n";
a value 5 a address 0x61ff08 b value 5 b address 0x61ff08 a value 50 a address 0x61ff08 b value 50 b address 0x61ff08 c value 50 c address 0x61ff04
左值 (lvalue) : 一個佔據某個特定記憶體的值。 右值 (rvalue) : 一個 expression 結束後就消失的值。 基本上這兩個定義包含了全部的值,非左即右,非右即左。
int var = 4; // var 參數佔據記憶體 = lvalue
4 = var; // 4 不佔據記憶體 = rvalue
(var + 1) = 4; // var + 1 也不佔據記憶體 = rvalue
以上 2 跟 3 行會報錯,lvalue required as left operand of assignment。表示這個賦值程式碼錯在給不存在的記憶體空間賦值。所以只要是 lvalue 就能賦予值,即使是 function 也一樣,下面的 function 引用一個左值 ( globalvar ),所以可以賦予值。
int globalvar = 20;
int &foo() {
return globalvar;
}
int main() {
foo() = 10;
std::cout << globalvar; // 10
return 0;
}
int a = 1;
int b = a + 1; // 這裡的 a 被直接視為右值
int arr[] = {a, b};
int *p = arr;
*(p + 1) = 10; // 利用 * 將右值 (p + 1) 傳換成左值
const int index = 5;
index = 8; // error: assignment of read-only variable 'index' // const pointer
char * const constptr = initptr;
*constptr = 1; // 改變其 value OK
constptr = otherptr; // 改變其 addr Error
// const value
const char * constval = "const value";
*constval = "nomal value"; // 改變其 value Error
constval = otherptr; // 改變其 addr OK
// const pointer & const value
const char * const consteverything = "ulti - const";在 C++ 可能常常看到 void function (type_Name const&); "type_Name const&" 是可以寫成 "const type_Name &"
int i = 10;
const int &r = i; // reference to const
++i; // i 仍然可以修改
++r; // r 不行 (expression must be a modifiable lvalue)
通常追求效率的 C++ 工程師,會選擇 pass by reference 來防止程式做不必要的 Copy,這時 const 通常就會出現,來確保原始 value 不會被亂改。且 const 的出現可以使 call function 時,使用 rvalue。
std::vector mySort(std::vector const &str)
{
std::vector r(str);
std::sort(r.begin(), r.end());
return r;
}
mySort({"abc", "def", "xyz"});
Callback Function : A 跟 B 講 "看到 C 就打電話給我" 打電話給 A = Callback Functionmain.cpp ( A )
int
main () {
// 從 0 數到 10000000,中間請回報進度 % 數
zeroTo(1000000, ShowPercentage);
return 0;
}
zeroTo ( B ) 每數一個數字 ( 看到 C ) void
zeroTo (size_t targetNumber, CallBackFuncPtr func)
{
for (size_t i = 0; i < targetNumber; i++){
func((i * 100) / targetNumber);
}
}
ShowPercentage ( 打電話給 A ) typedef void (*CallBackFuncPtr)(size_t);
void ShowPercentage(size_t percentage)
{
std::cout << "\rProcessing :\t" << percentage << "%";
}
Usage :
my_program tcp <host> <port> [--timeout=<seconds>]
my_program serial <port> [--baud=9600] [--timeout=<seconds>]
my_program (-h | --help)
| 參數 | |
| my_program | : program name |
| tcp | : commands |
| -h, --help | : options |
| <host> | : arguments |
| 符號 | |
| [ ] | : optional |
| ( ) | : required |
| | | : mutually exclusive |
| ... | : repeating elements |