3 c++提高——STL常用容器(一)

目錄

1 string容器

1.1 string基本概念

1.2 string構造函數

1.3 string賦值操作

1.4 string字符串拼接

1.5 string查找和替換

1.6 string字符串比較

1.7 string字符存取

1.8 string插入和刪除

1.9 string子串

2 vector容器

2.1 vector基本概念

2.2?vector構造函數

2.3?vector賦值操作

2.4?vector容量和大小

2.5?vector插入和刪除

2.6?vector數據存取

2.7?vector互換容器

2.8?vector預留空間

3 deque容器

3.1 deque容器基本概念

3.2?deque構造函數

3.3?deque賦值操作

3.4?deque大小操作

3.5?deque插入和刪除

3.6?deque數據存取

3.7?deque排序

4 案例:評委打分

4.1 案例描述

4.2 實現步驟

4.3 示例代碼


1 string容器

1.1 string基本概念

本質:

  • string是C++風格的字符串,而string本質上是一個類

string和char *的區別:

  • char *是一個指針
  • string是一個類,類內部封裝了char *,管理這個字符串,是一個char *型的容器

特點:

string類內部封裝了很多成員方法

例如:查找find、拷貝copy、刪除delete、替換replace、插入insert

string管理char *所分配的內存,不用擔心復制越界和取值越界等,由類內部進行負責

1.2 string構造函數

函數原型:

1. string();? ? ? ? ? ? ? ? ? ? ? ? ? ?? //創建一個空的字符串,例如:string str;

????string(const char *s);? ? ? ? //使用字符串s初始化

2. string(const string &str);? ?//使用一個string對象初始化另一個string對象

3. string(int n, char c);? ? ? ? ? //使用n個字符c初始化

示例:

void test()
{//默認構造string s1;//使用字符串s初始化const char *str = "hello world";string s2(str);    //使用一個string對象初始化另一個string對象string s3(s2); //使用n個字符c初始化   string s4(10, 'a');    
}int main()
{test();return 0;
}

1.3 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賦給當前字符串

示例:

#include <string>
#include <iostream>using namespace std;void test()
{//string& operator=(const char *s);?  //char *類型字符串賦值給當前的字符串string str1;str1 = "hello world";cout << "str1 = " << str1 << endl;//string& operator=(const string *s);?//把字符串s賦值給當前的字符串string str2;str2 = str1;cout << "str2 = " << str2 << endl;//string& operator=(char c);? ? ? ? ? //把字符賦值給當前的字符串string str3;str3 = 'a';cout << "str3 = " << str3 << endl;//string& assign(const char *s);? ? ? ?//把字符串s賦給當前的字符串string str4;str4.assign("hello world");cout << "str4 = " << str4 << endl;//string& assign(const char *s, int n);?//把字符串s的前n個字符賦給當前的字符串string str5;str5.assign("hello world", 5);cout << "str5 = " << str5 << endl;//string& assign(const string &s);? ? ? //把字符串s賦給當前字符串string str6;str6.assign(str5);cout << "str6 = " << str6 << endl;//string& assign(int n, char c);? ? ? ? //用n個字符c賦給當前字符串string str7;str7.assign(10, 'a');cout << "str7 = " << str7 << endl;
}int main()
{    test();return 0;
}

1.4 string字符串拼接

函數原型:

  • string &operator+=(const char *str);? ? ? ? //重載+=操作符
  • string &operator+=(const char c);? ? ? ? ? ? //重載+=操作符
  • string &operator+=(const string &str);? ? ? //重載+=操作符
  • string &append(const char *s);? ? ? ? ? ? ? ? ?//把字符串s連接到當前字符串結尾
  • string &append(const char *s, int n);? ? ? ? //把字符串s的前n個字符連接到當前字符串結尾
  • string &append(const string &s);? ? ? ? ? ? ? //同operator+=(const string &str)
  • string &append(const string &s, int pos, int n);? //字符串s中從pos開始的n個字符連接到字符串結尾

示例:

