【黑馬程序員】STL之set和map容器

文章目錄

  • set/multiset容器
    • set基本概念
      • 簡介
      • 區別
    • set的構造和賦值
      • 功能描述
      • 函數原型
      • 代碼示例
      • 運行結果
    • set的大小和交換
      • 功能描述
      • 函數原型
      • 代碼示例
      • 運行結果
    • set的插入和刪除
      • 功能描述
      • 函數原型
      • 代碼示例
      • 運行結果
    • set查找和統計
      • 函數原型
      • 代碼示例
      • 運行結果
    • set和multiset區別
      • 區別
      • 代碼示例
      • 運行結果
    • pair隊組創建
      • 功能描述
      • 創建方式
      • 代碼示例
      • 運行結果
    • set容器排序
      • 代碼示例
  • map容器
    • map基本概念
      • 簡介
      • 本質
      • 優點
      • map和multimap區別
    • map的構造和賦值
      • 函數原型
      • 代碼示例
    • map大小和交換
      • 代碼示例
    • map插入和刪除
      • 函數原型
      • 代碼示例
    • map查找和統計
      • 函數原型
      • 代碼示例
    • map容器排序
      • 主要技術點
      • 代碼示例

set/multiset容器

set基本概念

簡介

  • 所有元素都會在插入時被自動排序

  • set/multiset屬于關聯式容器,底層結構是用二叉樹實現

區別

  • set不允許容器中有重復的元素

  • multiset允許容器中有重復的元素

set的構造和賦值

功能描述

  • 創建set容器及賦值

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <set>using namespace std;void printSet(const set<int> &s) {for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl;
}void test() {// 默認構造set<int> s1;s1.insert(1);s1.insert(3);s1.insert(4);printSet(s1);// 拷貝構造set<int> s2(s1);printSet(s2);// 重載等號操作符set<int> s3 = s1;printSet(s3);
}int main() {test();return 0;
}

運行結果

在這里插入圖片描述

set的大小和交換

功能描述

  • 統計set大小及交換set容器

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <set>using namespace std;void printSet(const set<int> &s) {for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl;
}void test() {// 默認構造set<int> s1;s1.insert(1);s1.insert(3);s1.insert(2);s1.insert(4);printSet(s1);cout << "set容器的大小:" << s1.size() << endl;cout << "set容器是否為空:" << s1.empty() << endl;set<int> s2;s2.insert(7);s2.insert(5);cout << "交換前:" << endl;printSet(s1);printSet(s2);s1.swap(s2);cout << "交換后:" << endl;printSet(s1);printSet(s2);
}int main() {test();return 0;
}

運行結果

在這里插入圖片描述

set的插入和刪除

功能描述

  • set容器進行插入和刪除數據

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <set>using namespace std;void printSet(const set<int> &s) {for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl;
}void test() {// 默認構造set<int> s1;for (int i = 0; i < 10; i++) {// 使用insert向容器中插入元素s1.insert(i);}printSet(s1);cout << "erase刪除指定位置的元素:";s1.erase(s1.begin());cout << "erase刪除給定元素:";s1.erase(3);printSet(s1);cout << "erase 清空";s1.erase(s1.begin(), s1.end());printSet(s1);cout << "clear 清空";s1.clear();
}int main() {test();return 0;
}

運行結果

在這里插入圖片描述

set查找和統計

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <set>using namespace std;void printSet(const set<int> &s) {for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl;
}void test() {// 默認構造set<int> s1;for (int i = 0; i < 10; i++) {// 使用insert向容器中插入元素s1.insert(i);}printSet(s1);set<int>::iterator pos = s1.find(5);if (pos != s1.end()) {cout << "找到了" << endl;} else {cout << "沒找到" << endl;}cout << "統計給定值的出現次數:";int count = s1.count(2);// 對于set而言,統計的結果要么是1要么是0cout << count << endl;
}int main() {test();return 0;
}

運行結果

