C++, C Memo

1. Annoying Chores

static_cast vs. dynamic_cast: 資料類型轉換

static_cast <type> ( expression )
//將 expression 轉換為type的類型

dynamic > runtime 才去 cast
static > compile time 就幫你試過

setw: 向右對齊 (non-sticky)

#include <iomanip>
cout << setw(字元格子寬度) << "字元";  
//字元會右貼,但超過寬度沒反應

其他所有Manipulators都是sticky

setprecision: 總有效位數

#include <iomanip>
cout  <<  setprecision(輸出總位數)  <<  變數/數字;
//會一直影響後面輸出

fixed+setprecision: 小數點後位數

#include <iomanip>
cout << fixed << setprecision(輸出小數位數) << 變數/數字; 
//可用cout.unsetf( ios::fixed )關閉fixed
//但setprecision關不掉  -->sticky
//cout物件被setprecision sticky
//hex可以用dec解除

Binary Operation: 位元運算

位元AND運算(&), 位元OR運算(|), 位元XOR運算(^)

將原先的資料型別換成二進位表示之後再做運算

fstream 用法

https://hackmd.io/s/ry4GNk78-

to_string 沒定義 ( int -> string )

error: 'to_string' was not declared in this scope

自己寫:

std::string to_string(int i) { std::stringstream ss; ss << i; return ss.str(); }

string char* 互換

//char* <- string char *charname = stringname.c_str(); //string <- char string stringname(charArrayName); //or string stringname = charArrayName; // assign directly to a string.

stringstream is empty?

check if myStream.rdbuf()->in_avail() == 0

https://stackoverflow.com/questions/8046357/how-do-i-check-if-a-stringstream-variable-is-empty-null

cin/ cin.get()/ cin.getline()

  • cin 的结束符有:[enter] [space] [tab]
  • cin.get()的结束符只有:[enter],但会把enter放入队列等待
  • cin.getline()的结束符也只有:[enter],但不会把enter放入队列

cin.get() 有三种格式
无参,一参数,二参数
cin.get() , cin.get(char ch) , cin.get(array_name,Arsize)

cin.getline()只有一种格式
cin.getline(array_name,Arsize)

\r\n 問題

Unix系統->“\n”;
Windows系統>“\r\n”;
Mac系統->“\r”。

一個直接後果是,Unix/Mac系統下的文件在Windows裡打開的話,所有文字會變成一行;
而Windows裡的文件在Unix/Mac下打開的話,在每行的結尾可能會多出一個^M符號。

Stand Input Output 標準輸出輸入

  • printf() 執行後會return所輸出的字元數
  • 標準輸出可以用 > 將輸出結果導向至指定檔案
filename > result.txt
  • 執行會將print出的東西導向至 result.txt,螢幕上不再顯示。
  • 重新導向標準輸出用 > ,標準輸入則是 <
  • 附加重導標準輸出用 >> ,效果同append
format specifier meaning
%c char
%d 10 進位 int
%o 8 進位 int
%u unsign int
%x, %X 將int以 16 進位方式輸出
(x大小寫決定英文字輸出大小寫)
%f float
%e, %E 用科學記號顯示float
(e大小寫決定英文字e輸出大小寫)
%g, %G 等同使用 %f 或 %e(%f 或 %E)
兩方案電腦自行判斷哪個表示精簡
%% 顯示 %
%s string
%lu long unsigned 型態的整數
%p pointer
  • 輸出浮點數指定精度:
    printf("example:%.2f\n", 19.234);
    %.2f 指定小數點後取兩位

  • 靠右對齊:
    printf("example:%6.2f\n", 19.234);
    %6f 預留 6 個字元寬度

  • 靠左對齊:
    printf("example:%-6.2f\n", 19.234);
    加負號

  • 預留的寬度想用變數:
    printf("example:%*f\n", x, 19.234);
    用*

  • scanf()

int input;
printf("請輸入數字:");
scanf("%d", &input);

若已是字元陣列就不用 & 取址

  • regex效果:
int number1, number2;

printf("請再輸入兩個數字,中間使用-號區隔):");
scanf("%d-%d", &number1, &number2);
printf("您輸入的數字:%d-%d\n", number1, number2);
char str[50];

printf("請輸入 1 到 5 的字元:");
scanf("%[1-5]", str);
printf("輸入的字元為 %s\n", str);

fflush(stdin); // 清除輸入緩衝區

printf("請輸入 XYZ 任一字元:");
scanf("%[XYZ]", str);
printf("輸入的字元為 %s\n", str);
  • scanf() 函式連續讀入符合集合的字元並放到字元陣列中,直到讀到不符合的字元為止,剩下的字元仍會存在輸入緩衝區中
  • 可以直接使用 fflush(stdin) 清除輸入緩衝區
  • scanf() 函式若成功,則傳回成功填寫的格式指定字數目,否則直接略過輸入而傳回 0

https://openhome.cc/Gossip/CGossip/PrintfScanf.html

2. Ostream Operator Overloading

重載資料流運算子

friend ostream &operator<<(ostream& orz,const jector &in){
        orz<<"("<<in.x<<","<<in.y<<")"<<endl;
        return orz;
    }

3. const成員函數

若Object是宣告為const,則只能執行const member function

void func( ) const;

default argument 只能出現一次

4. Proxy Class

interface(int a):ptr(new implmentation(a)){
//empty
}

5. uint8_t, uint16_t, uint32_t, uint64_t

uint8_t  義同 unsigned char
uint16_t 義同 unsigned short
uint32_t 義同 unsigned int
uint64_t 義同 unsigned long long

https://pinglinblog.wordpress.com/2014/01/16/ctype-uint8_t-uint16_t-uint32_t-uint64_t/

6. C/C++ typedef , struct , typedef struct

Sun, Aug 13, 2017 12:37 PM

typedef: 型別取名

typedef int Jack;  // 幫int取名叫Jack
Jack age;          // age的型別是 int

struct: 結構

struct family{
int Ken;
float Sam;
};     //別忘了結尾要加分號

C和C++在宣告結構的變數時不同

  • C比較嚴謹:
struct family Chen;    //Chen的型別是family這個結構
                       //family的型別是struct
  • C++:
family Chen;             //Chen的型別是family這個結構

宣告結構時一起宣告變數 C和C++沒差

struct family{
int Ken;
float Sam;
}; Chen           //Chen的型別是family這個結構
  • 呼叫成員
    Chen.Ken=10; //設定Ken=10;

typedef struct

  • 結合 typedef 和 struct直接幫結構命名,C和C++都一樣:
typedef struct family{
int Ken;
float Sam;
}Asia;    //把family這個結構命名為Asia
  • 建立family的結構變數:
Asia Chen;    //沒加typedef之前,要用family Chen宣告 
  • 呼叫成員
    Chen.Sam=10.5;

polymorphism 也可以傳reference
base class 的 destructor 前面要加virtual

http://vincecc.blogspot.tw/2013/10/cc-typedef-struct-typedef-struct.html

查看我的其他筆記 — — 回到 Jexus.md 首頁