學習c++語言STL必不可少,今天首先爲大家簡單介紹下STL中vector和list的使用,其次,再簡單模擬下庫中的vector和list以方便大家更好的理解STL。
vector的使用:
代碼:
#include<iostream>
#include<vector>
using namespace std;
void Print_Vector(vector<int>& v){
vector<int>::iterator it = v.begin();//返回指向首元素的迭代器
while (it != v.end()){//判斷it是否指向了尾元素後的位置
cout << *it << " ";
it++;
}
cout << endl;
}
int main(){
vector<int> v;
v.push_back(1);//尾插
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
Print_Vector(v);
cout << "size:" <<v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.resize(10, 0);//調整size大小
Print_Vector(v);
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.reserve(20);//調整容量大小
Print_Vector(v);
cout << "size:" << v.size() << endl;
cout << "capacity:" << v.capacity() << endl;
v.pop_back();//尾刪
Print_Vector(v);
v.insert(v.begin(), 0);//在頭部插入
Print_Vector(v);
v.erase(--v.end());//在尾部刪除
Print_Vector(v);
return 0;
}
結果:
list的使用:
代碼:
#include<iostream>
#include<list>
using namespace std;
void Print_List(list<int>& v){
list<int>::iterator it = v.begin();//返回指向首元素的迭代器
while (it != v.end()){//判斷it是否指向了尾元素後的位置
cout << *it << " ";
it++;
}
cout << endl;
}
int main(){
list<int> v;
v.push_back(1);//尾插
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
Print_List(v);
v.pop_back();//尾刪
Print_List(v);
v.insert(v.begin(), 0);//在頭部插入
Print_List(v);
v.erase(--v.end());//在尾部刪除
Print_List(v);
return 0;
}
結果:
模擬實現MyVector:
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class MyVector{
public:
typedef T* Iterator;
MyVector()
: _start(new T[2])
, _finish(_start)
, _endOfStorage(_finish)
{}
Iterator End(){
return _finish;
}
Iterator Begin(){
return _start;
}
size_t Size(){
return _finish - _start;
}
size_t Capacity(){
return _endOfStorage - _start;
}
void Expand(size_t n){
size_t size = Size();
size_t capacity = Capacity();
if (n > size){
T* tmp = new T[n];
for (size_t i = 0; i < size; i++){
tmp[i] = _start[i];
}
delete[] _start;
_start = tmp;
_finish = _start + size;
_endOfStorage = _start + n;
}
}
void Insert(Iterator& pos, const T& x){
size_t n = pos - _start;
if (_finish == _endOfStorage){
size_t len = Capacity() == 0 ? 3 : Capacity() * 2;
Expand(len);
}
pos = _start + n;
for (Iterator i = End(); i != pos; i--){
*i = *(i - 1);
}
*pos = x;
_finish++;
}
T& operator[](size_t& pos){
return _start[pos];
}
const T& operator[](size_t& pos) const{
return _star[pos];
}
void Reserve(size_t n){
Expand(n);
}
void PushBack(T x){
Iterator end = End();
Insert(end, x);
}
void Resize(size_t n,const T& x= T()){
if (n <= Size()){
_finish = _start + n;
}
else{
Reserve(n);
size_t len = n - Size();
for (size_t i = 0; i < len; i++){
PushBack(x);
}
_finish = _start + n;
}
}
void PopBack(){
assert(_start != _finish);
_finish--;
}
protected:
Iterator _start;
Iterator _finish;
Iterator _endOfStorage;
};
void Print_Vecter(MyVector<int>& my){
for (size_t i = 0; i < my.Size(); i++){
cout << my[i] << " ";
}
cout << endl;
}
模擬實現MyList:
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
struct _ListNode{
T _data;
_ListNode<T>* _next;
_ListNode<T>* _prev;
_ListNode(T x)
:_data(x)
, _next(NULL)
, _prev(NULL)
{}
};
template<class T,class Ref,class Ptr>
struct _ListIterator{
typedef _ListNode<T> Node;
typedef _ListIterator<T, Ref, Ptr> Self;
Node* _node;
_ListIterator(Node* node)
:_node(node)
{}
Self operator++(int){
Node* tmp = _node;
_node = _node->_next;
return tmp;
}
Self& operator++(){
_node = _node->_next;
return *this;
}
Self operator--(int){
Node* tmp = this;
_node = _node->_prev;
return _node;
}
Self& operator--(){
_node = _node->_prev;
return *this;
}
bool operator==(const Self& x){
return _node == x._node;
}
bool operator!=(const Self& x){
return _node != x._node;
}
Ptr operator->(){
return &(operator*());
}
Ref operator*(){
return _node->_data;
}
};
template<class T>
class List{
typedef _ListNode<T> Node;
public:
typedef _ListIterator<T, T&, T*> Iterator;
typedef _ListIterator<T, const T&, const T*> ConstIterator;
Iterator Begin(){
return _head->_next;
}
Iterator End(){
return _head;
}
ConstIterator Begin() const{
return _head->_next;
}
ConstIterator End() const{
return _head;
}
List()
:_head(new Node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
List(const List<T>& l){
_head = new Node(T());
_head->_next = _head;
_head->_prev = _head;
ConstIterator it = l.Begin();
while (it != l.End()){
Push_Back(*it);
it++;
}
}
~List(){
Clear();
delete _head;
_head = NULL;
}
void Insert(Iterator& pos,const T& x){
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* tmp = new Node(x);
tmp->_next = cur;
tmp->_prev = prev;
prev->_next = tmp;
cur->_prev = tmp;
}
void Clear(){
Iterator it = Begin();
while (it != End()){
Node* tmp = it._node;
it++;
delete tmp;
}
_head->_next = _head;
_head->_prev = _head;
}
void Push_Back(const T& x){
Insert(End(),x);
}
void Push_Front(const T& x){
Insert(Begin(),x);
}
Iterator Erase(Iterator& pos){
assert(pos != End());
Node* prev = (pos._node)->_prev;
Node* next = (pos._node)->_next;
delete pos._node;
prev->_next = next;
next->_prev = prev;
pos._node = next;
return pos;
}
void Pop_Back(){
Erase(--End());
}
void Pop_Front(){
Erase(Begin());
}
protected:
Node* _head;
};
void Print_List(List<int>& l){
List<int>::Iterator it = l.Begin();
while (it != l.End()){
cout << *it << " ";
it++;
}
cout << endl;
}
void Test_List(){
List<int> l;
l.Push_Back(1);
l.Push_Back(2);
l.Push_Back(3);
l.Push_Back(4);
l.Push_Back(5);
Print_List(l);
l.Pop_Back();
Print_List(l);
l.Pop_Front();
Print_List(l);
l.Push_Front(1);
Print_List(l);
l.Insert(l.Begin(),0);
Print_List(l);
l.Erase(l.Begin());
Print_List(l);
l.Clear();
Print_List(l);
}