#include <string>
#include <iostream>
using namespace std;void test()
{//string &operator+=(const char *str);? ?//重載+=操作符string str1 = "我";str1 += "愛玩游戲";cout << "str1 = " << str1 << endl;//string &operator+=(const char c);? ? ? //重載+=操作符str1 += ":";cout << "str1 = " << str1 << endl;//string &operator+=(const string &str);?//重載+=操作符string str2 = "LOL DNF";str1 += str2;cout << "str1 = " << str1 << endl;//string &append(const char *s);? ? ? ?  //把字符串s連接到當前字符串結尾string str3 = "I";str3.append(" love ");cout << "str3 = " << str3 << endl;//string &append(const char *s, int n);? //把字符串s的前n個字符連接到當前字符串結尾str3.append("game abcdf", 4);cout << "str3 = " << str3 << endl;//string &append(const string &s);? ? ? ?//同operator+=(const string &str)str3.append(str2);cout << "str3 = " << str3 << endl;//string &append(const string &s, int pos, int n);? //字符串s中從pos開始的n個字符連接到字符串結尾str3.append(str2, 0, 3); //截取LOLcout << "str3 = " << str3 << endl;
}int main()
{test();return 0;
}

1.5 string查找和替換

函數原型:

查找:

  • int find(const string &str, int pos =?0) const;? ? ?//查找str第一次出現位置,從pos開始查找
  • int find(const char *s, int pos =?0) const;? ? ? ? ??//查找s第一次出現位置,從pos開始查找
  • int find(const char *s, int pos =?0, int n) const;?//從pos位置查找s的前n個字符第一次位置
  • int find(const char c, int pos =?0) const;? ? ? ? ? ?//查找字符c第一次出現位置
  • int rfind(const string &str, int pos = npos) const; //查找str最后一次位置,從pos開始查找
  • int rfind(const char *s, int pos = npos) const;??//查找s最后一次出現位置,從pos開始查找
  • int rfind(const char *s, int pos, int n) const;? //從pos查找s的前n個字符最后一次出現位置
  • int rfind(const char *s, int pos = 0) const;? ? ?//查找字符c最后一次出現位置

替換:

  • string &replace(int pos, int n, const string &str);//替換從pos開始n個字符為字符串str
  • string &replace(int pos, int n, const char *s);? ? ?//替換從pos開始的n個字符為字符串s

示例:

#include <string>
#include <iostream>
using namespace std;//查找
void test1()
{string str = "abcdefg";//int find(const string &str, int pos =?0) const;?//查找str第一次出現位置,從pos開始查找int pos = str.find("de");cout << "pos = " << pos << endl;int pos = str.find("tg");    //查找不到就返回-1cout << "pos = " << pos << endl;//int rfind(const string &str, int pos = npos) const; //查找str最后一次位置,從pos開始查找//rfind和find的區別:rfind從右往左進行查找,find從左往右進行查找pos = str.rfind("de");cout << "pos = " << pos << endl;
}//替換
void test2()
{string str = "abcdefg";//string &replace(int pos, int n, const string &str);//替換從pos開始n個字符為字符串strstr.replace(1, 3, "1111");           //從1號位置起的3個字符"bcd"替換為"1111"cout << "str = " << str << endl;     //a1111efg
}int main()
{test1();test2();return 0;
}

1.6 string字符串比較

比較方式:

  • 字符串比較是按照字符的ASCII碼進行對比
    • =:返回0
    • >:返回1
    • <:返回-1

函數原型:

  • int compare(const string &s) const;? ? ? ? //與字符串s比較
  • int compare(const char *s) const;? ? ? ? ? ?//與字符串s比較

示例:

#include <string>
#include <iostream>
using namespace std;void test()
{string str1 = "hello";string str2 = "hello";if (str1.compare(str2) == 0){cout << "str1 等于 str2" << endl;}else if(str1.compare(str2) > 0){cout << "str1 大于 str2" << endl;}else{cout << "str1 小于 str2" << endl;}
}int main()
{test();return 0;
}

1.7 string字符存取

函數原型:

  • char &operator[](int n);? ? ? ? //通過[]方式獲取字符? ? ? ? ? ? ? ?
  • char &at(int n);? ? ? ? ? ? ? ? ? ? //通過at方法獲取字符

示例:

#include <string>
#include <iostream>
using namespace std;void test()
{string str = "hello world";//char &operator[](int n);? ? ? ? //通過[]方式取字符? ? ? ? ? ? ? ?for (int i = 0; i < str.size(); i++){cout << str[i] << " ";}cout << endl;//char &at(int n);? ? ? ? ? ? ? ? //通過at方法獲取字符for (int i = 0; i < str.size(); i++){cout << str.at[i] << " ";}cout << endl;str[0] = 'x';str.at[1] = 'y';cout << "str = " << str << endl;
}int main()
{test();return 0;
}

1.8 string插入和刪除