在這里插入圖片描述

set和multiset區別

區別

  • set不可以插入重復數據,而multiset可以

  • set插入數據的同時會返回插入結果,表示插入是否成功

  • multiset不會檢測數據,因此可以插入重復數據

代碼示例

#include <iostream>
#include <set>using namespace std;void printSet(const multiset<int> &s) {for (multiset<int>::const_iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl;
}void test1() {set<int> s;pair<set<int>::iterator, bool> ret = s.insert(1);if (ret.second) {cout << "第一次插入成功" << endl;} else {cout << "第一次插入失敗" << endl;}ret = s.insert(1);if (ret.second) {cout << "第二次插入成功" << endl;} else {cout << "第二次插入失敗" << endl;}
}void test2() {// 默認構造multiset<int> s1;s1.insert(1);s1.insert(1);s1.insert(2);s1.insert(4);s1.insert(4);s1.insert(4);printSet(s1);int count = s1.count(4);cout << "4的數量:" << count << endl;
}int main() {test1();test2();return 0;
}

運行結果

在這里插入圖片描述

pair隊組創建

功能描述

  • 成對出現數據,利用隊組可以返回兩個數據

創建方式

在這里插入圖片描述

代碼示例

#include <iostream>using namespace std;void test() {pair<int, int> p1(1, 1);cout << p1.first << " " << p1.second << endl;pair <string, string> p2 = make_pair("Tom", "cat");cout << p2.first << " " << p2.second << endl;}int main() {test();return 0;
}

運行結果

在這里插入圖片描述

set容器排序

  • set默認插入的時候是從小到大排序的

  • 利用仿函數可以實現,從大到小排序

代碼示例

#include <iostream>
#include <set>using namespace std;class Person {
public:Person(string name, int age) {this->name = name;this->age = age;}// 對于自定義的數據類型需要指定排序規則
//    bool operator<(const Person &p) const {
//        if (name < p.name) {
//            return true;
//        } else if (name > p.name) {
//            return false;
//        } else {
//            return age < p.age;
//        }
//    }
//string name;int age;
};class comparePerson{
public:bool operator()(const Person &p1, const Person &p2) {return p1.age > p2.age;}
};void printPersonSet(const set <Person> &s) {for (set<Person>::const_iterator it = s.begin(); it != s.end(); it++) {cout << "name:" << (*it).name << " age: " << (*it).age << endl;}
}void test() {Person p1("sad1", 1);Person p2("sad2", 2);Person p3("sad3", 3);Person p4("sad4", 4);Person p5("sad5", 5);Person p6("sad6", 6);Person p7("sad6", 5);set <Person, comparePerson> s1;s1.insert(p1);s1.insert(p2);s1.insert(p3);s1.insert(p4);s1.insert(p5);s1.insert(p6);s1.insert(p7);for (set<Person>::const_iterator it = s1.begin(); it != s1.end(); it++) {cout << "name:" << (*it).name << " age: " << (*it).age << endl;}
}int main() {test();return 0;
}

map容器

map基本概念

簡介

  • map中所有元素都是pair

  • pair中第一個元素為key (鍵值),起到索引作用,第二個元素為value (實值)

  • 所有元素都會根據元素的鍵值自動排序

本質

  • map/multimap屬于關聯式容器,底層結構是用二叉樹實現

優點

  • 可以根據key值快速找到value值

map和multimap區別

  • map中不允許有重復的key值元素

  • multimap中不允許有重復的key值元素

map的構造和賦值

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> mp) {for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << (*it).first << " " << (*it).second << "\t";}cout << endl;
}void test() {cout << "默認構造函數:";map<int, int> mp;for (int i = 0; i < 5; i++) {mp[i] = i;}printMap(mp);cout << "拷貝構造函數:";map<int, int> mp1(mp);printMap(mp1);cout << "重載等號操作符:";map<int, int> mp2 = mp;printMap(mp2);
}int main() {test();return 0;
}

