容器C++

string容器

?string構造函數

#include<iostream>
using namespace std;
#include<string.h>
void test01() {string s1;//默認構造const char* str = "hello world";string s2(str);//傳入char*cout << "s2=" << s2 << endl;string s3(s2);//傳入stringcout << "s3=" << s3 << endl;string s4(5, 'a');//5個acout << "s4=" << s4 << endl;
}
int main() {test01();return 0;
}

?賦值操作

#include<iostream>
using namespace std;
#include<string.h>
//賦值操作
void test() {string str1;str1 = "hello";//=賦值cout << str1 << endl;string str2;str2 = str1;cout << str2 << endl;string str3;str3 = 'a';//可以把單個字符賦值給字符串cout << str3 << endl;string str4;str4.assign("hello c++");//assign賦值cout << str4 << endl;string str5;str5.assign("study", 3);//把字符串的前3個字符賦值給str5cout << str5 << endl;string str6;str6.assign(str5);cout << str6 << endl;string str7;str7.assign(6, 'w');cout << str7 << endl;
}
int main() {test();return 0;
}

字符串拼接

字符串末尾追加字符串

#include<iostream>
using namespace std;
#include<string.h>
//字符串拼接
void test() {string s1="hello";s1 += " world";//追加字符串cout << s1 << endl;s1 += '!';//追加字符cout << s1 << endl;string s2 = " study";s1 += s2;//追加字符串cout << s1 << endl;s1.append(" up up");cout << s1 << endl;s1.append(" eat food", 3);//把前n個字符拼接進來cout << s1 << endl;s1.append(s2);cout << s1 << endl;s1.append(s2, 0, 2);//從第0個位置截取2個字符,追加上cout << s1 << endl;
}
int main() {test();return 0;
}

string查找和替換

void test() {string s1 = "abcdefgcd";int pos = s1.find("cd");//3  返回d的位置。沒有這個字串返回-1if (pos == -1) {cout << "未找到字符串" << endl;}else {cout << pos << endl;//pos=2   position 位置}//rfind//rfind從右往左查找,find從左往右查找pos = s1.rfind("cd");//7cout << pos << endl;
}//替換
void test02() {string s1 = "abcdefg";s1.replace(1, 3, "1111");//從1號位置起 3個字符,替換成1111cout << s1 << endl;//a1111efg
}

string的字符串比較

void test() {string s1 = "hello";string s2 = "hello";if (s1.compare(s2) == 0)cout<<"相等" << endl;else if(s1.compare(s2) > 0)cout << "s1>s2" << endl;else if(s1.compare(s2)<0)cout << "s1<s2" << endl;
}

string字符讀取

void test() {string s1 = "hello";//cout << s1 << endl;for (int i = 0; i < s1.size(); i++)cout << s1[i] << " ";cout << endl;for (int i = 0; i < s1.size(); i++)cout << s1.at(i) << " ";s1[2] = 'a';//可以修改字符s1.at(1) = 'b';
}

string插入和刪除

void test() {string str = "hello";str.insert(1, "111");//插入111cout << str << endl;str.erase(1, 3);//刪除111.從1號位置刪除3個字符
}

string字串

從字符串中獲取想要的字串

void test02() {string email = "zhangsan@qq.com";//從郵箱中獲取用戶名int pos = email.find("@");cout << email.substr(0, pos) << endl;
}

vector

相似于數組,也稱為單端數組。

和數組不同的是,vector可以動態擴展

#include<iostream>
using namespace std;
#include<vector>//vector頭文件
//iterator迭代器
void printVector(vector<int>&v) {for (vector<int>::iterator it = v.begin(); it != v.end(); it++)cout << *it << " ";cout << endl;
}void test() {vector<int>v1;//默認構造 無參構造for (int i = 0; i < 10; i++)v1.push_back(i);printVector(v1);//通過區間方式進行構造vector<int>v2(v1.begin(), v1.end());printVector(v2);//n個 elem方式構造vector<int>v3(3, 5);//3個5printVector(v3);//拷貝構造vector<int>v4(v3);printVector(v4);
}
int main() {test();return 0;
}

vector賦值操作

void printVector(vector<int>& v) {for (vector<int>::iterator it = v.begin(); it != v.end(); it++)cout << *it << " ";cout << endl;
}void test() {vector<int>v1;for (int i = 0; i < 10; i++)v1.push_back(i);printVector(v1);//operator= 賦值vector <int>v2;v2 = v1;printVector(v2);//assignvector<int>v3;v3.assign(v1.begin(), v1.end());printVector(v3);//n個 elem方式賦值vector<int>v4;v4.assign(5, 100);//5個100printVector(v4);
}