函數原型:

  • string &insert(int pos, const char *s);? ? ? ? //插入字符串
  • string &insert(int pos, const string &str);? ?//插入字符串
  • string &insert(int pos, int n, char c);? ? ? ? ? //在指定位置插入n個字符c
  • string &erase(int pos, int n = pos);? ? ? ? ? ? //刪除從pos開始的n個字符

示例:

#include <string>
#include <iostream>
using namespace std;void test()
{string str = "hello";//string &insert(int pos, const char *s);? ? ? ?//插入字符串str.insert(1, "111");cout << "str = " << str << endl;    //h111ello//string &erase(int pos, int n = pos);? ? ? ? ? //刪除從pos開始的n個字符str.insert(1, 3);cout << "str = " << str << endl;    //hello
}int main()
{test();return 0;
}

1.9 string子串

函數原型:

  • string substr(int pos = 0, int npos) const;? ? ? ? //返回由pos開始的n個字符組成的字符串

示例:

#include <string>
#include <iostream>
using namespace std;void test1()
{string str = "abcdef";string substr = str.substr(1, 3);cout << "substr = " << substr << endl;    //bcd
}void test2()
{string email = "zhangsan@sina.com";//從郵箱中獲取用戶信息int pos = email.find("@");string username = email.substr(0, pos);cout << "username = " << username << endl;    //zhangsan
}int main()
{test1();test2();return 0;
}

2 vector容器

2.1 vector基本概念

功能:

  • vector數據結構和數組非常相似,也稱為單端數組

vector和普通數組區別:

  • 不同之處在于數組是靜態空間,而vector可以動態擴展

動態擴展:

  • 并不是在原空間后續接新空間,而是找更大的內存空間,然后將原數據拷貝新空間,釋放原空間。
  • vector容器的迭代器是支持隨機訪問的迭代器。

2.2?vector構造函數

函數原型:

  • vector<T> v;? ? ? ? ????????????????//采用模板實現類實現,默認構造函數
  • vector(v.begin(),?v.end());? ? //將v[begin(), end()]區間中的元素拷貝給本身
  • vector(n, elem);? ? ? ? ? ? ? ? ? ?//構造函數將n個elem拷貝給本身
  • vector(const vector &vec);? //拷貝構造函數

示例:

#include <iostream>
#include <vector>
using namespace std;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.pushback(i);}printVector(v1);//通過區間方式進行構造vector<int> v2(v1.begin(),?v1.end());printVector(v2);//n個elem方式構造vector<int> v3(10, 100);printVector(v3);//拷貝構造函數vector<int> v4(v3);printVector(v4);
}int main()
{test();return 0;
}

2.3?vector賦值操作

函數原型:

  • vector &operator=(const vector &vec);? ? ? ? //重載等號操作符
  • assign(beg, end);? ? ? ? ? ? ? ? //將[beg, end]區間中的數據拷貝賦值給本身
  • assign(n, elem);? ? ? ? ? ? ? ? ? //將n個elem拷貝賦值給本身

示例:

#include <vector>
#include <iostream>
using namespace std;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.pushback(i);}printVector(v1);//等號賦值vector<int> v2;v2 = v1;printVector(v2);//assign賦值vector<int> v3;v3.assign(v1.begin(), v1.end());printVector(v3);//assign賦值:n個elemvector<int> v4;v4.assign(10, 100);printVector(v4);
}int main()
{test();return 0;
}

2.4?vector容量和大小

函數原型:

  • empty();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷容器是否為空
  • capacity();? ? ? ? ? ? ? ? ? ? ? ? ? //容器的容量
  • size();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//返回容器中元素的個數
  • resize(int num);? ? ? ? ? ? ? ? ? //重新指定容器的長度為num,若容器變長,則以默認值0填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除
  • resize(int num, elem);? ? ? ? //重新指定容器的長度為num,若容器變長,則以elem值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除

示例:

#include <vector>
#include <iostream>
using namespace std;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.pushback(i);}printVector(v1);if (v1.empty())    //為真,代表容器為空{cout << "v1為空" << endl;}else{cout << "v1不為空" << endl;cout << "v1的容量為:" << v1.capacity() << endl;    //13cout << "v1的大小為:" << v1.size() << endl;        //10}//重新指定大小v1.resize(15, 100);printVector(v1);v1.resize(5);printVector(v1);
}int main()
{test();return 0;
}

2.5?vector插入和刪除

