中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

長安網(wǎng)站設計每日新聞

長安網(wǎng)站設計,每日新聞,網(wǎng)站建設定位,西寧網(wǎng)站建設公司排名文章目錄 數(shù)組vector字符串輸入輸出結(jié)構(gòu)體枚舉指針引用綜合案例 數(shù)組 相同類型的數(shù)據(jù)的集合{ },通過索引訪問元素;在內(nèi)存中連續(xù)存儲,屬于順序表;插入、刪除時間復雜度 O ( n ) O(n) O(n),訪問復雜度 O ( 1 ) O(1) O(1…

文章目錄

  • 數(shù)組
  • vector
  • 字符串
  • 輸入輸出
  • 結(jié)構(gòu)體
  • 枚舉
  • 指針
  • 引用
  • 綜合案例

數(shù)組

  • 相同類型的數(shù)據(jù)的集合{ },通過索引訪問元素;
  • 在內(nèi)存中連續(xù)存儲,屬于順序表;
  • 插入、刪除時間復雜度 O ( n ) O(n) O(n),訪問復雜度 O ( 1 ) O(1) O(1)
  • 定義數(shù)組,首先確定內(nèi)部的數(shù)據(jù)類型,并指定長度(常量),初始化時只能使用{ }來初始化;
// 定義
int arr[30] = {1, 2, 3}; // 后面的值默認為0,初始值個數(shù)不能超過長度// 不指定長度時,需要初始化
double score[] = {79.5, 80.5, 90}; // 訪問數(shù)組,注意索引越界問題
for(int i=0; i < sizeof(arr) / sizeof(arr[0]) ; i++){cout << "arr[" << i << "]:" << arr[i] << endl;
}// 僅僅獲取元素
for(int e:arr){cout << e << endl;
}

?

  • 數(shù)組內(nèi)部嵌套數(shù)組,即二維數(shù)組
// 定義二維數(shù)組
int arr[2][3] = {{1,2,3},{4,5,5}};// 遍歷
int rowCount = sizeof(arr) / sizeof(arr[0]);
int colCount = sizeof(arr[0]) / sizeof(arr[0][0]);
for(int i=0; i<rowCount; i++){for(int j=0; j<colCount; j++){cout << arr[i][j] << endl;}
}// 元素遍歷
for(auto& row:arr){ // 復合數(shù)據(jù)類型需要 &引用for(auto e:row){ // auto 自動類型cout <<e << endl;	}
}

?

  • 數(shù)組實現(xiàn) 選擇排序

int main() {int arr[] = {4, 3, 2, 1, 5};// 選擇排序O(n^2)// 每次選擇最大值或者最小值,放在最后一位// n個數(shù) 做n-1次選擇int arrLen = sizeof(arr) / sizeof(arr[0]);int n = arrLen - 1;int temp;while (n >= 1) { // 控制選擇次數(shù)int max_idx = 0; // for (int i = 1; i <= n; i++) {if (arr[i] > arr[max_idx]) {max_idx = i;}}// 交換if (max_idx != n) {// 交換temp = arr[n];arr[n] = arr[max_idx];arr[max_idx] = temp;}n--;}for (int i = 0; i < arrLen; i++) {cout << arr[i] << endl;}
  • 數(shù)組實現(xiàn) 冒泡排序

int main() {int arr[] = {4, 3, 2, 1, 5};// 冒泡排序 O(n^2)// 每次將較大值向后一位冒泡// n個數(shù) 做n-1輪冒泡int arrLen = sizeof(arr) / sizeof(arr[0]);int temp;for (int i = 0; i < arrLen - 1; i++) { // 控制冒泡的輪數(shù)// 控制冒泡for (int j = 0; j < arrLen - 1 - i; j++) {if (arr[j] > arr[j + 1]) {temp = arr[j + 1];arr[j + 1] = arr[j];arr[j] = temp;}}}for (int i = 0; i < arrLen; i++) {cout << arr[i] << endl;}// system("dir");system("pause");return 0;
}

?

vector

  • 數(shù)組的長度固定,且越界操作可能帶來未知的風險,故對數(shù)組擴展,得到vector,array;
  • vector容器,也是相同類型的數(shù)據(jù)的集合,長度可變,但性能降低;
  • array容器,也是相同類型的數(shù)據(jù)的集合,長度固定,但性能降低;
#include <vector>
#include <array>
using namespace std;  // 必須包含頭文件,并使用std int main() {// 容器是對數(shù)組的擴展,不定長用vector, 定長用array (也是擴展的數(shù)組)// 拷貝初始化vector<int> cont1 = {1, 2, 3}; vector<float> cont2{ 34.5, 46.7 }; //  賦值號可省略// 直接初始化vector<double> cont3(5); // 指定長度為5  可以繼續(xù)添加數(shù)據(jù),擴展長度vector<string> cont4(3, "jack"); // 長度為3,且默認值均為"jack"// 容器  通過索引訪問,修改for (int i = 0; i < cont1.size(); i++) {cout << cont1[i] << "\t";}// 添加cont1.push_back(100); // 尾部添加//cont1.pop_back(); // 尾部刪除for (int e : cont1) {cout << e << " ";}// 基于數(shù)組擴展的array , 固定長度array<string, 5> userList = { "jack", "tom", "lucy" }; // 賦值也可省略// array<string, 5> userList;  // userList = { "jack", "tom", "lucy" };   // 先聲明,再賦值 userList.size() 長度for (string e : userList) {cout << e << " ";}return 0;
}

字符串

  • string類型,也是一個容器,內(nèi)容可變,通過索引訪問每個字符;
  • size()方法獲取長度,size() - 1是最后一個索引;同數(shù)組,也不能越界訪問/賦值
  • 支持兩種for循環(huán)
#include <string>
using namespace std;int main() {// c++ 中的string是可變的容器,而python的str是不可變的容器string name; //初始化為空string sex = "male"; //拷貝初始化string addr("wuhan province"); // 直接初始化// 普通的forfor (int i = 0; i < addr.size(); i++) {cout << addr[i] << " "; // 索引訪問每個字符}// 元素遍歷的forfor (char e : addr) {cout << e << " ";}return 0;
}
  • 字符串的拼接
    • 字符串變量 + 字符串變量;
    • 字符串變量 + 字符串字面量;
    • 字符串字面量 + 字符串字面量 (X); 不同于python
  • 字符串的比較
    • ==、> >= < <= 逐字符比較;
  • c風格的字符串,字符類型的數(shù)組;
    • {‘a(chǎn)’,‘b’, ‘c’, ‘\0’} , \0為結(jié)束字符
      ?

輸入輸出

  • cin 控制臺輸入,捕獲字符串,遇空格則將空格前的整體作為一個字符串,賦值給變量;
string name;
string last_name;
cout << "input your name:"; // 控制臺輸出
cin >> name ; // 輸入"jack li", 只能捕獲到 "jack"
cin >> name >> last_name;   // 捕獲到"li" 賦值給 last_name  
  • getline 獲取一行
string name;
getline(cin, name); //  獲取一行,并賦值給name
  • cin.get() 獲取一個字符
char sex;
sex = cin.get();  // or cin.get(sex)
  • fstream 文件操作
    • 包含頭文件<fstream>;
    • ifstream readFile(“xxx.txt”) 讀取文件;
    • ofstream toFile(“target.txt”) 寫入到文件;
    • 案例,輸入用戶的姓名,年齡,學歷,性別,電話,保存到 user.txt 文件中,并讀取該文件,逐行打印;
#include <iostream>
#include <string>  // 字符串的 頭文件
#include <fstream> // 文件操作的頭文件
using namespace std;// 枚舉 性別
enum Sex {Male,  // 默認從0開始, 也可以賦值指定從2、4等開始Female
};int main() {// 姓名  學歷   手機號string name, stuLevel, phone;int age = 18;string ageLabel = to_string(age); // 整型轉(zhuǎn)為字符串// 性別string sex_label;enum Sex sex = Male;// 用戶輸入cout << "input your name:";getline(cin, name); // 獲取一行  回車則結(jié)束本次輸入cout << "input your stuLevel:";cin >> stuLevel;cout << "input your phone:";cin >> phone;cout << "input your age and sex separated with whitespace:";cin >> age >> sex_label; // 捕獲多個空格分割的字符串// 枚舉 重新賦值if (sex_label == "0")sex = Male;  // 對應int數(shù)值elsesex = Female;// 字符串拼接string totalContent = "姓名:";totalContent += name + "\n" + "年齡:";totalContent += ageLabel + "\n" + "性別:";  // 必須字符串對象開頭  開始拼接totalContent += sex_label + "\n" + "學歷:";totalContent += stuLevel + "\n" + "手機號:";totalContent += phone;system("cd C:\\Users\\lenovo\\Desktop");// 寫入文件ofstream writeToFile("C:\\Users\\lenovo\\Desktop\\user.txt");// << 流輸出   到文件對象中writeToFile << totalContent;// 關(guān)閉文件對象writeToFile.close();// 讀取文件ifstream readFileObj("C:\\Users\\lenovo\\Desktop\\user.txt");//逐單詞 讀取/*string word;while (readFileObj >> word) {cout << word << endl;}*/// 逐行讀取string oneLine;while (getline(readFileObj, oneLine)) {cout << oneLine << endl;}//逐字符讀取/*char c;while (readFileObj.get(c)) {cout << c << endl;}*/return 0;
}

?

結(jié)構(gòu)體

  • 結(jié)構(gòu)體是一個復合的數(shù)據(jù)類型,可以存儲不同的數(shù)據(jù)類型;
    • 內(nèi)部可以定義基本類型;
    • 也可以定義結(jié)構(gòu)體類型;
    • 通過. 訪問內(nèi)部屬性,并可修改;
    • 定義學生結(jié)構(gòu)體,包含學生的基本信息;
      • 姓名
      • 年齡
      • 班級
      • 同學s
#include <iostream>
#include <string>  // 字符串的 頭文件
#include <fstream> // 文件操作的頭文件
#include <vector>  // 變長容器
using namespace std;// 班級結(jié)構(gòu)體
struct StuClass {   // 使用struct關(guān)鍵字 定義結(jié)構(gòu)體string name;   // 班級的名字int stuNum;string classMaster;
}; // 此行 }后面還可以聲明變量,只是不常用// 學生結(jié)構(gòu)體
struct StuInfo {string name;int age;StuClass myClass;vector<StuInfo> myClassmates; // 同學
};//輸出一個學生的信息
void printStu(StuInfo stu) {cout << "姓名:" << stu.name << endl;cout << "年齡:" << stu.age << endl;cout << "班級:" << stu.myClass.name << endl;cout << "同學數(shù):" << stu.myClassmates.size() << endl;
}int main() {	// 結(jié)構(gòu)體類   c++ 可以直接使用,c中需要帶著struct一起使用// 創(chuàng)建班級StuClass class1 = { "三年級01", 30, "teacher wang" }; // 賦值可以省略//創(chuàng)建學生StuInfo stu1 = { "jack", 23, class1, {} };StuInfo stu2 = { "tom", 18, class1, {} };// 為stu2 添加一個同學stu2.myClassmates.push_back(stu1);// 輸出學生信息printStu(stu1);printStu(stu2);return 0;
}

?

枚舉

  • 把可以取的值,都一 一地列舉出來;
  • 使用關(guān)鍵字enum定義枚舉;
// 如之前使用的性別的枚舉
enum Sex{Male,  // 默認從0開始, 可以在任意項處指定起始值Female, // 1
}//定義變量
Sex sex = Male; // 以枚舉項 賦值
Sex sex1 = Sex(2);  // 以整數(shù)的強制類型轉(zhuǎn)換賦值,不能直接復制int// 枚舉 周工作日
enum WEEK {Mon,Tue,Wed,Thu = 10, // 從10開始Fri,Sat,Sun
};

?

指針

  • 變量即存儲數(shù)據(jù)的地址空間的名字;
  • 指針即存儲數(shù)據(jù)的地址空間的地址,即指針變量 存放的是地址
  • 地址占用8bytes;
    在這里插入圖片描述

int main() {	int a = 10;int* p; // 指針變量p = &a;  // 指針變量   賦值a的地址cout << "指針:" << p << endl;cout << "指針的值:" << *p << endl;// 指針操作改值*p = 20;cout << "修改后的值:" << *p << endl;return 0;
}
  • 未初始化的指針,即無效指針(野指針),不可直接使用;
  • 空指針,指針賦值nullptr / NULL / 0; 聲明指針,必須初始化
  • void* 指針,可以賦值任意類型的地址,但不能解引用;
  • int** p ,指向指針的指針;解引用使用** ;
  • const int* p, 指向常量的指針,只能賦值常量的地址;
  • int* const p, 常量指針, 不可改變量p的地址;

int main() {	// 常量const int a = 4, b = 5;// 指向常量的指針const int* p = &a;cout << *p << endl;p = &b;cout << *p << endl;// 指向變量的  常量指針int c = 10;int* const p2 = &c;  // 存儲地址不可修改cout << *p2 << endl;return 0;
}
  • 數(shù)組名稱為指向內(nèi)部首元素的地址,即首地址;
  • 指針數(shù)組,指針組成的數(shù)組,int* arr[5];
  • 數(shù)組指針,代表數(shù)組的指針;

int main() {	int a = 1, b = 2, c = 3;// 指針組成的數(shù)組-->指針數(shù)組int* arr[5];arr[0] = &a;arr[1] = &b;arr[3] = &c;cout << *(arr[0]) << endl;cout << *(arr[1]) << endl;// 數(shù)組指針-- 指向一個數(shù)組的指針int arr2[3];int* p = arr2;  // 或者  int(*p)[3]*p = 1;*(p + 1) = 2; // 地址偏移一個數(shù)據(jù)類型的長度*(p + 2) = 3;for (int e : arr2) {cout << e << endl;}// 數(shù)組指針int(*pp)[3]; // *pp 相當于arr2pp = &arr2; // 取地址cout << arr2 << endl; // 數(shù)組的首地址cout << pp << endl; // 數(shù)組的首地址cout << *pp << endl; // *pp 為arr2   還是首地址cout << **pp << endl; // 解引用首個元素cout << *(*pp + 1) << endl; // 解引用第二個元素return 0;
}

?

引用

  • 引用,是給一個變量起別名,一旦綁定一個變量,再不可綁定其他變量;
  • 定義引用, 如 int&
int a = 10;
// 定義引用(必須引用變量)
int& af = a;  // 必須 聲明同時 初始化,指向同一個內(nèi)存地址
af = 20;
cout << a << endl; // 20int& aaf = af;  // 引用 的引用  使用同引用;
  • 常量引用
const int aa = 20;// 常量的引用
const int& aaref = aa;  // 引用常量
const int& bbref = 10;  // 引用字面量int a = 10;
const int& ccref = a;  // 引用變量
  • 引用相當于指針常量;
    ?

綜合案例

  • 反轉(zhuǎn)一個數(shù)組
    • 思路:兩邊對稱的元素互換;

int main() {	// 定義數(shù)組容器array<int, 5> arr = { 4,3,2,1,5 }; // 兩邊對換int temp;int n = arr.size();for (int i = 0; i < n / 2; i++) {temp = arr[i];arr[i] = arr[n - 1 - i];arr[n - 1 - i] = temp;}for (int e : arr) {cout << e << " ";}cout << endl;return 0;
}
  • 兩個字符串相加
    • 從個位逐位,沒有的一方用0代替;
    • label表示進位,有進位則為1(進位最多為1),無進位則為0;
    • 從字符串的最后一個索引開始向索引0方向,循環(huán)處理;
    • 字符串通過索引訪問每一個字符,數(shù)值字符減去‘0’字符得到其代表的整數(shù)值;
    • int轉(zhuǎn)為字符串使用 to_string(int);
    • 字符串的拼接必須字符串對象在左

int main() {	// 字符串相加string s1 = "39999"; // 可變?nèi)萜?#xff0c;索引訪問,獲取一個字符   '2' - '0'  得到對應的數(shù)值2string s2 = "2";  // 字符串-->int   stoi("5793")  stof stodstring result = "";int temp;int s1Idx = s1.size() - 1;int s2Idx = s2.size() - 1;// 表示是否有進位 為1有進位 為0無進位int mid = 0;int a, b;while (s1Idx >= 0 || s2Idx >= 0 || mid) { // 逐位相加,沒有則用0代替,特殊情況:兩者長度相同,且最后有進位a = s1Idx >= 0 ? s1[s1Idx] - '0' : 0; // 當前字符  減去 '0'字符 得到自己的整數(shù)值b = s2Idx >= 0 ? s2[s2Idx] - '0' : 0;// 求和temp = a + b + mid;if (temp <= 9) {// 本次求和沒有進位result = to_string(temp) + result; // int 轉(zhuǎn)為 stringmid = 0;}else {// 有進位mid = temp / 10;result = to_string(temp % 10) + result;}s1Idx--;s2Idx--;}cout << "計算結(jié)果:" << result << endl;return 0;
}
  • 反轉(zhuǎn)一個單向鏈表,應用的知識點如下。
    • 包含自定義的頭文件使用#include “xxx.h”
    • 空指針初始化 NULL/nullptr,值為0;
    • 指針的解引用+調(diào)用屬性等價于 指針 -> 調(diào)用屬性;

定義結(jié)構(gòu)體

// 自定義一個頭文件lauf.h , 并定義結(jié)構(gòu)體
struct LinkNode {int value;LinkNode* next;
};

主程序

#include <iostream>
#include "lauf.h" // 包含自定義的頭文件
using namespace std;// 入口函數(shù)
int main() {// 創(chuàng)建單鏈表  7->8->9->NULL    空指針為0LinkNode* myLink;LinkNode node9 = { 9, nullptr }; // nullptr 空指針 值為0LinkNode node8 = { 8, &node9 };LinkNode node7 = { 7, &node8 };myLink = &node7;// 雙指針 方式反轉(zhuǎn)LinkNode* curPtr = myLink;LinkNode* prePtr = nullptr;LinkNode* temp;while (curPtr) { // 指針存在 temp = (*curPtr).next;  //  解引用 再調(diào)用屬性  等價于  curPtr -> nextcurPtr->next = prePtr;// 移動指針prePtr = curPtr;curPtr = temp;}LinkNode* reversedLink = prePtr;   // 指針變量賦值 // 打印反轉(zhuǎn)后的鏈表while (reversedLink) {cout << reversedLink->value << "->";  // -> 等價于  指針的解引用+調(diào)用屬性reversedLink = reversedLink->next;}return 0;
}

?
?
上一篇:C++ 教程 - 01 基礎(chǔ)篇
下一篇:函數(shù)

http://m.risenshineclean.com/news/65506.html

相關(guān)文章:

  • 網(wǎng)站被入侵后需做的檢測(1)網(wǎng)站建站推廣
  • 成都網(wǎng)站制作的公司高明搜索seo
  • 嶗山區(qū)建設管理局網(wǎng)站怎么了黑seo工作內(nèi)容
  • 網(wǎng)站開發(fā)建設總結(jié)seo技巧
  • wordpress企業(yè)站實操天津seo排名效果好
  • 做靜態(tài)網(wǎng)站的軟件電話營銷技巧和營銷方法
  • 諸城 建設外貿(mào)網(wǎng)站網(wǎng)站維護中
  • 自己做網(wǎng)站排名好嗎seo網(wǎng)站優(yōu)化技術(shù)
  • 云南企業(yè)網(wǎng)站代發(fā)qq群發(fā)廣告推廣
  • 成都網(wǎng)頁設計班百度seo系統(tǒng)
  • 做消費信貸網(wǎng)站百度天眼查公司
  • 網(wǎng)站制作需求分析中國營銷網(wǎng)
  • 手機微網(wǎng)站怎么做的開封網(wǎng)絡推廣哪家好
  • 效果型網(wǎng)站建設seo查詢5118
  • 織夢如何做幾種語言的網(wǎng)站技術(shù)培訓學校機構(gòu)
  • 自己做電臺直播的網(wǎng)站自己建網(wǎng)站流程
  • div css快速做網(wǎng)站西安seo外包行者seo06
  • 黑龍江省建設局網(wǎng)站太原做網(wǎng)站的
  • 如何做招聘網(wǎng)站分析google官方下載安裝
  • 品牌設計需要多少錢關(guān)鍵詞首頁優(yōu)化
  • 手機網(wǎng)站源代碼seo網(wǎng)站內(nèi)部優(yōu)化方案
  • 做網(wǎng)站的技術(shù)哪個簡單seo關(guān)鍵詞優(yōu)化怎么收費
  • asp.net網(wǎng)站開發(fā)項目源碼百度收錄怎么做
  • 彩票網(wǎng)站我想自己做網(wǎng)絡推廣員有前途嗎
  • 深圳企業(yè)網(wǎng)站制作設計深圳網(wǎng)絡營銷推廣專員
  • 網(wǎng)站文檔設置index.php哈市今日頭條最新
  • 如何查詢網(wǎng)站建設時間太原seo網(wǎng)站排名
  • 廣告設計培訓機構(gòu)哪家好seo推廣優(yōu)化外包公司
  • java怎么做直播網(wǎng)站鄭州網(wǎng)站優(yōu)化平臺
  • 有了域名 做網(wǎng)站seo查詢愛站網(wǎng)