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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

详解vector容器(应用+模拟实现,vector相关练习题)

發(fā)布時(shí)間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 详解vector容器(应用+模拟实现,vector相关练习题) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

vector容器

動(dòng)態(tài)的順序表,數(shù)組。

vector操作

vector操作及其概念

構(gòu)造

vector<int>v1;vector<int>v2(10, 5);vector<int>v3(v2);int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };vector<int>v4(array, array + sizeof(array) / sizeof(array[0]));vector<int>v5{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//c++11中給出的構(gòu)造

容量

cout << v5.size() << endl; cout << v5.capacity() << endl; cout << v5.front() << endl; cout << v5.back() << endl;

vector增容機(jī)制

size_t sz; std::vector<int> foo; sz = foo.capacity(); std::cout << "making foo grow:\n"; for (int i = 0; i<100; ++i) { foo.push_back(i); if (sz != foo.capacity()) { sz = foo.capacity(); std::cout << "容量改變: " << sz << '\n'; } }

vs2013底層的vector增容機(jī)制差不多是1.5倍的方式進(jìn)行擴(kuò)容。
而linux下g++的vector是按照兩倍的方式進(jìn)行擴(kuò)容

在采用push_back向vector中尾插元素期間,如果知道大概知道要放置多少個(gè)元素的情況下,可以通過reverse提前將空間開辟號(hào),這樣效率就高了。
注意reserve的下面這種情況

size_t sz; std::vector<int> foo; foo.reserve(100); //底層空間有了但是沒有有效元素 foo[0] = 10; //這個(gè)式子左邊就錯(cuò)了,不能訪問

總結(jié)

  • 通過push_back來進(jìn)行插入------通過reserve來進(jìn)行開辟空間
  • 通過[]運(yùn)算符進(jìn)行插入------通過resize來進(jìn)行開辟空間
  • 元素訪問

    for (size_t i = 0; i < v2.size(); ++i) {cout << v2[i]<<" " ; } cout << endl;for (auto e : v3) {cout << e<<" "; } cout << endl;

    元素修改

    void TestVector2() {vector<int>v5{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };cout << v5.size() << endl;cout << v5.capacity() << endl;cout << v5.front() << endl;cout << v5.back() << endl;v5.push_back(1);v5.push_back(2);v5.push_back(3);cout << v5.size() << endl;cout << v5.capacity() << endl;cout << v5.front() << endl;cout << v5.back() << endl;v5.erase(v5.begin());v5.erase(v5.end() - 1);//vector里沒有find方法,要想刪除最后一個(gè)元素,必須用系統(tǒng)給的算法函數(shù)findauto pos = find(v5.begin(), v5.end(), 5);if (pos != v5.end())v5.erase(pos);v5.clear(); }

    迭代器

    迭代器:類似指針的一種類型,類似是一種指針類型,可以將迭代器定義的對(duì)象當(dāng)成是指針方式進(jìn)行應(yīng)用
    作用:幫助用戶**透明的(用戶可以不用知道該容器的底層數(shù)據(jù)結(jié)構(gòu))**遍歷數(shù)據(jù)

    auto it1 = v4.begin(); while (it1 != v4.end()) {cout << *it1<<" ";it1++; } cout << endl;auto it2 = v5.rbegin(); while (it2 != v5.rend()) {cout << *it2 << " ";it2++; } cout << endl;


    有些情況下我們需要二維數(shù)組

    void TestVector3() {vector<vector<int>>vv;//10 * 10----->6vv.resize(10); //已經(jīng)有了10行,但每一行還沒有空間//給每行設(shè)置元素for (size_t i = 0; i < 10; ++i){//每一行10個(gè)元素vv[i].resize(10,6);}for (int i = 0; i < 10; ++i){for (int j = 0; j < 10; ++j){cout << vv[i][j] << " ";}cout<<endl;}}

    迭代器失效

    迭代器失效:迭代器本質(zhì)是指針,指針失效,指針指向了非法空間

    vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };for (size_t i = 0; i < v.size(); ++i){cout << v[i] << " ";}cout << endl;for (auto& e : v){cout << e << " ";}cout << endl;//迭代器:類似指針的一種類型//類似是一種指針類型,可以將迭代器定義的對(duì)象當(dāng)成是指針方式進(jìn)行應(yīng)用//作用:幫助用戶透明的(用戶可以不用知道該容器的底層數(shù)據(jù)結(jié)構(gòu))遍歷數(shù)據(jù)auto it = v.begin();//往容器里多增加了一個(gè)元素就引起了代碼崩潰v.push_back(1);//vector<int>::iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;

    擴(kuò)容后,原來空間釋放,it迭代器指向非法空間

    vector:迭代器失效場(chǎng)景

  • push_back----可以自動(dòng)擴(kuò)容
  • resize
  • reserve
  • insert
  • assign
  • 還有一種場(chǎng)景

    vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//v.resize(0);//v.clear();//v.erase(v.begin(), v.end());auto it = v.begin();while (it!=v.end()){v.erase(it);++it;}cout << v.size() << endl;

    因?yàn)?br />

    返回值指向了新的空間

    如何解決

    給當(dāng)前迭代器重新賦值,讓其指向有效空間

    auto it = v.begin();//v.assign(20, 8);v.push_back(1); //可能會(huì)迭代器失效//解決方式:給it迭代器重新賦值it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;

    引用失效

    vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };int& ra = v[0];ra = 10;v.push_back(1);ra = 100;

    楊輝三角


    用二維數(shù)組,每一行用一個(gè)一維數(shù)組存。

    class Solution { public:vector<vector<int>> generate(int numRows) {vector<vector<int>> ret;ret.resize(numRows); //先創(chuàng)建numRows行for(size_t i=0;i<numRows;++i){ret[i].resize(i+1);ret[i][0] = 1;ret[i][i] = 1;}for(size_t i = 2; i < numRows; ++i){//j為每一列for(size_t j = 1; j < i; ++j){ret[i][j]=ret[i-1][j]+ret[i-1][j-1];}}return ret;} };

    vector模擬實(shí)現(xiàn)

    vector結(jié)構(gòu)

    class vector { public:typedef T* iterator; private:T* _start;T* _finish;T* _endofstorage;};

    構(gòu)造與銷毀

    // 構(gòu)造與銷毀vector():_start(nullptr), _finish(nullptr), _endofstorage(nullptr){}vector(int n, const T& data):_start(new T[n]){for (size_t i = 0; i < n; ++i){_start[i] = data;}_finish = _start + n;_endofstorage = _finish;}//[first,last)template<class Iterator>vector(Iterator first, Iterator last){//計(jì)算[first,last)區(qū)間中元素的個(gè)數(shù)size_t n = 0;auto it = first;while (it != last){++it;++n;}_start = new T[n];//將[first,last)區(qū)間中的元素放置到_start空間中for (size_t i = 0; i < n; ++i){_start[i] = *first++;}_finish = _start + n;_endofstorage = _start + n;}vector(const vector<T>& v);vector<T>& operator=(const T& v);~vector(){if (_start){delete[] _start;_start = _finish = _endofstorage = nullptr;}}

    容量操作

    // 容量操作 size_t size()const {return _finish - _start; }size_t capacity()const {return _endofstorage - _start; }bool empty()const {return _start == _finish; }// T(): // 如果T代表內(nèi)置類型,T()--->0 // 如果T代表自定義類型,T()---> 調(diào)用該類的無參構(gòu)造函數(shù) void resize(size_t newsize, const T& data = T()) {size_t oldsize = size();if (newsize > oldsize){size_t cap = capacity();if (newsize > cap)reserve(newsize);for (; oldsize < newsize; ++oldsize)_start[oldsize] = data;//*_finish++ = data;}_finish = _start + newsize; }void reserve(size_t newCapacity) {size_t oldCapcity = capacity();if (newCapacity > oldCapcity){// 1. 申請(qǐng)新空間T* temp = new T[newCapacity];// 2. 拷貝元素//memcpy(temp, _start, size()*sizeof(T));// 如果_start指向的空間存在size_t n = size();if (_start){for (size_t i = 0; i < n; ++i){temp[i] = _start[i];}// 3. 釋放舊空間delete[] _start;}_start = temp;_finish = _start + n;_endofstorage = _start + newCapacity;} }

    元素訪問操作

    //元素訪問的操作//v[0] = 100; v如果是普通類型T& operator[](size_t index){assert(index <= size());return _start[index];}//對(duì)于const類型的變量const T& operator[](size_t index)const{assert(index <= size());return _start[index];}T& front(){return *_start;}const T& front()const{return *_start;}T& back(){return *(_finish-1);}const T& back()const{return *(_finish-1);}

    元素修改

    //元素修改 void push_back(const T& data) {//檢測(cè)是否需要擴(kuò)容if (_finish == _endofstorage){reserve(capacity() * 2 + 3);}*_finish++ = data; }void pop_back() {--_finish; }//返回值含義:反回新插入元素的位置 iterator insert(iterator pos, const T&data) {//檢測(cè)是否需要擴(kuò)容if (_finish == _endofstorage){reserve(capacity() * 2 + 3);}//插入元素//將[pos,finish)之間所有元素整體向后搬移一個(gè)位置auto it = _finish;while (it > pos){*it = *(it - 1);it--;}//插入新元素*pos = data;_finish++;return pos; }iterator erase(iterator pos) {if (pos == end())return pos;//it代表待搬移元素的位置auto it = pos + 1;while (it != _finish){*(it - 1) = *it;++ot;}_finish--;return pos; }

    迭代器操作

    iterator begin() {return _start; }iterator end() {return _finish; }

    測(cè)試代碼

    void TestVector() {bite::vector<int>v1;bite::vector<int>v2(10, 5);int array[] = { 1, 2, 3, 4, 5 };bite::vector<int>v3(array,array+sizeof(array)/sizeof(array[0]));cout << v2.size() << endl;cout << v2.capacity() << endl;cout << v3.front() << endl;cout << v3.back() << endl;for (size_t i = 0; i < v3.size(); ++i)cout << v3[i] << " ";cout << endl;//bite::vector<int>::iterator it = v3.begin();auto it = v3.begin();while (it != v3.end()){cout << *it << " ";++it;}cout << endl;for (auto& e : v3)e *= 2;for (auto e : v3)cout << e << " ";cout << endl; }

    void TestVector2() {bite::vector<int>v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);cout << v.size() << endl;cout << v.capacity() << endl;cout << v.back() << endl;v.pop_back();cout << v.back() << endl;cout << v.size() << endl;cout << v.front() << endl;v.insert(v.begin(), 0);cout << v.front() << endl;cout << v.size() << endl;}

    void TestVector3() {bite::vector<int>v(10, 5);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(5);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(8);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(20, 5);cout << v.size() << endl;cout << v.capacity() << endl; }

    總結(jié)

    以上是生活随笔為你收集整理的详解vector容器(应用+模拟实现,vector相关练习题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。