函數原型:

  • push_back(elem);? ? ? ? //尾部插入元素elem
  • pop_back();? ? ? ? ? ? ? ? ?//刪除最后一個元素
  • insert(const_iterator pos, elem);? ? ? ? ? ? ? ? ? ?//迭代器指向位置插入元素elem
  • insert(const_iterator pos, int count, elem);? ?//迭代器指向位置pos插入count個元素elem
  • erase(const_iterator pos);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除迭代器指向的元素
  • erase(const_iterator start, const_iterator end);? //刪除迭代器從start到end之間的元素
  • clear();? ? ? ? //刪除容器中所有元素

示例:

#include <string>
#include <iostream>
using namespace std;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;//尾插法//push_back(elem);? ? ? ? //尾部插入元素elemv1.push_back(10);v1.push_back(20);v1.push_back(30);v1.push_back(40);v1.push_back(50);//遍歷printVector(v1);//尾刪//pop_back();? ? ? ? ? ? ?//刪除最后一個元素v1.pop_back();printVector(v1);//insert(const_iterator pos, elem);? ? ?//迭代器指向位置插入元素elemv1.insert(v1.begin(), 100);printVector(v1);//insert(const_iterator pos, int count, elem);?//迭代器指向位置pos插入count個元素elemv1.insert(v1.begin(), 2, 1000);printVector(v1);//erase(const_iterator pos);? ? ? ? ? ? //刪除迭代器指向的元素v1.erase(v1.begin());printVector(v1);//erase(const_iterator start, const_iterator end);? //刪除迭代器從start到end之間的元素v1.erase(v1.begin(), v1.end());printVector(v1);//clear();? ? ? ? //刪除容器中所有元素v1.clear();?printVector(v1);
}int main()
{test();return 0;
}

2.6?vector數據存取

函數原型:

  • at(int idx);? ? ? ? //返回索引idx所指的數據
  • operator[];? ? ? ?//返回索引idx所指的數據
  • front();? ? ? ? ? ? ?//返回容器中的第一個元素
  • back();? ? ? ? ? ? //返回容器中最后一個數據元素

示例:

#include <vector>void test()
{vector<int> v;for (int i = 0; i < v.size(); i++){v.push_back(i);}//利用[]方式訪問數組中元素for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;//利用at方式訪問數組中元素for (int i = 0; i < v.size(); i++){cout << v.at(i) << " ";}cout << endl;//front();? ? ? ? ?//返回容器中的第一個元素cout << "第一個元素為:" << v.front() << endl;//back();? ? ? ? ? //返回容器中最后一個數據元素cout << "第一個元素為:" << v.back() << endl;
}int main()
{test();return 0;
}

2.7?vector互換容器

函數原型:

  • swap(vec);? ? ? ? //將vec與本身的元素互換

示例:

#include <vector>
#include <iostream>
using namespace std;void printVector(vector<int> &v)
{for (vector<int>::iterator it = v.begin(); it != v.end(); it++){cout << *it << " ";}cout << endl;
}void test1()
{vector<int> v1;for (int i = 0; i < 10; i++){v1.pushback(i);}printVector(v1);vector<int> v2;for (int i = 10; i > 0; i--){v2.pushback(i);}printVector(v2);cout << "交換后:" << endl;//swap(vec);? ? ?  //將vec與本身的元素互換v1.swap(v2);printVector(v1);printVector(v2);
}//實際應用:巧用swap可以收縮內存空間
void test2()
{vector<int> v;for (int i = 0; i < 100000; i++){v.pushback(i);}cout << "v的容量為:" << v.capacity() << endl;    //138255cout << "v的大小為:" << v.size() << endl;        //100000v.resize(3);     //更改大小為3cout << "v的容量為:" << v.capacity() << endl;    //138255cout << "v的大小為:" << v.size() << endl;        //3//巧用swap收縮內存vector<int> (v).swap(v); //匿名對象(執行完這行后,會被系統釋放),類似于拷貝構造函數cout << "v的容量為:" << v.capacity() << endl;    //3cout << "v的大小為:" << v.size() << endl;        //3
}int main()
{test1();test2();return 0;
}

2.8?vector預留空間

描述:

  • 減少vector在動態擴展容量時的擴展次數

函數原型:

  • reserve(int len);//容器預留len個元素長度,預留位置不初始化,元素不可訪問

示例:

