網站首頁 編程語言 正文
知識點1【STL的概述】
STL(Standard Template Library,標準模板庫)
STL的三大組件:容器(container)、算法(algorithm)、迭代器(iterator)。
算法操作數據,容器存儲數據,迭代器是算法操作容器的橋梁,迭代器和容器一一對應。
STL六大組件:
容器 算法 迭代器 仿函數 適配器 空間配置器
容器:存放數據
算法:操作數據
迭代器:容器和算法的橋梁
仿函數:為算法提供更多的策略
適配器:為算法提供更多的參數接口
空間配置器:管理容器和算法的空間
算法的分類:
質變算法:是指運算過程中會改變區間元素的內容。例如拷貝,替換,刪除等。
非質變算法:是指運算過程中不會區間的元素內容,例如查找、計數、遍歷、尋找極值。
迭代器的分類:
知識點2【迭代器的案例】
案例:容器vector
#include <iostream>
#include <vector>
#include<algorithm>
void myPrintInt(int num);
using namespace std;
void test01(){
//單端動態數組vector類模板
vector<int> arr(0);
//push_back()尾部插入數據
arr.push_back(100);
arr.push_back(200);
arr.push_back(300);
arr.push_back(400);
//訪問數據
//定義一個迭代器存儲arr的起始迭代器
vector<int>::iterator beginIt = arr.begin();
//定義一個迭代器存儲arr的結束迭代器
vector<int>::iterator endIt = arr.end();
//for循環遍歷1
for(vector<int>::iterator i = beginIt; i != endIt; i++){
//對迭代器取* 代表的是 容器的元素
//*biginIt
cout << *i << " ";
}
cout << endl;
//for循環遍歷2(推薦)
for(vector<int>::iterator beginIt = arr.begin(); beginIt != arr.end();beginIt++){
cout << *beginIt << " ";
}
cout << endl;
//STL提供的算法來遍歷容器(包含算法頭文件algorithm)
//for_each從容器的起始--->結束,逐個元素取出
//myPrintInt容器數據的打印方式
for_each(arr.begin(),arr.end(),myPrintInt);
}
void myPrintInt(int num){
cout << num << " ";
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
運行結果:
案例2:容器存放自定義數據類型
#include <iostream>
#include <vector>
#include<algorithm>
#include <string.h>
using namespace std;
class Person{
friend void myPrintInt1(Person &ob);
private:
string name;
int age;
public:
Person(string name,int age){
this->name = name;
this->age = age;
}
};
void myPrintInt1(Person &ob){
cout << ob.age << " " << ob.name << endl;
}
void test02(){
vector<Person> arr;
Person p1("tom",15);
arr.push_back(p1);
arr.push_back(Person("davi",16));
arr.push_back(Person("mary",17));
arr.push_back(Person("peter",18));
for_each(arr.begin(),arr.end(),myPrintInt1);
}
int main(int argc, char *argv[])
{
test02();
return 0;
}
運行結果:
案例3:容器嵌套容器
void test03(){
vector<int> v1;
vector<int> v2;
vector<int> v3;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v2.push_back(100);
v2.push_back(200);
v2.push_back(300);
v2.push_back(400);
v3.push_back(1000);
v3.push_back(2000);
v3.push_back(3000);
v3.push_back(4000);
vector< vector<int>> v4;
v4.push_back(v1);
v4.push_back(v2);
v4.push_back(v3);
for(vector<vector<int>>::iterator it = v4.begin(); it != v4.end(); it++){
for(vector<int>::iterator mit = (*it).begin(); mit != (*it).end();mit++){
cout << (*mit) << " ";
}
cout << endl;
}
}
運行結果:
知識點3【string類】
1、案例:string的構造和賦值
#include <iostream>
#include <string.h>
using namespace std;
/*
3.1.2.1 string 構造函數
string();//創建一個空的字符串 例如: string str;
string(const string& str);//使用一個 string 對象初始化另一個 string 對象
string(const char* s);//使用字符串 s 初始化
string(int n, char c);//使用 n 個字符 c 初始化
3.1.2.2 string 基本賦值操作
string& operator=(const char* s);//char*類型字符串 賦值給當前的字符串
string& operator=(const string &s);//把字符串 s 賦給當前的字符串
string& operator=(char c);//字符賦值給當前的字符串
string& assign(const char *s);//把字符串 s 賦給當前的字符串
string& assign(const char *s, int n);//把字符串 s 的前 n 個字符賦給當前的字符串
string& assign(const string &s);//把字符串 s 賦給當前字符串
string& assign(int n, char c);//用 n 個字符 c 賦給當前字符串
string& assign(const string &s, int start, int n);//將 s 從 start 開始 n 個 字符賦值給字符串
*/
void test01(){
//string(const char* s);//使用字符串s初始化
string str1 = "hello";
cout << str1 << endl;
//string(int n, char c);//使用n個字符c初始化
string str2(10,'H');
cout<<str2<<endl;//"HHHHHHHHHH"
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
原文鏈接:https://blog.csdn.net/DUANJIAWEIDUANJIAWEI/article/details/126954146
相關推薦
- 2022-09-26 shell變量,shell函數,shell數組,shell常用命令,shell流程控制語句
- 2022-06-21 C語言零基礎精通變量與常量_C 語言
- 2022-10-31 Python?NumPy隨機抽模塊介紹及方法_python
- 2022-07-02 react 替換頁面頭圖標失敗
- 2022-06-29 python人工智能tensorflow函數tf.layers.dense使用方法_python
- 2022-03-18 AndroidStudio集成OpenCV的實現教程_Android
- 2023-05-03 C++類與對象的基礎知識點詳細分析_C 語言
- 2023-07-15 react+antd+table實現表格數據在當前頁從頭到尾循環滾動展示
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支