map大小和交換

代碼示例

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> mp) {for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << (*it).first << " " << (*it).second << "\t";}cout << endl;
}void test() {cout << "默認構造函數:";map<int, int> mp;for (int i = 0; i < 5; i++) {mp[i] = i;}printMap(mp);cout << "mp大小: " << mp.size() << endl;if (mp.empty()) {cout << "mp為空" << endl;} else {cout << "mp不為空" << endl;}map<int, int> mp1;for (int i = 5; i > 0; i--) {mp1[i] = i;}printMap(mp1);cout << "交換后" << endl;mp1.swap(mp);printMap(mp);printMap(mp1);
}int main() {test();return 0;
}

map插入和刪除

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> mp) {for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << (*it).first << " " << (*it).second << "\t";}cout << endl;
}void test() {cout << "mp insert: ";map<int, int> mp;pair<int, int> p1(4, 6);pair<int, int> p2(2, 1);pair<int, int> p3(5, 3);pair<int, int> p4(1, 6);mp.insert(p1);mp.insert(p2);mp.insert(p3);mp.insert(p4);printMap(mp);cout << "mp刪除指定key: ";mp.erase(2);printMap(mp);cout << "mp刪除指定pos: ";mp.erase(mp.begin());printMap(mp);cout << "mp刪除指區間所有元素: ";mp.erase(mp.begin(), mp.end());printMap(mp);cout << "清空mp";mp.clear();printMap(mp);
}int main() {test();return 0;
}

map查找和統計

函數原型

在這里插入圖片描述

代碼示例

#include <iostream>
#include <map>using namespace std;void printMap(map<int, int> mp) {for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {cout << (*it).first << " " << (*it).second << "\t";}cout << endl;
}void test() {cout << "mp insert: ";map<int, int> mp;pair<int, int> p1(4, 6);pair<int, int> p2(2, 1);pair<int, int> p3(5, 3);pair<int, int> p4(1, 6);mp.insert(p1);mp.insert(p2);mp.insert(p3);mp.insert(p4);printMap(mp);map<int, int>::iterator pos = mp.find(3);if (pos != mp.end()) {cout << "3 find" << endl;} else {cout << "3 not find" << endl;}cout << "查找給定值出現的次數:" << mp.count(1) << endl;
}int main() {test();return 0;
}

map容器排序

主要技術點

  • 利用仿函數,可以改變函數規則

代碼示例

#include <iostream>
#include <map>using namespace std;class DescCompare {
public:// error: no matching function for call to object of type 'const DescCompare',所以需要使用const修飾bool operator()(int a, int b) const {return a > b;}
};void test() {cout << "mp insert: ";map<int, int, DescCompare> mp;pair<int, int> p1(4, 6);pair<int, int> p2(2, 1);pair<int, int> p3(5, 3);pair<int, int> p4(1, 6);mp.insert(p1);mp.insert(p2);mp.insert(p3);mp.insert(p4);for (map<int, int, DescCompare>::const_iterator it = mp.begin(); it != mp.end(); it++) {cout << (*it).first << " " << (*it).second << "\t";}cout << endl;
}int main() {test();return 0;
}

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

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

相關文章

JVM(6)

JMM JVM定義了一種Java內存模型來屏蔽掉各種硬件和操作系統的內存訪問差異,以實現讓Java程序在各種平臺下都能達到一致的內存訪問效果.在此之前,C/C直接使用物理硬件和操作系統的內存模型,因此,會由于不同平臺下的內存模型差異,有可能導致程序在一套平臺上并發完全正常,而在另…

深入解剖指針(4)

個人主頁&#xff08;找往期文章包括但不限于本期文章中不懂的知識點&#xff09;&#xff1a; 我要學編程(?_?)-CSDN博客 目錄 回調函數 qsort使用舉例 使用qsort函數排序整型數據 使用qsort排序結構數據 qsort函數的模擬實現 回調函數 回調函數就是一個通過函數指…