#include <vector>
#include <iostream>
using namespace std;void test1()
{vector<int> v;int num = 0;    //統計開辟次數int *p = NULL;if (p != &v[0]){v.push_back(i);if (p != &v[0])   //若p不指向v的v的首元素地址,即v進行了動態擴展,更換了地址{p = &v[0];num++;        //統計vector開辟的次數}}cout << "num = " << num << endl;    //30次//預留空間v.reserve(100000);
}void test1()
{vector<int> v;int num = 0;int *p = NULL;//預留空間v.reserve(100000);if (p != &v[0]){v.push_back(i);if (p != &v[0]){p = &v[0];num++;}}cout << "num = " << num << endl;    //1次
}int main()
{test1();test2();return 0;
}

3 deque容器

3.1 deque容器基本概念

功能:

  • 雙端數組,可以對頭端進行插入刪除操作。

deque和vector的區別:

  • vector對于頭部的插入效率低,數據量越大,效率越低。
  • deque相對而言,對頭部的插入刪除速度會比vector塊。
  • vector訪問元素時的速度會比deque快,這和兩者內部實現有關。

deque內部工作原理:

deque內部有個中控器,維護每段緩沖區中的內容,緩沖區中存放真實數據。中控器維護的是每個緩沖區的地址,使得使用deque時像一片連續的內存空間。

  • deque容器的迭代器也是支持隨機訪問的。?

3.2?deque構造函數

函數原型:

  • deque(T);? ? ? ? //默認構造形式
  • deque(begin, end);? ? ? ? //構造函數將[begin, end]區間中的元素拷貝給本身
  • deque(n, elem);? ? ? ? //構造函數將n個elem拷貝給本身
  • deque(const deque &deq);? ? ? ? //拷貝構造函數

示例:

#include <deque>
#include <iostream>
using namespace std;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);//將n個elem拷貝給本身deque<int> d3(10, 100);printDeque(d3);//拷貝構造函數deque<int> d4(d3);printDeque(d4);
}int main()
{test();return 0;
}

3.3?deque賦值操作

函數原型:

  • deque& operator=(const deque &deq); //重載等號操作符
  • assign(begin, end);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //將[begin, end]區間中的數據拷貝賦值給本身
  • assign(n, elem);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//將n個elem拷貝賦值給本身

示例:

#include <deque>
#include <iostream>
using namespace std;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;printDeque(d2);//assign: 將[begin, end]區間中的數據拷貝賦值給本身deque<int> d3;d3.assign(d1.begin(), d3.end());printDeque(d3);//assign: 將n個elem拷貝賦值給本身deque<int> d4;d4.assign(10, 100);printDeque(d4);
}int main()
{test();return 0;
}

3.4?deque大小操作

函數原型:

  • deque.empty();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷容器是否為空
  • deque.size();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//返回容器中元素的個數
  • deque.resize(int num);? ? ? ? ? ? ? ? ? //重新指定容器的長度為num,若容器變長,則以默認值0填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除
  • deque.resize(int num, elem);? ? ? ? //重新指定容器的長度為num,若容器變長,則以elem值填充新位置。如果容器變短,則末尾超出容器長度的元素被刪除

注意:沒有容量capacity概念,因為內部結構中沒有容量限制,可以無限開拓空間,只用在中控器加一個地址就可以維護。

示例:

#include <deque>
#include <iostream>
using namespace std;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);if (d1.empty()){cout << "d1為空!"<< endl;}else{cout << "d1為空!"<< endl;cout << "d1的大小為:"<< d1.size() << endl;}//重新指定大小d1.resize(15);printDeque(d1);d1.resize(12, 1);printDeque(d1);
}int main()
{test();return 0;
}

3.5?deque插入和刪除

函數原型:

  • 兩端插入操作:
    • push_back(elem);? ? ? ? //在容器尾部添加一個數據
    • push_front(elem);? ? ? ? //在容器頭部插入一個數據
    • pop_back();? ? ? ? ? ? ? ? ?//刪除容器最后一個數據
    • pop_front();? ? ? ? ? ? ? ? ?//刪除容器第一個數據
  • 指定位置操作:
    • insert(pop, elem);? ? ? ? ? ? //在pos位置插入一個elem元素的拷貝,返回新數據的位置
    • insert(pos, n, elem);? ? ? ? //在pos位置插入n個elem數據,無返回值
    • insert(pos, begin, end);? ?//在pos位置插入[begin, end)區間的數據,無返回值
    • clear();? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//清空容器的所有數據
    • erase(begin, end);? ? ? ? ? ?//刪除[begin, end]區間的數據,返回下一個數據的位置
    • erase(pos);? ? ? ? ? ? ? ? ? ? ? //刪除pos位置的數據,返回下一個數據的位置

