第二十三模板 18标准模板库
生活随笔
收集整理的這篇文章主要介紹了
第二十三模板 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标准模板库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决ECSHOP后台订单里面点击编辑配送
- 下一篇: 为何三军仪仗队指挥刀被称为天下第一刀