vector容器? 容量和大小

#include<iostream>
using namespace std;
#include<vector>//vector頭文件void printVector(vector<int>& v) {for (vector<int>::iterator it = v.begin(); it != v.end(); it++)cout << *it << " ";cout << endl;
}
void test() {vector<int>v1;for (int i = 0; i < 10; i++) {v1.push_back(i);}printVector(v1);if (v1.empty())cout << "v1為空" << endl;else {cout << "v1不為空" << endl;cout << v1.capacity() << endl;cout << v1.size() << endl;}//重新指定大小 v1.resize(15,100);//指定用100填充   v1.resize(15);   printVector(v1);//如果重新指定的比原來長,默認用0填充v1.resize(5);//指定的比原來短,超出的部分會刪掉printVector(v1);
}int main() {test();return 0;
}

?插入和刪除

void test() {vector<int>v1;//尾插v1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);//遍歷printVector(v1);//尾刪v1.pop_back();printVector(v1);//插入 第一個參數是迭代器v1.insert(v1.begin(), 100);//起始迭代器printVector(v1);v1.insert(v1.begin(), 2, 1000);//在起始迭代器處插入2個1000printVector(v1);//刪除v1.erase(v1.begin());//在起始迭代器處刪除printVector(v1);//清空//v.clear();v1.erase(v1.begin(), v1.end());//提供一個區間printVector(v1);
}

vector數據存取

