欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ STL入門教程(7) multimap、multiset的使用

 更新時間:2017年08月18日 16:50:33   作者:synapse7  
這篇文章主要介紹了C++ STL入門教程第七篇,multimap一對多索引,multiset多元集合的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、multimap(一對多索引)

C++ multimap和map所支持的操作相同(除了multimap不支持下標運算),但是multimap允許重復的元素。

完整程序代碼:

/*請務必運行以下程序后對照閱讀*/ 
 
///頭文件依舊是map 
#include <map> 
#include <string> 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  ///1. 初始化 
  multimap<int, string> mapStudent; 
  multimap<int, string>::iterator iter, beg, end; 
   
  ///2. 添加元素 
  ///multimap不支持下標操作 
  mapStudent.insert(pair<int, string>(0, "student_one")); 
  mapStudent.insert(pair<int, string>(0, "student_one_copy"));///一對多 
  mapStudent.insert(pair<int, string>(1, "student_two")); 
  mapStudent.insert(pair<int, string>(5, "Fear Kubrick")); 
  mapStudent.insert(pair<int, string>(2, "Akemi Homura")); 
  mapStudent.insert(pair<int, string>(-1, "Eren Jaeger")); 
  mapStudent.insert(pair<int, string>(99, "lin")); 
  cout << mapStudent.size() << endl; 
  cout << endl; 
   
  ///3. 遍歷 
  for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
   
  ///4. 單鍵查詢與范圍查詢 
  ///單鍵查詢 
  int count = mapStudent.count(0); 
  iter = mapStudent.find(0); 
  for (int i = 0; i < count; i++, iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
  ///范圍查詢 
  beg = mapStudent.lower_bound(1);/// >=1 
  end = mapStudent.upper_bound(5);/// <=5 
  for (; beg != end; beg++) 
    cout << beg->first << " " << beg->second << endl; 
  cout << endl; 
   
  ///5. 刪除 
  iter = mapStudent.find(1); 
  mapStudent.erase(iter); 
  cout << mapStudent.size() << endl; 
  for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
    cout << iter->first << " " << iter->second << endl; 
  cout << endl; 
   
  ///6. 判空與清空 
  if (!mapStudent.empty()) 
    mapStudent.clear(); 
} 

二、multiset(多元集合)

多元集合(multiset)和集合(set)所支持的操作相同,只不過支持重復對象。
它是<set>庫中一個非常有用的類型,它可以看成一個序列,插入一個數(shù),刪除一個數(shù)都能夠在O(log n)的時間內(nèi)完成,而且他能時刻保證序列中的數(shù)是有序的,而且序列中可以存在重復的數(shù)。
PS:與priority_queue(優(yōu)先隊列)相比,multiset取出任意一個元素要O(log n),但priority_queue要O(n)。(這就是它叫做queue的原因)

完整程序代碼:

/*請務必運行以下程序后對照閱讀*/ 
 
///頭文件依舊為set 
#include <set> 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  ///1. 初始化 
  multiset<int> num; 
  multiset<int>::iterator iter,beg,end; 
  cout << num.max_size() << endl;///multiset容納上限 
  cout << endl; 
 
  ///2. 添加元素 
  for (int i = 0; i < 10; i++) 
    num.insert(i); 
  cout << num.size() << endl; 
  cout << endl; 
 
  ///3. 遍歷 
  for (iter = num.begin(); iter != num.end(); iter++) 
    cout << *iter << " " ; 
  cout << endl; 
  cout << endl; 
 
  ///4. 查詢 
 
  iter = num.find(1); 
  if (iter != num.end()) 
    cout << *iter << endl; 
  else 
    cout << -1 << endl; 
 
  iter = num.find(99); 
  if (iter != num.end()) 
    cout << *iter << endl; 
  else 
    cout << -1 << endl; 
  cout << endl; 
 
  beg=num.lower_bound(2); 
  end=num.upper_bound(7); 
  for (; beg != end; beg++) 
    cout << *beg << " " ; 
  cout << endl; 
 
  ///5. 刪除 
  iter = num.find(1); 
  num.erase(iter); 
  cout << num.size() << endl; 
  for (iter = num.begin(); iter != num.end(); iter++) 
    cout << *iter << " " ; 
  cout << endl; 
  cout << endl; 
 
  ///6. 判空與清空 
  if (!num.empty()) 
    num.clear(); 
} 

參考網(wǎng)址:

http://www.cplusplus.com/reference/map/multimap/

http://www.cplusplus.com/reference/set/multiset/

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++利用opencv實現(xiàn)人臉檢測

    C++利用opencv實現(xiàn)人臉檢測

    這篇文章主要為大家詳細介紹了C++利用opencv實現(xiàn)人臉檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++?Protobuf的學習使用指南

    C++?Protobuf的學習使用指南

    protocol?buffers是一種語言無關、平臺無關、可擴展的序列化結(jié)構(gòu)數(shù)據(jù)的方法,它可用于(數(shù)據(jù))通信協(xié)議、數(shù)據(jù)存儲等,下面就來跟隨小編一起簡單學習一下它的使用吧
    2023-07-07
  • 詳解C++ 共享數(shù)據(jù)保護機制

    詳解C++ 共享數(shù)據(jù)保護機制

    這篇文章主要介紹了詳解C++ 共享數(shù)據(jù)保護機制的相關資料,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下
    2021-02-02
  • Visual Studio Community 2022(VS2022)安裝圖文方法

    Visual Studio Community 2022(VS2022)安裝圖文方法

    這篇文章主要介紹了Visual Studio Community 2022(VS2022)安裝方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • C語言中關于動態(tài)內(nèi)存分配的詳解

    C語言中關于動態(tài)內(nèi)存分配的詳解

    動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存。棧上分配的內(nèi)存是由系統(tǒng)分配和釋放的,空間有限,在復合語句或函數(shù)運行結(jié)束后就會被系統(tǒng)自動釋放而堆上分配的內(nèi)存則不會有這個問題。
    2021-09-09
  • C++簡明講解缺省參數(shù)與函數(shù)重載的用法

    C++簡明講解缺省參數(shù)與函數(shù)重載的用法

    所謂缺省參數(shù),顧名思義,就是在聲明函數(shù)的某個參數(shù)的時候為之指定一個默認值,在調(diào)用該函數(shù)的時候如果采用該默認值,你就無須指定該參數(shù)。C++ 允許多個函數(shù)擁有相同的名字,只要它們的參數(shù)列表不同就可以,這就是函數(shù)的重載,借助重載,一個函數(shù)名可以有多種用途
    2022-06-06
  • C語言const關鍵字的用法詳解

    C語言const關鍵字的用法詳解

    今天探討const,首先來說是將變量常量化。為什么要將變量常量化,原因有諸多好處有諸多。比如可以使數(shù)據(jù)更加安全不會被修改
    2022-08-08
  • C語言零基礎徹底掌握預處理下篇

    C語言零基礎徹底掌握預處理下篇

    在C語言的程序中包括各種以符號#開頭的編譯指令,這些指令稱為預處理命令。預處理命令屬于C語言編譯器,而不是C語言的組成部分,通過預處理命令可擴展C語言程序設計的環(huán)境
    2022-08-08
  • C++ Boost Atomic詳細講解

    C++ Boost Atomic詳細講解

    Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱
    2022-11-11
  • 深入理解二叉樹的非遞歸遍歷

    深入理解二叉樹的非遞歸遍歷

    本篇文章是對二叉樹的非遞歸遍歷進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05

最新評論