【Android12】Android性能調優工具SystemServerTiming日志

Android性能調優工具SystemServerTiming日志 性能優化、穩定性優化是Android系統優化的兩大方面&#xff0c;對于性能調試Android提供了很多工具&#xff0c;比如&#xff1a;bootchart、systrace、perfboot、log、dmsg等等。 SystemServerTiming是Android原生系統中一個日志…

《Spring Security 簡易速速上手小冊》第10章 未來趨勢與高級話題(2024 最新版)

文章目錄 10.1 云原生安全性趨勢10.1.1 基礎知識10.1.2 重點案例&#xff1a;保護微服務通信10.1.3 拓展案例 1&#xff1a;容器安全最佳實踐10.1.4 拓展案例 2&#xff1a;自動化安全審計和合規性檢查 10.2 反應式編程與 Spring Security10.2.1 基礎知識10.2.2 重點案例&#…

nginx-圖片模塊

./configure --with-http_image_filter_module location / {root html;index index.html index.htm;if ($arg_w "") {set $arg_w -;}if ($arg_h "") {set $arg_h -;}image_filter resize $arg_w $arg_h;image_filter_jpeg_quality 95; } 訪問: 1234…

CSS錐形漸變:conic-gradient()

畫一個扇形圖&#xff0c;使用常規方法可能很難畫&#xff0c;但是用錐形漸變的話非常好畫 <style>.pattern{width: 100px; height: 100px;border-radius: 50%;background: conic-gradient(yellow 30deg , black 30deg , black 90deg , yellow 90deg ,yellow 150d…

Git分布式版本控制系統——git學習準備工作

一、Git倉庫介紹 開發者可以通過Git倉庫來存儲和管理文件代碼&#xff0c;Git倉庫分為兩種&#xff1a; 本地倉庫&#xff1a;開發人員自己電腦上的Git倉庫 遠程倉庫&#xff1a;遠程服務器上的Git倉庫 倉庫之間的運轉如下圖&#xff1a; commit&#xff1a;提交&#xff…

Decoupled Knowledge Distillation解耦知識蒸餾

Decoupled Knowledge Distillation解耦知識蒸餾 現有的蒸餾方法主要是基于從中間層提取深層特征&#xff0c;而忽略了Logit蒸餾的重要性。為了給logit蒸餾研究提供一個新的視角&#xff0c;我們將經典的KD損失重新表述為兩部分&#xff0c;即目標類知識蒸餾&#xff08;TCKD&a…

c++之旅——第四彈

大家好啊&#xff0c;這里是c之旅第三彈&#xff0c;跟隨我的步伐來開始這一篇的學習吧&#xff01; 如果有知識性錯誤&#xff0c;歡迎各位指正&#xff01;&#xff01;一起加油&#xff01;&#xff01; 創作不易&#xff0c;希望大家多多支持哦&#xff01; 本篇文章的主…

如何對比 MySQL 主備數據的一致性?

隨著業務范圍的擴大&#xff0c;很多企業為了保障核心業務的高可用性&#xff0c;選擇了 MySQL 主從架構&#xff0c;這一套方案通常具備主備數據同步、數據備份與恢復、讀寫分離、高可用切換等特性&#xff0c;是一種相當成熟可靠的數據庫架構方案。然而這套方案在特定情況下可…

Redis小白入門教程

Redis入門教程 1. Redis入門1.1 Redis簡介1.2 Redis服務啟動與停止1.2.1 Redis下載1.2.2 服務啟動命令1.2.3 客戶端連接命令1.2.4 修改Redis配置文件 2. Redis數據類型2.1 五種常用數據類型介紹2.1.1 字符串操作命令2.1.2 哈希操作命令2.1.3 列表操作命令2.1.4 集合操作命令2.1…

雙周回顧#006 - 這三個月