示例:

#include <deque>
#include <iostream>
using namespace std;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;//尾插d1.push_back(10);d1.push_back(20);d1.push_back(30);d1.push_back(40);//頭插d1.push_front(100);d1.push_front(200);printDeque(d1);    //200 100 10 20 30 40//尾刪d1.pop_back();printDeque(d1);    //200 100 10 20 30//頭刪d1.pop_front();printDeque(d1);    //100 10 20 30//insert插入d1.insert(d1.begin(), 1000);printDeque(d1);    //1000 100 10 20 30//insert插入d1.insert(d1.begin(), 2, 10000);printDeque(d1);    //10000 10000 1000 100 10 20 30deque<int> d2;d2.push_back(1);d2.push_back(2);d2.push_back(3);//insert插入d1.insert(d1.begin(), d2.begin(), d2.end());printDeque(d1);    //1 2 3 10000 10000 1000 100 10 20 30//刪除deque<int>::iterator it = d1.begin();it++;d1.erase(it);printDeque(d1);    //1 3 10000 10000 1000 100 10 20 30d1.erase(it, d1.end());printDeque(d1);    //1d1.clear();printDeque(d1);    //\nint main()
{test();return 0;
}

3.6?deque數據存取

函數原型:

  • at(int idx);? ? ? ? //返回索引idx所指的數據
  • operator[];? ? ? ? //返回索引idx所指的數據
  • front();? ? ? ? ? ? ? //返回容器中第一個數據元素
  • back();? ? ? ? ? ? ? //返回容器中最后一個數據元素

示例:

#include <deque>
#include <iostream>
using namespace std;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> d;d.push_back(10);d.push_back(20);d.push_back(30);d.push_front(100);d.push_front(200);d.push_front(300);printDeque(d);//通過[]方式訪問元素for (int i = 0; i < d.size(); i++){cout << d[i] << " ";}cout << endl;//利用at方式訪問數組中元素for (int i = 0; i < d.size(); i++){cout << d.at(i) << " ";}cout << endl;//front();? ? ? ? ?//返回容器中的第一個元素cout << "第一個元素為:" << d.front() << endl;//back();? ? ? ? ? //返回容器中最后一個數據元素cout << "第一個元素為:" << d.back() << endl;
}int main()
{test();return 0;
}

3.7?deque排序

函數原型:

  • sort(iterator begin, iterator end);? ? ? ? //對begin和end區間內元素進行排序

注意:對于支持隨機訪問的迭代器容器,都可以利用sort算法直接對其進行排序。

示例:

#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;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> d;d.push_back(10);d.push_back(20);d.push_back(30);d.push_front(100);d.push_front(200);d.push_front(300);printDeque(d);//排序:默認排序規則為從小到大(升序)sort(d.begin(), d.end());printDeque(d);
}int main()
{test();return 0;
}

4 案例:評委打分

4.1 案例描述

有5名選手ABCDE,10各評委分別對每一名選手打分,去除最高分,去除最低分,取平均分。

4.2 實現步驟

1. 創建5名選手,放到vector中

2. 遍歷vector容器,取出來每一個選手,執行for循環,可以把10個評分打分存到vector容器中

3. sort算法對deque容器中分數排序,去除最高和最低分

4. deque容器遍歷一遍,累加總分

5. 獲取平均分

4.3 示例代碼

