日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第二十三模板 18标准模板库

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第二十三模板 18标准模板库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//第二十三模板 18標準模板庫 //1 容器 容器是包含其他對像的對像,標準C++ 庫中提供了一系列的容器類 //可以分為兩種類型,順序和關聯類型,順序容器可提供對自身元素的順序或者隨機訪問,關聯容器則過濾掉了一些元素,只按關鍵值訪問有關元素//2 順序容器 //標準C++庫提供了三種順序容器,分別為vector list 和deque//2.1 向量容器 /*#include <iostream> #include <vector> #include <string> using namespace std; const int num=2; int main() {vector<double>price(num);vector<string>book(num);cout<<"開始錄入"<<num<<"本書的數據"<<endl;for(int i=0; i<num; i++){cout<<"請輸入第"<<i+1<<"本書的書名:";getline(cin,book[i]);cout<<"請輸入價格:";cin>>price[i];cin.ignore();}for(int i=0; i<num; i++){cout<<"第"<<i+1<<"本書的書名:";cout<<book[i]<<"\t"<<"價格:"<<price[i]<<endl;}cout<<"max_size:"<<book.max_size()<<endl;return 0; }*//* #include <iostream> #include <string> #include <vector> using namespace std; class people { public:people();people(const string&name, const int age);people(const people&r);~people();void SetName(const string&name);string GetName()const;void SetAge(const int age);int GetAge()const;people&operator=(const people&r); private:string theName;int theAge; };people::people():theName("默認創建的新人"),theAge(52){}people::people(const string&name, const int age):theName(name),theAge(age){}people::people(const people&r):theName(r.GetName()),theAge(r.GetAge()) {cout<<"復制構造函數"<<endl; } people::~people(){cout<<"析構函數執行"<<endl;}void people::SetName(const string&r) {theName = r; }string people::GetName()const {return theName; }void people::SetAge(const int age) {theAge = age; }int people::GetAge()const {return theAge; }people&people::operator=(const people&r) {cout<<"operator=函數執行";theName = r.GetName();theAge = r.GetAge();return *this; }ostream &operator<<(ostream&out, const people&r) {out<<r.GetName()<<"的年齡是:"<<r.GetAge()<<endl;return out; }template<class T> void show(const vector<T>&v); //定認一個容器參數為一個模板值 typedef vector<people>man; //man的在這里定義一個容器,里面放的是people int main() {people Jack;people Mary("Mark",24);people Tom("Tom",18);people Jesson("Jesson",29);man non;cout<<"non:"<<endl;show(non);man manyMan(3);cout<<"manyMan(3):"<<endl;show(manyMan);manyMan[0] = Mary;manyMan[1] = Tom;manyMan[2] = Jesson;cout<<"為容器manyMan(3)分配個人后:"<<endl;show(manyMan);people Ketty("Ketty",58);manyMan.push_back(Ketty);manyMan.push_back(Jack);cout<<"manyMan()增加二個人后:"<<endl;show(manyMan);manyMan[0].SetName("Rose");manyMan[0].SetAge(16);cout<<"設置manyMan[0]后:"<<endl;system("pause");return 0; }template<class T> void show(const vector<T> &v) {cout<<"max_size()="<<v.max_size();cout<<"\tsize()="<<v.size();cout<<"\t capacity()="<<v.capacity();cout<<"\t"<<(v.empty()?"空":"不為空");cout<<endl;for(int i=0; i<v.size(); ++i){cout<<v[i]<<endl;}cout<<endl; }*/ /* Constructors() 構造函數 Operators() 對vector進行賦值或比較 assign() 對vector中的元素賦值 at() 返回指定位置的元素 back() 返回最末一個元素 begin() 返回第一個元素的迭代器 capacity() 返回vector所能容納的元素數量(在不重新分配內存的情況下) clear() 清空所有元素 empty() 判斷Vector是否為空(返回true時為空) end() 返因最末元素的迭代器(實指向最末元素的下一個位置) erase() 刪除指定元素 front() 返回第一個元素 get_allocator() 返回vector的內存分配器 insert() 插入元素的vctor中 pop_back() 移除最后一元素 push_back() 在vector最后添加一個元素 rbegin() 返回vector尾部的逆迭代器 rend() 返回vector起始的逆迭代器 reserve() 設置vector最小的元素容納數量 resize() 改變Vector元素的數量大小 size() 返回vector元素數量的大小 swap() 交換兩個Vector */

  

轉載于:https://www.cnblogs.com/xiangxiaodong/archive/2012/10/04/2711695.html

總結

以上是生活随笔為你收集整理的第二十三模板 18标准模板库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。