目錄
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;
}