void test() {vector<int>v1;for (int i = 0; i < 10; i++) {v1.push_back(i);//尾插法}for (int i = 0; i < v1.size(); i++) {cout << v1[i] << " ";}cout << endl;for (int i = 0; i < v1.size(); i++)cout << v1.at(i) << " ";cout << endl;//獲取第一個元素cout << v1.front() << endl;//獲取最后一個元素cout << v1.back() << endl;
}

vector互換容器

void test() {vector<int>v;int num = 0;//統計開辟次數int* p = NULL;for (int i = 0; i < 10000; i++) {v.push_back(i);if (p != &v[0]) {p = &v[0];num++;}}cout << num << endl;
}

deque容器

#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d) {for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)cout << *it << " ";cout << endl;
}
void test() {deque<int>d1;for (int i = 0; i < 10; i++){d1.push_back(i);//插數}printDeque(d1);deque<int>d2(d1.begin(), d1.end());printDeque(d2);deque<int>d3(5, 100);printDeque(d3);deque<int>d4(d3);printDeque(d4);
}
int main() {test();return 0;
}

賦值

void printDeque(const deque<int>&d) {//const使其只讀for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)cout << *it << " ";cout << endl;
}
void test() {deque<int>d1;for (int i = 0; i < 10; i++) {d1.push_back(i);}printDeque(d1);//operator= 賦值deque<int>d2;d2 = d1;printDeque(d2);//assign 賦值deque<int>d3;d3.assign(d1.begin(), d1.end());printDeque(d3);deque<int>d4;d4.assign(5, 6);printDeque(d4);
}

deque容器大小

void test() {deque<int>d1;for (int i = 0; i < 10; i++)d1.push_back(i);if (d1.empty())cout << "d1為空" << endl;else {cout << "d1不為空" << endl;cout << d1.size() << endl;//deque沒有容量的概念,可以無限放//重新指定大小//d1.resize(15);d1.resize(15, 3);//多的用3填充printDeque(d1);}
}

deque容器的插入和刪除

#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>d) {//const防止誤修改for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)cout << *it << " ";cout << endl;
}
void test() {deque<int>d1;//尾插d1.push_back(10);d1.push_back(20);//頭插d1.push_front(30);d1.push_front(40);printDeque(d1);//40 30 10 20//尾刪d1.pop_back();printDeque(d1);//40 30 10//頭刪d1.pop_front();printDeque(d1);//30 10
}void test02() {deque<int>d1;//尾插d1.push_back(10);d1.push_back(20);//頭插d1.push_front(30);d1.push_front(40);printDeque(d1);//40 30 10 20//insert插入d1.insert(d1.begin(), 1000);printDeque(d1);//1000 40 30 10 20d1.insert(d1.begin(), 3, 9);printDeque(d1);//9 9 9 1000 40 30 10 20//刪除指定位置deque<int>::iterator it = d1.begin();it+=5;d1.erase(it);printDeque(d1);//9 9 9 1000 40 10 20//按照區間的形式刪除d1.erase(d1.begin(), it);//10 20printDeque(d1);//按照區間進行插入deque<int>d2;d2.push_back(1);d2.push_back(2);d2.push_back(3);d1.insert(d1.begin(), d2.begin(), d2.end());printDeque(d1);//1 2 3 10 20//清空d1.clear();printDeque(d1);//只剩一個換行了
}
int main() {test();test02();return 0;
}

數據存取

void test() {deque<int>d1;d1.push_back(10);d1.push_back(20);d1.push_back(30);d1.push_front(100);d1.push_front(200);d1.push_front(300);//[]方式for (int i = 0; i < d1.size(); i++)cout << d1[i] << endl;//at方式for (int i = 0; i < d1.size(); i++)cout << d1.at(i) << endl;cout << "第一個元素:" << d1.front() << endl;cout << "最后一個元素:" << d1.back() << endl;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/73680.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/73680.shtml
英文地址,請注明出處:http://en.pswp.cn/web/73680.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【2.項目管理】2.4 Gannt圖【甘特圖】

甘特圖&#xff08;Gantt&#xff09;深度解析與實踐指南 &#x1f4ca; 一、甘特圖基礎模板 項目進度表示例 工作編號工作名稱持續時間(月)項目進度&#xff08;周&#xff09;1需求分析3▓▓▓???????2設計建模3?▓▓▓??????3編碼開發3.5???▓▓▓▓??…

C++List模擬實現|細節|難點|易錯點|全面解析|類型轉換|

目錄 1.模擬代碼全部 2.四大塊代碼理解 1.最底層&#xff1a;ListNode部分 2.第二層&#xff1a;ListIterator部分 3.第三層&#xff1a;ReserveListIterator部分 4最終層&#xff1a;List 1.模擬代碼全部 using namespace std; template<class T> struct ListNode …

【深度學習與實戰】2.1、線性回歸模型與梯度下降法先導

import numpy as np# 數據準備 X np.array([1, 2, 3]) y np.array([3, 5, 7])# 參數初始化 w0, w1 0, 0 alpha 0.1 n len(X)# 迭代10次 for epoch in range(10):# 計算預測值y_pred w1 * X w0# 計算梯度grad_w0 (1/n) * np.sum(y_pred - y)grad_w1 (1/n) * np.sum((y_…

銳捷EWEB路由器 timeout.php任意文件上傳漏洞代碼審計(DVB-2025-9003)

免責聲明 僅供網絡安全研究與教育目的使用。任何人不得將本文提供的信息用于非法目的或未經授權的系統測試。作者不對任何由于使用本文信息而導致的直接或間接損害承擔責任。如涉及侵權,請及時與我們聯系,我們將盡快處理并刪除相關內容。 一:產品介紹 銳捷EWEB路由器是銳…

flask開發中設置Flask SQLAlchemy 的 db.Column 只存儲非負整數(即 0 或正整數)

如果你想控制一個 Flask SQLAlchemy 的 db.Column 只存儲非負整數&#xff08;即 0 或正整數&#xff09;&#xff0c;你可以在模型中使用驗證來確保這一點。一種常見的方法是使用模型的 validate 方法或者在執行插入或更新操作時進行檢查。 以下是實現這一目標的幾種方法&…

sqlmap 源碼閱讀與流程分析

0x01 前言 還是代碼功底太差&#xff0c;所以想嘗試閱讀 sqlmap 源碼一下&#xff0c;并且自己用 golang 重構&#xff0c;到后面會進行 ysoserial 的改寫&#xff1b;以及 xray 的重構&#xff0c;當然那個應該會很多參考 cel-go 項目 0x02 環境準備 sqlmap 的項目地址&…

vscode連接服務器失敗問題解決

文章目錄 問題描述原因分析解決方法徹底刪除VS Code重新安裝較老的版本 問題描述 vscode鏈接服務器時提示了下面問題&#xff1a; 原因分析 這是說明VScode版本太高了。 https://code.visualstudio.com/docs/remote/faq#_can-i-run-vs-code-server-on-older-linux-distribu…

企業網站源碼HTML成品網站與網頁代碼模板指南

在當今數字化時代&#xff0c;企業網站已成為展示品牌形象、吸引客戶和提供在線服務的重要工具。對于許多企業來說&#xff0c;使用現成的HTML網站源碼模板是快速搭建網站的高效方式。本文將詳細介紹企業網站源碼、HTML成品網站以及網頁代碼模板的相關內容&#xff0c;幫助你快…

計算機網絡 - OSI 七層模型

OSI 七層模型 OSI&#xff08;Open System Interconnection&#xff0c;開放系統互聯&#xff09;模型由 ISO&#xff08;國際標準化組織&#xff09; 制定&#xff0c;目的是為不同計算機網絡系統之間的通信提供一個標準化的框架。它將網絡通信劃分為 七個層次&#xff0c;每…

flutter-實現瀑布流布局及下拉刷新上拉加載更多

文章目錄 1. 效果預覽2. 結構分析3. 完整代碼4. 總結 1. 效果預覽 在 Flutter 應用開發中&#xff0c;瀑布流布局常用于展示圖片、商品列表等需要以不規則但整齊排列的內容。同時&#xff0c;下拉刷新和上拉加載更多功能&#xff0c;能夠極大提升用戶體驗&#xff0c;讓用戶方…

在 Ubuntu 下通過 Docker 部署 Nginx 服務器

1. Docker 和 Nginx 簡介以及實驗環境 Docker 是一個開源的容器化平臺&#xff0c;允許開發者將應用程序及其依賴項打包成一個輕量級的、可移植的容器。通過 Docker&#xff0c;開發者可以在任何支持 Docker 的環境中運行應用&#xff0c;從而實現一致的開發和生產環境。Docke…

IoT平臺實時監測機器人狀態的實現方案

通過IoT平臺實時監測機器人狀態的實現方案與可執行路徑 一、整體架構設計 #mermaid-svg-6xMlDfFSZM4Wc8tA {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-6xMlDfFSZM4Wc8tA .error-icon{fill:#552222;}#mermaid-sv…

mybatis里in關鍵字拼接id問題

我們一般會把ids集合用StrUtil.join(‘,’)轉成"1,2,3"這種形式 然后放入in中 我們會這么寫: select id, nick_name, icon from tb_user where id in (#{ids}) order by FIELD(id, #{ids})結果發現sql執行是這樣的: select id, nick_name, icon from tb_user where…

4.用 Excel 錄入數據

一 用 Excel 錄入數據的兩種方式 用鼠標鍵盤錄入數據和從網上爬取數據。 二 用鼠標鍵盤錄入數據 1.錄入數據的規范 橫著錄入數據&#xff08;橫著一條條錄入數據&#xff09;。 2.使用快捷鍵進行數據錄入 tab 鍵和 enter 鍵。 tab 鍵&#xff1a;向右移動一個單元格。 tab 鍵…

C++類與對象-3.23筆記

今天學習了類的概述和寫類的基本框架 在嗶哩嗶哩學習的這個老師的C面向對象高級語言程序設計教程&#xff08;118集全&#xff09;講的很不錯&#xff08;真的&#xff01;&#xff01;&#xff01;&#xff09;&#xff0c;C語言也是在這個老師的帶領下學習的 #include<io…

Android讀寫權限分析

Android系統使用的是Linux內核&#xff0c;所以Android系統沿用了linux系統的那一套文件讀寫權限。 目錄 1&#xff0c;權限解讀1.1&#xff0c;權限分為三種類型&#xff1a;1.2&#xff0c;權限針對的三類對象&#xff1a;1.3&#xff0c;文件和目錄的權限區別1.3.1&#xf…

Python二分查找【清晰易懂】

1. 二分查找是什么&#xff1f; 想象你在玩“猜數字”游戲&#xff1a; 對方心里想一個 1~100 的數字&#xff0c;你每次猜一個數&#xff0c;對方會告訴你是“大了”還是“小了”。 最快的方法&#xff1a;每次都猜中間的數&#xff01;比如第一次猜50&#xff0c;如果大了&…

關于Qt的各類問題

目錄 1、問題&#xff1a;Qt中文亂碼 2、問題&#xff1a;啟動時避免ComBox控件出現默認值 博客會不定期的更新各種Qt開發的Bug與解決方法,敬請關注! 1、問題&#xff1a;Qt中文亂碼 問題描述&#xff1a;我在設置標題時出現了中文亂碼 this->setWindowTitle("算法…

關于我對接了deepseek之后部署到本地將數據存儲到mysql的過程

寫在前面 今天寫一下使用nodejs作為服務端&#xff0c;vue作為客戶端&#xff0c;mysql的數據庫&#xff0c;對接deepseek的全過程&#xff0c;要實現一個很簡單的效果就是&#xff0c;可以自由的詢問&#xff0c;然后可以將詢問的過程存儲到mysql的數據庫中。 文檔對接 deeps…

游戲引擎學習第182天

回顧和今天的計劃 昨天的進展令人驚喜&#xff0c;原本的調試系統已經被一個新的系統完全替換&#xff0c;新系統不僅能完成原有的所有功能&#xff0c;還能捕獲完整的調試信息&#xff0c;包括時間戳等關鍵數據。這次的替換非常順利&#xff0c;效果很好。 今天的重點是在此基…