斷更啦~~ 上次更新時間 2023/11/23, 斷更近三個月的時間。 先狡辯下&#xff0c;因為忙、著實忙。因為忙&#xff0c;心安理得給斷更找了個借口&#xff0c;批評下自己~~ 這三個月在做啥&#xff1f;跨部門援助&#xff0c;支援公司互聯網的 ToC 項目&#xff0c;一言難盡。 …

智能時代:人工智能引領未來創新

智能時代&#xff1a;人工智能引領未來創新 1. 人工智能的定義與特點 人工智能&#xff08;Artificial Intelligence&#xff0c;AI&#xff09;是指模擬、延伸和擴展人類智能的理論、方法、技術及應用系統的一門交叉學科。其特點包括學習能力、推理能力、感知能力和交互能力…

【C語言】InfiniBand 驅動mlx4_ib_init和mlx4_ib_cleanup

一、中文講解 這兩個函數是Linux內核模塊中對于Mellanox InfiniBand 驅動程序初始化和清理的函數。 mlx4_ib_init()函數是模塊初始化函數&#xff0c;使用__init宏標注&#xff0c;表示該函數只在模塊加載時運行一次。 函數執行的步驟如下&#xff1a; 1. 通過alloc_ordered_w…

數據結構——lesson5棧和隊列詳解

hellohello~這里是土土數據結構學習筆記&#x1f973;&#x1f973; &#x1f4a5;個人主頁&#xff1a;大耳朵土土垚的博客 &#x1f4a5; 所屬專欄&#xff1a;數據結構學習筆記 &#x1f4a5;對于順序表鏈表有疑問的都可以在上面數據結構的專欄進行學習哦~感謝大家的觀看與…

ElasticSearch開篇

1.ElasticSearch簡介 1.1 ElasticSearch&#xff08;簡稱ES&#xff09; Elasticsearch是用Java開發并且是當前最流行的開源的企業級搜索引擎。能夠達到實時搜索&#xff0c;穩定&#xff0c;可靠&#xff0c;快速&#xff0c;安裝使用方便。 1.2 ElasticSearch與Lucene的關…

Angular項目升級的一般步驟?

升級Angular項目是一個重要的任務&#xff0c;可以帶來性能改進、新功能和安全性增強等好處。以下是升級Angular項目的一般步驟&#xff1a; 1、備份項目文件&#xff1a; 在進行升級之前&#xff0c;務必對整個項目進行備份&#xff0c;以防意外情況發生。 2、查看當前版本&…

如何快速遷移其他云服務器中的網站數據到騰訊云輕量應用服務器中?教你使用寶塔Linux面板遷移網站

要快速遷移其他云服務器中的網站數據到騰訊云輕量應用服務器中&#xff0c;可以遵循以下步驟&#xff1a; 準備遷移前的工作&#xff1a;首先&#xff0c;確保你已經有了從其他云服務器到騰訊云輕量應用服務器的數據備份。這一步是為了在遷移過程中避免數據丟失或損壞。 使用寶…

模擬器抓HTTP/S的包時如何繞過單向證書校驗(XP框架)

模擬器抓HTTP/S的包時如何繞過單向證書校驗&#xff08;XP框架&#xff09; 逍遙模擬器無法激活XP框架來繞過單向的證書校驗&#xff0c;如下圖&#xff1a; ?? 解決辦法&#xff1a; 安裝JustMePlush.apk安裝Just Trust Me.apk安裝RE管理器.apk安裝Xposedinstaller_逍遙64位…

智能邊緣小站 CloudPond(低延遲、高帶寬和更好的數據隱私保護)

智能邊緣小站 CloudPond(低延遲、高帶寬和更好的數據隱私保護) 邊緣小站的主要功能是管理用戶在線下部署的整機柜設施&#xff0c;一個邊緣小站關聯一個華為云指定的區域和一個用戶指定的場地&#xff0c;相關的資源運行狀況監控等。 邊緣計算 邁入5G和AI時代&#xff0c;新…