柚子快報激活碼778899分享:C++全系列學(xué)習(xí)(基礎(chǔ)篇)
一、基礎(chǔ)語法
1、數(shù)據(jù)類型
基本數(shù)據(jù)類型:
int用于表示整數(shù)float用于表示單精度浮點數(shù)double用于表示雙精度浮點數(shù)char用于表示字符bool用于表示布爾值,只能取 true 或 falsevoid表示無類型,通常用于函數(shù)返回類型或指針類型
復(fù)合數(shù)據(jù)類型:
數(shù)組由相同類型的元素組成的集合結(jié)構(gòu)體(struct)由不同類型的成員組成的復(fù)合類型枚舉(enum)一組具有命名值的常量類(class)一種封裝數(shù)據(jù)和操作的方式,支持面向?qū)ο缶幊讨羔槪╬ointer)存儲內(nèi)存地址的變量,用于間接訪問內(nèi)存中的數(shù)據(jù)引用(reference)提供現(xiàn)有變量的別名,用于簡化代碼和避免復(fù)制
引例:
int a = 10;
cout< //科學(xué)計數(shù)法 float a = 3e2; //表示3*10^2 float a = 3e-2; //表示3*10^-2 //字符串 a = 'b' a = "b" a = "hello" //單個字符時可用''或"",字符串只能用"" //轉(zhuǎn)義字符 cout<<"hello\n"< cout<<"aa\thello"< cout<<"aaaa\thello"< cout<<"a\thello"< 2、運算符 算數(shù)運算符 /可以是兩個小數(shù)相除,若是兩個整數(shù)相除則結(jié)果向下取整 %為取模 遞增遞減 ++前置遞增a=2,b=++aa=3,b=3++后置遞增a=2,b=a++a=3,b=2--前置遞減a=2,b=--aa=1,b=1--后置遞減a=2,b=a--a=1,b=2 所謂前置就是先加1,后置就是先運算 邏輯運算符 運算符術(shù)語示例結(jié)果!非!a若a為假則!a為真&&與a&&b若a與b都為真則為真,否則為假|(zhì)|或a||b若a與b有一個為真則為真,否則為假 3、數(shù)組 一維數(shù)組 3種定義方式: 1)數(shù)據(jù)類型 數(shù)組名[數(shù)組長度]; 2)數(shù)據(jù)類型 數(shù)組名[數(shù)組長度] = {值1,值2,……} 注意:若實際值與長度不同時,自動用0補(bǔ)足 3)數(shù)據(jù)類型 數(shù)組名[] = {值1,值2,……} int arr[10]; arr[0] = 1; int arr2[4] = {1,2,3,4}; int arr3[] = {1,2,3}; 獲取首地址 兩種方式:arr或&arr[0] 數(shù)組長度統(tǒng)計 數(shù)組占用內(nèi)存空間大?。簊izeof(arr) 數(shù)組單個元素占用內(nèi)存空間大小:sizeof(arr[0]) 數(shù)組長度:sizeof(arr) / sizeof(arr[0]) 冒泡排序(后文具體介紹) 比較相鄰元素: 從第一個元素開始,比較相鄰的兩個元素,如果它們的順序不正確(比如在升序排序中,前一個元素大于后一個元素),則交換它們。 一次遍歷完成: 經(jīng)過一次遍歷,最大(或最?。┑脑貢灰频叫蛄械淖詈笪恢?。 重復(fù)步驟1和2: 重復(fù)進(jìn)行上述步驟,直到序列完全有序。在每次遍歷中,待排序序列的長度減一,因為每次遍歷都會將一個元素放置到了正確的位置上 二維數(shù)組 定義方式 二維數(shù)組定義的4種方式: 1)數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ]; 2)數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = { {數(shù)據(jù)1,數(shù)據(jù)2} ,{數(shù)據(jù)3,數(shù)據(jù)4} }; 3)數(shù)據(jù)類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4}; 4)數(shù)據(jù)類型 數(shù)組名[ ][ 列數(shù) ] = { 數(shù)據(jù)1,數(shù)據(jù)2,數(shù)據(jù)3,數(shù)據(jù)4} 注意:常用第二種,可讀性較強(qiáng) int array2D[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; cout << "Array elements:" << endl; for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { cout << array2D[i][j] << " "; } cout << endl; } Array elements: 1 2 3 4 5 6 7 8 9 計算行數(shù)與列數(shù) 二維數(shù)組的行數(shù):sizeof(arr) / sizeof(arr[0]) 二維數(shù)組的列數(shù):sizeof(arr[0]) / sizeof(arr[0][0]) 地址 二維數(shù)組首地址:arr[0]?或?&arr[0][0] 二維數(shù)組第1個元素的地址:?arr[0]?或?&arr[0][0] 二維數(shù)組第 0 行的地址:?arr或arr[0]或arr + 0? 二維數(shù)組第 i 行的地址:arr[i]或arr + i 二維數(shù)組第 i 行首元素的地址:arr[i]或arr + i或*(arr + i)或&a[0] + i 二維數(shù)組第 i 行第 j 列元素的地址:&arr[i][j]或*(arr + i) + j 4、循環(huán) for循環(huán) #include using namespace std; int main () { int sum=0; for (int i=1; i<=100 ; ++i) sum+=i; cout< return 0; } while循環(huán) while(表達(dá)式)語句; while(表達(dá)式){ ? ? 語句1; ? ? 語句2; } 例:用while打印數(shù)組 #include using namespace std; int main() { int array[] = {1, 2, 3, 4, 5}; int length = sizeof(array) / sizeof(array[0]); // 計算數(shù)組的長度 cout << "Array elements: "; int i = 0; // 初始化計數(shù)器 while (i < length) { cout << array[i] << " "; i++; // 更新計數(shù)器 } cout << endl; return 0; } do-while循環(huán) do{ ? ? 語句1; ? ? 語句2; } while(條件表達(dá)式) 示例:獲取輸入,直到輸入的值為正數(shù)為止 #include using namespace std; int main() { int num; do { cout << "請輸入一個正數(shù):"; cin >> num; } while (num <= 0); cout << "你輸入的正數(shù)是:" << num << endl; return 0; } while和do-while的區(qū)別: while 循環(huán): 在 while 循環(huán)中,循環(huán)條件會在每次循環(huán)開始之前被檢查。如果條件為真,循環(huán)體會執(zhí)行,然后再次檢查條件。如果條件為假,循環(huán)終止。這意味著,如果條件一開始就為假,循環(huán)體可能一次都不會執(zhí)行。 do-while 循環(huán): 在 do-while 循環(huán)中,循環(huán)體會先執(zhí)行一次,然后再檢查循環(huán)條件。只要條件為真,循環(huán)體會繼續(xù)執(zhí)行,否則循環(huán)終止。這意味著,do-while 循環(huán)至少會執(zhí)行一次循環(huán)體,即使條件一開始就為假。 嵌套循環(huán) 示例:水仙花數(shù)字 #include using namespace std; int main() { for (int a=1; a<=9; ++a) for (int b=0; b<=9; ++b) for (int c=0; c<=9; ++c) { if (a*a*a+b*b*b+c*c*c==a*100+b*10+c) cout< } return 0; } 5、條件語句(if) 單行格式if #include using namespace std; int main() { int num; cout << "請輸入一個整數(shù):"; cin >> num; if (num > 0) { cout << "這是一個正數(shù)。" << endl; } return 0; } 多行格式if #include using namespace std; int main() { int num; cout << "請輸入一個整數(shù):"; cin >> num; if (num > 0) { cout << "這是一個正數(shù)" << endl; } else if (num == 0){ cout << "0" << endl; } else{ cout << "這是一個負(fù)數(shù)" << endl; } return 0; } 6、結(jié)構(gòu)體 語法 struct 結(jié)構(gòu)體名 { 結(jié)構(gòu)體成員列表 }; 結(jié)構(gòu)體創(chuàng)建變量 #include using namespace std; // 定義一個名為 Person 的結(jié)構(gòu)體 struct Person { string name; int age; double height; }; int main() { // 創(chuàng)建一個 Person 類型的對象 struct Person person1; // 給結(jié)構(gòu)體成員賦值 person1.name = "Alice"; person1.age = 30; person1.height = 5.6; // 輸出結(jié)構(gòu)體成員的值 cout << "Name: " << person1.name << endl; cout << "Age: " << person1.age << endl; cout << "Height: " << person1.height << endl; struct Person person2 = {"BEN",40,4.4}; return 0; } 結(jié)構(gòu)體數(shù)組 #include #include using namespace std; // 定義一個名為 Person 的結(jié)構(gòu)體 struct Person { string name; int age; double height; }; int main() { // 使用大括號初始化結(jié)構(gòu)體數(shù)組 Person people[3] = { {"Alice", 30, 5.6}, {"Bob", 25, 6.0}, {"Charlie", 35, 5.9} }; // 輸出結(jié)構(gòu)體數(shù)組中每個結(jié)構(gòu)體的成員值 for (int i = 0; i < 3; ++i) { cout << "Person " << i+1 << ":" << endl; cout << "Name: " << people[i].name << endl; cout << "Age: " << people[i].age << endl; cout << "Height: " << people[i].height << endl; cout << endl; } return 0; } 結(jié)構(gòu)體指針 #include using namespace std; // 定義一個名為 Person 的結(jié)構(gòu)體 struct Person { string name; int age; double height; }; int main() { // 創(chuàng)建一個 Person 類型的結(jié)構(gòu)體指針 struct Person* p = new Person; // 使用 -> 運算符給結(jié)構(gòu)體成員賦值 p->name = "Alice"; p->age = 30; p->height = 5.6; // 使用 -> 運算符訪問結(jié)構(gòu)體成員并輸出 cout << "Name: " << p->name << endl; cout << "Age: " << p->age << endl; cout << "Height: " << p->height << endl; // 記得釋放動態(tài)分配的內(nèi)存 delete p; return 0; } 結(jié)構(gòu)體嵌套 結(jié)構(gòu)體中成員是另一個結(jié)構(gòu)體 7、函數(shù) 主函數(shù)為main(),但可以創(chuàng)作其他函數(shù)進(jìn)行運算,只需在主函數(shù)中調(diào)用即可,以下以階乘為例 #include using namespace std; int factorial(int n){ if(n==1) return n; else return n*factorial(n-1); } int main() { int n; cout << "請輸入整數(shù):" << endl; cin >> n; cout << "整數(shù):" << n << "的階乘為:" << factorial(n) << endl; cout << "\n" << endl; return 0; } 二、STL庫 STL(Standard Template Library)是 C++ 標(biāo)準(zhǔn)庫的一部分,它提供了一組通用的模板類和函數(shù),用于實現(xiàn)常見的數(shù)據(jù)結(jié)構(gòu)和算法。STL 的設(shè)計旨在提供一種高效、靈活和可重用的方法來處理數(shù)據(jù)結(jié)構(gòu)和算法問題??偟膩碚f,STL庫裝了許多算法和組件,包含多種函數(shù),可用于開發(fā)各類應(yīng)用程序 1、快速排序(Sort) 基本用法 格式: void sort(起始地址,結(jié)束地址,比較函數(shù)); #include #include using namespace std; int main(){ int a[] = {2,3,5,4,1,8,6,9}; sort(a,a+8);//a為數(shù)組的開頭,a+8就等于排序到數(shù)組的第8個元素 for(int i=0;i<6;i++) cout< } cmp用法 在sort比較函數(shù)中傳入排序函數(shù),>為降序,<為升序 #include #include using namespace std; bool cmp(int x,int y) { if(x>y)return true;//降序 return false; } int main(){ int a[] = {2,3,5,4,1,8,6,9}; sort(a,a+8,cmp);//a為數(shù)組的開頭,a+8就等于排序到數(shù)組的第8個元素 for(int i=0;i<6;i++) cout< } 2、關(guān)聯(lián)容器(Map) map是一個關(guān)聯(lián)容器,它提供了一種將鍵與值關(guān)聯(lián)起來的方式,map中的每個元素都是一個鍵(key-value pair),其中鍵(key)是唯一的,值(value)則可以不唯一。它基于紅黑樹(Red-Black Tree)實現(xiàn)。 注意:map不允許容器中有重復(fù)的key值,multimap允許容器中有重復(fù)的key 格式: map(數(shù)據(jù)類型,數(shù)據(jù)類型)一個自定義的名稱; #include #include int main() { map phonebook["Alice"] = 123456; phonebook["Bob"] = 789012; phonebook["Charlie"] = 345678; cout << "Bob's phone number: " << phonebook["Bob"] << endl; phonebook["Alice"] = 111111; phonebook.erase("Charlie"); // 遍歷元素 for (const auto& pair : phonebook) { cout << pair.first << "'s phone number: " << pair.second < } return 0; } 1)構(gòu)造 map 2)賦值 map& operator=(const map &mp);重載等號操作符 3)大小和交換 size(); 返回容器中元素數(shù)目empty();判斷容器是否為空swap(st);交換兩個集合容器 4)插入和刪除 insert(elem);在容器中插入元素clear();清除所有元素erase(pos);刪除pos迭代器所指的元素,返回下一個元素的選代器erase(beg,end);刪除區(qū)間[beg,end)的所有元素,返回下一個元素的迭代器erase(elem);刪除容器中值為elem的元素。 5)查找和統(tǒng)計 find(key);查找是否存在key,若存在返回該鍵的元素的迭代器,若不存在,返回set.end()count(key);統(tǒng)計key的元素的個數(shù) 3、棧(stack) 相當(dāng)于是一個小箱子,每次向箱子頂部塞入數(shù)據(jù),遵循先進(jìn)后出(Last In, First Out,LIFO)的原則。??梢员豢醋魇且环N容器,其中元素按照后進(jìn)先出的順序進(jìn)行插入和刪除。 格式: #include stack<數(shù)據(jù)類型>一個自定義的名字; 或: stack 一個自定義的名字; 棧的成員函數(shù) .empty()判斷棧是否為空,空則返回true.push(…)在棧頂增加元素.pop()在棧頂移除元素.top()返回棧頂元素.size()返回棧的元素數(shù)量 代碼示例: #include #include using namespace std; stack int main(){ st.push(1); st.push(2); st.push(3); cout< return 0; } 輸出: 3 4、動態(tài)數(shù)組容器(Vector) 使用的時候可以看作數(shù)組,但他相對于數(shù)組來說可以動態(tài)擴(kuò)展,增加長度 1)構(gòu)造 頭文件:#include 構(gòu)造:vector 放入數(shù)據(jù):v.push_back(…); 迭代器:vector 將[v.begin(),v.end())區(qū)間中的元素拷貝給本身:vector(v.begin(),v.end()); 將n個elem拷貝給本身:vextor(n,elem); 拷貝構(gòu)造函數(shù):vector(const vector &v) ; void printVector(vector { //利用迭代器打印 v for (vector { cout << *it << " "; } cout << endl; } void text01() { vector for (int i = 0; i < 5; ++i) { v1.push_back(i);//向v1末尾添加數(shù)據(jù) } vector vector vector cout << "打印v2: "; printVector(v2); cout << "打印v3: "; printVector(v3); cout << "打印v4: "; printVector(v4); } 2)遍歷 v.begin()返回迭代器,這個迭代器指向容器中第一個數(shù)據(jù)v.end()返回迭代器,這個迭代器指向容器元素的最后元素的下一個位置vector 第一種遍歷方式 vector vector while(pBegin != pEnd){ cout<<*pBegin< pBegin++; } 第二種遍歷方式 for (vector cout<<*it< } 3)賦值 vector& operator=(const vector &v);?重載賦值運算符assign(v.begin(),v.end());將[v.begin(),v.end())區(qū)間中的元素賦值給本身assign(n,elem);將n個elem賦值給本身 void printVector(vector { //利用迭代器打印 v for (vector { cout << *it << " "; } cout << endl; } void text02() { vector for (int i = 0; i < 5; ++i) { v1.push_back(i); } v2 = v1; vector v3.assign(v1.begin(), v1.end()); v4.assign(10,100); cout << "打印v2: "; printVector(v2); cout << "打印v3: "; printVector(v3); cout << "打印v4: "; printVector(v4); } 4)容量和大小 empty();判斷容器是否為空capacity();容器容量size();容器中元素個數(shù)resize(int num);重新指定長度num,變短刪末尾元素,變長以默認(rèn)值填充新位置resize(int num,elem);重新指定長度num,變短刪末尾元素,變長以elem填充新位置 5)插入和刪除 push_back(elem);尾部插入元素elempop_back();刪除最后一個元素insert(const_iterator pos,elem);指向的位置pos處插入一個元素eleminsert(const_iterator pos,int count,elem);指向的位置pos處count元素elemerase(const_iterator pos);指向的位置pos處刪除元素erase(const_iterator start,const_iterator end); 刪除start到end之間的元素 clear();清空所有元素 6)數(shù)據(jù)存取 at(int id);返回id處數(shù)據(jù) operator[]; 返回[]處數(shù)據(jù)front();返回第一個數(shù)據(jù)back();返回最后一個數(shù)據(jù) 7)互換容器 swap(vec);將vec與本身元素互換 v1.swap(v2) 8)預(yù)留空間 reserve(int len);預(yù)留len個元素長度 當(dāng)數(shù)據(jù)量較大時可以一開始就利用reserve預(yù)留空間 5、String string內(nèi)部封裝了許多成員方法,如find,copy,delete,insert,replace等 頭文件:#include 1)構(gòu)造 string();構(gòu)造空字符串string(const char* s);拷貝s所指向的字符串序列,使字符串s初始化string(const char* s, size_t n);拷貝s所指向的字符串序列的第n個到結(jié)尾的字符string(size_t n, char c);將字符c復(fù)制n次string(const string& str);拷貝構(gòu)造函數(shù)。使用一個string對象初始化另一個對象string(const string& str, size_t pos, size_t n = npos);拷貝s中從pos位置起的n個字符,若npos>字符串長度,就拷貝到字符串結(jié)尾結(jié)束 #include void test1(){ string s1; //創(chuàng)建空字符串,使用無參構(gòu)造函數(shù) const char*str = "hello"; string s2(str); string s3(s2); //調(diào)用拷貝構(gòu)造函數(shù) string s4(10,'a'); //將字符a復(fù)制10次 string s5(s2,1);//拷貝s2所指向的字符串序列的第1個到結(jié)尾的字符 } 2)賦值 string& operator=(const char* s);char*類型字符串賦值給當(dāng)前的字符串string& operator=(const string &s);把字符串s賦給當(dāng)前的字符串string& operator=(char c);字符賦值給當(dāng)前的字符串string& assign(const char *s);把字符串s賦給當(dāng)前的字符串string& assign(const char *s, int n);把字符串s的前n個字符賦給當(dāng)前的字符串string& assign(const string &s);把字符串s賦給當(dāng)前字符串string& assign(int n, char c);用n個字符c賦給當(dāng)前字符串 3)字符串插入、拼接和刪除 插入: void push_back (char c);向字符串末尾追加一個字符 string& insert(size_t pos, const string& str); 插入字符串拷貝string& insert (size_t pos, const char* s);插入c形式的字符串string& insert (size_t pos, const char* s, size_t n);將字符串s的前n個字符插到pos位置 #include #include using namespace std; int main() { string s0(""); s0.push_back('a');//s0尾插入a string s1("b"); s0.insert(1, s1);//在下標(biāo)為1的位置插入s1的拷貝 s0.insert(4, "c");//在下標(biāo)為4的位置插入字符串o s0.insert(0, "hello",2);//在下標(biāo)為0的位置插入"hello"的前2個字符 return 0; } 拼接: string& operator+=(const char* str);重載+=操作符string& operator+=(const char c);重載+=操作符string& operator+=(const string& str);重載+=操作符string& append(const char *s);把字符串s連接到當(dāng)前字符串結(jié)尾string& append(const char *s, int n);把字符串s的前n個字符連接到當(dāng)前字符串結(jié)尾string& append(const string &s);同operator+"(const string& str)string& append(const string &s,int pos,int n); 字符用s中從pos開始的n個字符連接到字符串結(jié)尾 刪除: string& erase(int pos, int n = npos);刪除從pos開始的第n個字符 str.erase(1,3); //從1號位置開始刪除3個字符 4)查找和替換 int find(const string& str, int pos = 0) const;查找str最后一次位置,從pos開始查找int find(const char*s, int pos =0)const;查線s第一次出現(xiàn)位置,從pos開始查找int find(const char* s, int pos, int n) const;從pos位置查找s的前n個字符第一次位置int find(const char c, int pos=0) const;查找字符c第一次出現(xiàn)位置int rfind(const string& str, int pos = npos) const;查找str最后一次位置,從pos開始查找int rfind(const char* s, int pos = npos) const;查找s最后一次出現(xiàn)位置.從pos開始查找int rfind(const char* s, int pos, int n) const;從pos查找s的前n個字符最后一次位置int rfind(const char c, int pos = 0) const;查找李符c最后一次出現(xiàn)位置string& replace(int pos, int n, const string& str);替換從pos開始n個字符為字符串strstring& replace(int pos, int n, const char* s);替換從pos開始的n個字符為字符串s 注意: find是從左往右查找,rfind是從右往左查找 find找到字符串后返回查找的第一個字符位置,找不到就返回-1 replace在替換時,要指定從哪個位置起,替換多少個字符,替換成什么 5)字符串的比較 int compare(const string &s) const;與字符串s比較int compare(const char *s) const:與字符串s比較 注意:若=則返回0,>返回1,<返回-1 int main(){ string s1="hello"; string s2="hhllo"; int ret = s1.compare(s2); …… return 0; } 6)字符串存取 char& operator[](int n);通過[]方式取字符char& at(int n);通過at方式取字符 int main(){ string str = "hello world" for (int i = 0;i < str.size();i++){ cout << str[i] << " "; cout << str.at(i) << " "; } cout< } //字符修改: str[0] = "x"; str.at(1) = "x"; return 0; } 7)子串 string substr(int pos=0,int n = npos) const;返回由pos開始的n個字符組成的字符串 void teste1(){ string str ="abcdefg"; string substr = str.substr(1,3) cout<<"substr ="<< substr << endl; string email="hello@sina.com"; int pos = email.find("@"); string username =email.substr(0, pos); cout<<"username:"< } int main(){ teste1(); system("pause"); return0; } 6、Set 基本概念:以有序的方式存儲一組唯一的元素。具體來說,std::set使用紅黑樹(Red-Black Tree)實現(xiàn),這使得元素在插入時就會按照特定的順序進(jìn)行排序,并且保證了查找、插入和刪除操作的高效性能。 注意:set不允許容器出現(xiàn)重復(fù)元素,multiset允許容器出現(xiàn)重復(fù)元素 頭文件:#include 遍歷: #include void printSet(set for(set cout<<*it<<" "; } cout< } 1)構(gòu)造和賦值 set set s1.insert(10); s1.insert(10); set set s3=s2; //賦值 2)大小和交換 size(); 返回容器中元素數(shù)目empty();判斷容器是否為空swap(st);交換兩個集合容器 set s1.insert(10); s1.insert(20); s1.insert(30); set s1.insert(40); s1.insert(50); s1.insert(60); s1.swap(s2); 3)插入和刪除 insert(elem);在容器中插入元素clear();清除所有元素erase(pos);刪除pos迭代器所指的元素,返回下一個元素的選代器erase(beg,end);刪除區(qū)間[beg,end)的所有元素,返回下一個元素的迭代器erase(elem);刪除容器中值為elem的元素。 set s1.insert(10); s1.insert(20); s1.insert(30); s1.erase(s1.begin()); //刪除 s1.erase(10); s1.clear(); //清空 4)查找和統(tǒng)計 find(key);查找是否存在key,若存在返回該鍵的元素的迭代器,若不存在,返回set.end()count(key);統(tǒng)計key的元素的個數(shù) 7、Queue 先進(jìn)先出的數(shù)據(jù)結(jié)果,有兩個出口,只有隊頭和隊尾能被外界訪問,因此不存在遍歷的行為 1)構(gòu)造函數(shù) queue 2)賦值 queue& operator=(const queue &que);重載等號操作符 3)數(shù)據(jù)存取 push(elem);往隊尾添加元素pop();從隊頭移除一個元素back();返回最后一個元素front();返回第一個元素 4)大小 empty();判斷堆棧是否為空size();返回棧的大小 三、字符串 1、簡介 C語言風(fēng)格: char a[5] = {'1','2','3','4','5'}; //字符數(shù)組 char b[5] = {'1','2','3','4','\0'}; //字符串 char greeting['h','e','l','l','o','\0'}; //字符串 char greeting[]="hello"; //輸出結(jié)果為 hello C++引入的string類類型(函數(shù)及其目的) Strcpy(s1,s2) 復(fù)制字符串s2到字符串s1 Strcat(s1,s2) 連接字符串s2到s1的末尾 Strlen(s1)返回字符串s1的長度Strcmp(s1,s2) 若s1=s2,返回0 若s1 2、字符串的讀入 1)對字符數(shù)組得輸入方法 1.1cin使用空白字符作為一次輸入的結(jié)尾,并忽略該空字 char word[10]; cin >> word; cout< //若輸入123 456,只會輸出123 該情況下只能輸入一個單詞,若輸入了多個單詞,只會取第一個單詞,其余單詞被忽略掉 1.2使用getline()/get()函數(shù)完成面向行的輸入 區(qū)別:主要區(qū)別在于它們處理輸入流的方式:getline() 用于讀取整行并丟棄定界符,而 get() 則用于逐字符讀取,并保留定界符在輸入流中。 #cin.getline() cin.getline(line,nums,(optional)delim) nums:代表從該隊列中獲取nums-1個字符到line中(最后一個字符為\0)若超出nums-1,后面的都無法取到。 delim:指定分界符,表示遇到分界符后當(dāng)前字符串的輸入將停止,其后的會保留在輸入隊列中作為下一個字符串的開始。 int main(){ char line1[10]; char line2[10]; cin.getline(line1,10,'s'); cin.getline(line2,10); cout< cout< return 0; } //測試:123s456 //結(jié)果: line1:123 line2:456 #cin.get() cin.get(line,nums,(optional)delim) get會在輸入隊列中保留最后的換行符,若不做處理可能出現(xiàn)問題,以下方式可以避免: cin.get(line2,10).get(); cin.get(line2,10); get(); 1.3數(shù)字與字符串混合輸入 int a,b; char s[7]; cin>>a; (cin>>b).get(); cin.getline(s,7); cout< //測試:123 456 hello 結(jié)果: 123 456 hello 123 456 hello 2)對string對象的輸入方法 string a getline(cin,a); cout< 3、常見操作 1)常用函數(shù) 2)讀寫string string s1,s2,s3; cin>>s1; cin>>s2>>s3; //結(jié)果: nice to meet nice to meet 若要保留輸入時的空格,可以使用getline string s1; getline(cin,s1); //結(jié)果: nice to meet nice to meet 3)cctype頭文件 4)用for循環(huán)完成cctype各項 #include #include #include using namespace std; int main(void){ string s1 = "hello world"; for(auto &c : s1) c=toupper(c); cout< return 0; } //結(jié)果 HELLO WORLD 四、排序 1、冒泡排序 原理:比較前后兩個數(shù)字的大小,大的放在后面,依次遍歷所有數(shù)字 #include #define N 1010 using namespace std; int n = 6; //待排序的元素個數(shù)為6 int a[N] = {0,2,3,2,11,7,6}; //待排序元素 int main(){ for(int i = 1;i //一共n-1個階段,在第i個階段,未排序序列長度從n-i+1到n-i for (int j = 1;j<=n-i;++j) //將序列從1到n-i+1的最大值移到n-i+1的位置 if (a[j] > a[j+1]) // 將序列從1到n-i+1的最大值,移到n-i+1的位置 swap(a[j],a[j+1]); // 其中j枚舉的是前后交換元素的前一個元素序號 } //輸出 for (int i=1;i<=n;++i)
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。