正文
自定義數據結構
在函數內部自定義數據結構 :
struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; void testSales_data() { Sales_data data1, datda2; double price = 0; cin >> data1.bookNo >> data1.units_sold >> price; data1.revenue = data1.units_sold * price; }
在函數體外定義類 : 在.h頭文件聲明
命名空間的using聲明 :
using聲明 :
using namespace std; using std::cin;
string初始化 : 直接初始化 , 拷貝初始化
void testinitializeString() { string s1; string s2 = s1; string s3 = "hiya"; string s4(10, 'c'); // s4 的內容是 cccccccccc string s5 = "hiya"; string s6("hiya"); //直接初始化 }
string 讀寫操作熟練代碼 :
void testReadString() { string word; while (cin >> word) { cout << word << endl } string line; while (getline(cin, line)) { if (!line.empty) { cout << line << endl } } }
string 對象比較 : 除了常規的== 和 != , 這裏C++ 還有< , >等關係運算符來比較長度
- string 對象相加 : 字面值不能直接相加
string 字符處理 : 在cctype頭文件中定義了一組標準庫函數來處理.
標準庫類型vector : 類似於java的arrylist , 有下標 , 可以添加.
- vector 的初始化 :
void testInitializeVector() { vector<string> v1{ "a","b","c","d","e" }; vector<string> v1(10, "hi"); // 列表裏面10個 "hi" }
- vector 的初始化 :
迭代器 :
遍歷 : 與 java 遍歷略有不同
void testIterator() { string s("hello world"); for (auto it = s.begin; it != s.end && !isspace(*it); ++it) { //do something } }
迭代器類型 :
void testIterator() { vector<int>::iterator it; //可讀寫vector裏面的元素 vector<int>::const_iterator it1; //只能讀 string::iterator it2; //可讀寫string中的字符 string::const_iterator it3; //只能讀 }
補充
typedef : 定義新的類型 , 一般作用於 指針函數 或 struct
http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.htmlextern : 在C++中使用c語言的函數,需要使用extern “C“編譯才能過 .
http://blog.csdn.net/puppet_master/article/details/47681849C/C++預處理指令 : #define,#ifdef,#ifndef,#endif
http://www.cnblogs.com/zi-xing/p/4550246.html指針函數 和 函數指針 : http://www.cnblogs.com/gmh915/archive/2010/06/11/1756067.html
(1) 指針函數 : 首先它是一個函數,只不過這個函數的返回值是一個地址值。函數返回值必須用同類型的指針變量來接受
(2) 函數指針 : 指向函數的指針變量,即本質是一個指針變量
c/c++ 宏的使用 : http://blog.csdn.net/cshichao/article/details/9340885
a. 可以對C語法做小的修改
b. 控制條件編譯
c. 帶參數的宏 相當於定義的一個方法
d. ##運算符 , 表示連接符attribute((unused)) : 消除未使用參數警告 ;
http://www.cnblogs.com/coding-my-life/p/4175761.htmlclass 與 struct 在 C 和 C++ 中的區別 :
http://blog.csdn.net/yangzhiloveyou/article/details/8536793