#include <vector>
#include <ctime>
#include <deque>
#include <algorithm>
#include <iostream>
using namespace std;class Person
{
public:Person(string name, int score){m_Name = name;m_Score = score;}string m_Mame;int m_Score;
};void createPerson(vector<Person> &v)
{string nameSeed = "ABCDE";int score = 0;for (int i = 0; i < 5 ;i++){string name = "Player_";name += nameSeed[i];Person p(name, score);v.push_back(p);}
}void setScore(vector<Person> &v)
{for (vector<Person>::iterator it = v.begin(); it != v.end(); it++){//評委的分數deque<int> d;for (int i = 0; i < 10; i++){int score = rand() % 41 + 60; //60-100的隨機數d.push_back(score);}cout << "選手" << it->m_Name << "的打分為:" << endl;for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++){cout << *dit << " ";}cout << endl;//分數排序sort(d.begin(), d.end());//去除最高最低分d.pop_back();d.pop_front();//取平均分int sum = 0;for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++){sum += *dit;}int avg = sum / d.size();//將平均分賦值給選手it->m_Score = avg;}
}void showScore(vector<Person> &v)
{for (vector<Person>::iterator it = v.begin(); it != v.end(); it++){cout << "選手" << it->m_Name << "的打分為:" << it->m_Score << endl;}
}int main()
{//隨機數種子srand((unsigned int)time(NULL));//1. 創建5名選手,放到vector中vector<Person> v;createPerson(v);//2. 給每個選手打分setScore(v);//3. 顯示最后得分showScore(v);return 0;
}

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

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

相關文章

手把手教你用【Go】語言調用DeepSeek大模型

1、首先呢&#xff0c;點擊 “DeepSeek”” 這個&#xff0c; 可以充1塊玩玩。 2、然后獲取api-key 3、替換apiKey const (apiURL "https://api.deepseek.com/v1/chat/completions"apiKey "your api key" // 替換為你的實際 API KeymodelName &…

自動化UI測試工具TestComplete的核心功能及應用

對桌面應用穩定性與用戶體驗的挑戰&#xff0c;手動測試效率低、覆蓋有限&#xff0c;而普通自動化工具常難以應對復雜控件識別、腳本靈活性和大規模并行測試的需求。 自動化UI測試工具TestComplete憑借卓越的對象識別能力、靈活的測試創建方式以及高效的跨平臺并行執行功能&a…

【C/C++】邁出編譯第一步——預處理

【C/C】邁出編譯第一步——預處理 在C/C編譯流程中&#xff0c;預處理&#xff08;Preprocessing&#xff09;是第一個也是至關重要的階段。它負責對源代碼進行初步的文本替換與組織&#xff0c;使得編譯器在后續階段能正確地處理規范化的代碼。預處理過程不僅影響編譯效率&…

快捷鍵——VsCode

一鍵折疊所有的代碼塊 先按 ctrl K&#xff0c;再ctrl 0 快速注釋一行 ctrl /

import 和require的區別

概念 import 是es6 規范&#xff0c;主要應用于瀏覽器和主流前端框架當中&#xff0c;export 導出&#xff0c; require 是 commonjs 規范&#xff0c;主要應用于nodejs環境中&#xff0c;module.exports 導出編譯規則 import 靜態導入是編譯時解析&#xff0c;動態導入是執…

8、鴻蒙Harmony Next開發:相對布局 (RelativeContainer)

目錄 概述 基本概念 設置依賴關系 設置參考邊界 設置錨點 設置相對于錨點的對齊位置 子組件位置偏移 多種組件的對齊布局 組件尺寸 多個組件形成鏈 概述 RelativeContainer是一種采用相對布局的容器&#xff0c;支持容器內部的子元素設置相對位置關系&#xff0c;適…

Linux命令的命令歷史

Linux下history命令可以對當前系統中執行過的所有shell命令進行顯示。重復執行命令歷史中的某個命令&#xff0c;使用&#xff1a;!命令編號&#xff1b;環境變量histsize的值保存歷史命令記錄的總行數&#xff1b;可用echo查看一下&#xff1b;需要大寫&#xff1b;環境變量hi…

【C++小白逆襲】內存管理從崩潰到精通的秘籍

目錄【C小白逆襲】內存管理從崩潰到精通的秘籍前言&#xff1a;為什么內存管理讓我掉了N根頭發&#xff1f;內存四區大揭秘&#xff1a;你的變量都住在哪里&#xff1f;&#x1f3e0;內存就像大學宿舍區 &#x1f3d8;?C語言的內存管理&#xff1a;手動搬磚時代 &#x1f9f1;…

【網絡安全】利用 Cookie Sandwich 竊取 HttpOnly Cookie

未經許可,不得轉載。 文章目錄 引言Cookie 三明治原理解析Apache Tomcat 行為Python 框架行為竊取 HttpOnly 的 PHPSESSID Cookie第一步:識別 XSS 漏洞第二步:發現反射型 Cookie 參數第三步:通過 Cookie 降級實現信息泄露第四步:整合攻擊流程修復建議引言 本文將介紹一種…

【工具】什么軟件識別重復數字?

網上的數字統計工具雖多&#xff0c;但處理重復數字時總有點不盡如人意。 要么只能按指定格式輸入&#xff0c;要么重時得手動一點點篩&#xff0c;遇上數據量多的情況&#xff0c;光是找出重復的數字就得另外花不少功夫。? 于是我做了個重復數字統計器&#xff0c;不管是零…

CSS分層渲染與微前端2.0:解鎖前端性能優化的新維度

CSS分層渲染與微前端2.0&#xff1a;解鎖前端性能優化的新維度 當你的頁面加載時間超過3秒&#xff0c;用戶的跳出率可能飆升40%以上。這并非危言聳聽&#xff0c;而是殘酷的現實。在當前前端應用日益復雜、功能日益臃腫的“新常態”下&#xff0c;性能優化早已不是錦上添花的“…

AI Agent開發學習系列 - langchain之Chains的使用(5):Transformation

Transformation&#xff08;轉換鏈&#xff09;是 LangChain 中用于“自定義數據處理”的鏈式工具&#xff0c;允許你在鏈路中插入任意 Python 代碼&#xff0c;對輸入或中間結果進行靈活處理。常用于&#xff1a; 對輸入/輸出做格式化、過濾、摘要、拆分等自定義操作作為 LLMC…

Druid 連接池使用詳解

Druid 連接池使用詳解 一、Druid 核心優勢與架構 1. Druid 核心特性 特性說明價值監控統計內置 SQL 監控/防火墻實時查看 SQL 執行情況防 SQL 注入WallFilter 防御機制提升系統安全性加密支持數據庫密碼加密存儲符合安全審計要求擴展性強Filter 鏈式架構自定義功能擴展高性能…

9.2 埃爾米特矩陣和酉矩陣

一、復向量的長度 本節的主要內容可概括為&#xff1a;當對一個復向量 z\pmb zz 或復矩陣 A\pmb AA 轉置后&#xff0c;還要取復共軛。 不能在 zTz^TzT 或 ATA^TAT 時就停下來&#xff0c;還要對所有的虛部取相反的符號。對于一個分量為 zjajibjz_ja_jib_jzj?aj?ibj? 的列向…

AI驅動的低代碼革命:解構與重塑開發范式

引言&#xff1a;低代碼平臺的范式轉移 當AI技術與低代碼平臺深度融合&#xff0c;軟件開發正經歷從"可視化編程"到"意圖驅動開發"的根本性轉變。這種變革不僅提升了開發效率&#xff0c;更重新定義了人與系統的交互方式。本文將從AI介入的解構層次、交互范…

zookeeper etcd區別

ZooKeeper與etcd的核心區別體現在設計理念、數據模型、一致性協議及適用場景等方面。?ZooKeeper基于ZAB協議實現分布式協調&#xff0c;采用樹形數據結構和臨時節點特性&#xff0c;適合傳統分布式系統&#xff1b;而etcd基于Raft協議&#xff0c;以高性能鍵值對存儲為核心&am…

模擬注意力:少量參數放大 Attention 表征能力

論文標題 SAS: Simulated Attention Score 論文地址 https://arxiv.org/pdf/2507.07694 代碼 見論文附錄 作者背景 摩根士丹利&#xff0c;斯坦福大學&#xff0c;微軟研究院&#xff0c;新加坡國立大學&#xff0c;得克薩斯大學奧斯汀分校&#xff0c;香港大學 動機 …

零基礎|寶塔面板|frp內網穿透|esp32cam遠程訪問|微信小程序

1.準備好阿里云服務器和寶塔面板2.安裝frp服務端3.測試(密碼賬號在詳情里面)4.配置客戶端#一、沒有域名情況下 [common] server_addr #公網ip地址&#xff0c;vps server_port 7000 服務的bind_port token 12121212 [httpname] type tcp # 沒有域名情況下使用 tcp local_i…

Spring Boot整合MyBatis+MySQL+Redis單表CRUD教程

Spring Boot整合MyBatisMySQLRedis單表CRUD教程 環境準備 1. Redis安裝&#xff08;Windows&#xff09; # 下載Redis for Windows # 訪問: https://github.com/tporadowski/redis/releases # 下載Redis-x64-5.0.14.1.msi并安裝# 啟動Redis服務 redis-server# 測試連接 redis-c…

linux學習第30天(線程同步和鎖)

線程同步協同步調&#xff0c;對公共區域數據按序訪問。防止數據混亂&#xff0c;產生與時間有關的錯誤。數據混亂的原因資源共享(獨享資源則不會)調度隨機(意味著數據訪問會出現競爭)線程間缺乏必要同步機制鎖的使用建議鎖&#xff01;對公共數據進行保護。所有線程【應該】在…