最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中vector迭代器失效問題詳解

 更新時(shí)間:2021年11月17日 10:01:24   作者:~怎么回事啊~  
vector是向量類型,它可以容納許多類型的數(shù)據(jù),如若干個(gè)整數(shù),所以稱其為容器,這篇文章主要給大家介紹了關(guān)于C++中vector迭代器失效問題的相關(guān)資料,需要的朋友可以參考下

問題:

 (1)刪除vector中所有的偶數(shù)

 
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
  vector<int> vec;
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)刪除
  auto it = vec.begin();
  for (; it != vec.end(); ++it) {
    if ((*it) % 2 == 0) {
      vec.erase(it);
    }
  }
  return 0;
}

運(yùn)行導(dǎo)致程序崩潰!

(2)vector容器插入元素問題

 
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
  vector<int> vec;
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)前面添加一個(gè)小于偶數(shù)值1的數(shù)字
  auto it = vec.begin();
  for (; it != vec.end(); ++it) {
    if ((*it) % 2 == 0) {
      vec.insert(it,*it-1);
    }
  }
  return 0;
}

運(yùn)行導(dǎo)致程序崩潰!

原因:iterator失效

 當(dāng)刪除(獲取增加)it位置的元素時(shí),導(dǎo)致it后面的迭代器全部失效。因此多次調(diào)用erase insert導(dǎo)致崩潰

迭代器失效原因

1 當(dāng)容器調(diào)用erase時(shí),當(dāng)前位置到容器末尾元素的所有的迭代器全部失效

2 當(dāng)容器調(diào)用insert時(shí),當(dāng)前位置到容器末尾元素的所有的迭代器全部失效;

3 當(dāng)容器調(diào)用insert時(shí),如果引起容器內(nèi)存擴(kuò)容,原來容器的所有的迭代器就全部失效

解決:

進(jìn)行更新操作:erase(insert)后會(huì)返回指向下一個(gè)元素的迭代器

 解釋:vector::erase - C++ Reference

從向量中刪除單個(gè)元素(位置)或一系列元素([第一、最后一個(gè)])。
這有效地減少了容器的大小,減少了被刪除的元素的數(shù)量,這些元素會(huì)被銷毀。
由于向量使用數(shù)組作為其底層存儲(chǔ),擦除向量端以外位置的元素會(huì)導(dǎo)致容器在段擦除后將所有元素重新定位到其新位置。與其他類型的序列容器對(duì)相同操作執(zhí)行的操作相比,這通常是一種低效的操作(如列表或轉(zhuǎn)發(fā)列表)。

同理有:vector::insert - C++ Reference

通過在指定位置的元素之前插入新元素來擴(kuò)展向量,從而通過插入的元素?cái)?shù)量有效地增加容器大小。

當(dāng)且僅當(dāng)新向量大小超過當(dāng)前向量容量時(shí),這會(huì)導(dǎo)致自動(dòng)重新分配分配分配的存儲(chǔ)空間

因?yàn)橄蛄渴褂脭?shù)組作為其底層存儲(chǔ),所以在向量末端以外的位置插入元素會(huì)導(dǎo)致容器將位置之后的所有元素重新定位到它們的新位置。與其他類型的序列容器(如list或forward_list)對(duì)相同操作執(zhí)行的操作相比,這通常是一種低效的操作。
這些參數(shù)確定插入的元素?cái)?shù)量及其初始化值:

 也說明了進(jìn)行插入操作會(huì)導(dǎo)致之后的迭代器失效

修改代碼:

 
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
  vector<int> vec;
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)刪除
  auto it = vec.begin();
  while (it!=vec.end())
  {
    if ((*it) % 2 == 0) {
      it = vec.erase(it);
    }
    else {
      it++;
    }
  }
 
  for(auto it:vec) {
    cout << it << " ";
  }
  return 0;
}
 
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
  vector<int> vec;
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)前面添加一個(gè)小于偶數(shù)值1的數(shù)字
  auto it = vec.begin();
  for (; it != vec.end(); ++it) {
    if ((*it) % 2 == 0) {
      it = vec.insert(it, *it - 1);
      //it原來的位置插入了新的,需要++it兩次,才能到該偶數(shù)的后一個(gè)元素
      ++it;
    }
  }
 
  for (auto val : vec) {
    cout << val << " ";
  }
  return 0;
}

這樣就沒有問題。

vector中實(shí)現(xiàn)                                                                                   ​​​​​​

http://www.fzitv.net/article/229393.htm    接該文最終實(shí)現(xiàn)的vector上繼續(xù):

頭插法:

                                                          

檢查迭代器失效:

 在進(jìn)行刪除或增加的時(shí)候,要檢測(cè)該位置到last位置,使其迭代器失效

  void pop_back() // 從容器末尾刪除元素
  {
    if (empty())
      return;
 
    //檢查迭代器 從該位置到最后
    verify(_last-1,_last);
 
    // 不僅要把_last指針--,還需要析構(gòu)刪除的元素
    --_last;
    _allocator.destroy(_last);
  }
  //檢查迭代器失效
  void verify(T* first, T* last) {
    Iterator_Base * pre = &this->_head;
    Iterator_Base * it = this->_head._next;
 
    while (it != nullptr) {
      if (it->_cur->_ptr > first && it->_cur->_ptr <= last) {
        it->_cur->_pVec = nullptr;//迭代器失效,把iterator持有的容器指針置空
        pre->_next = it->_next;//刪除當(dāng)前迭代器節(jié)點(diǎn),繼續(xù)判斷后面的迭代器是否失效
        delete it;
        it = pre->_next;
      }
      else {
        pre = it;
        it = it->_next;
      }
    
    }
  }

insert

  //insert
  iterator insert(iterator it, const T&val) {
     //未考慮擴(kuò)容和it._ptr的合法性 todo
    verify(it._ptr - 1, _last);
    //依次向后移動(dòng)一個(gè)位置
    T*p = _last;
    while (p > it->_ptr) {
      _allocator.construct(p,*(p-1));
      _allocator.destroy(p-1);
      p--;
    }
 
    //在該位置插入
    _allocator.construct(p, val);
    _last++;
    return iterator(this, p);//生成新的迭代器
  }

erase

 
#include <iostream>
 
//容器的空間配置器
template <typename T>
struct Allocator
{
  T* allocate(size_t size)//只負(fù)責(zé)內(nèi)存開辟
  {
    return (T*)malloc(sizeof(T) * size);
  }
  void deallocate(void *p)//只負(fù)責(zé)內(nèi)存釋放
  {
    free(p);
  }
  void construct(T *p, const T &val)//已經(jīng)開辟好的內(nèi)存上,負(fù)責(zé)對(duì)象構(gòu)造
  {
    new (p) T(val);//定位new,指定內(nèi)存上構(gòu)造val,T(val)拷貝構(gòu)造
  }
  void destroy(T *p)//只負(fù)責(zé)對(duì)象析構(gòu)
  {
    p->~T();//~T()代表了T類型的析構(gòu)函數(shù)
  }
};
 
template <typename T, typename Alloc = Allocator<T>>
class vector//向量容器
{
public:
  vector(int size = 10)//構(gòu)造
  {
    //_first = new T[size];
    _first = _allocator.allocate(size);
    _last = _first;
    _end = _first + size;
  }
  ~vector()//析構(gòu)
  {
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destroy(p);//把_first指針指向的數(shù)組的有效元素析構(gòu)
    }
    _allocator.deallocate(_first);//釋放堆上的數(shù)組內(nèi)存
    _first = _last = _end = nullptr;
  }
  vector(const vector<T> &rhs)//拷貝構(gòu)造
  {
    int size = rhs._end - rhs._first;//空間大小
    //_first = new T[size];
    _first = _allocator.allocate(size);
    int len = rhs._last - rhs._first;//有效元素
    for (int i = 0; i < len; ++i)
    {
      //_first[i] = rhs._first[i];
      _allocator.construct(_first + i, rhs._first[i]);
    }
    _last = _first + len;
    _end = _first + size;
  }
  vector<T>& operator=(const vector<T> &rhs)//賦值運(yùn)算符重載
  {
    if (this == &rhs)
    {
      return *this;
    }
 
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destory(p);//把_first指針指向的數(shù)組的有效元素析構(gòu)
    }
    _allocator.deallocate(_first);//釋放堆上的數(shù)組內(nèi)存
 
    int size = rhs._end - rhs._first;//空間大小
    _first = _allocator.allocate(size);
    int len = rhs._last - rhs._first;//有效元素
    for (int i = 0; i < len; ++i)
    {
      _allocator.construct(_first + i, rhs._first[i]);
    }
    _last = _first + len;
    _end = _first + size;
    return *this;
  }
  void push_back(const T &val)//尾插
  {
    if (full())
    {
      expand();
    }
    //*_last++ = val;
    _allocator.construct(_last, val);//_last指針指向的內(nèi)存構(gòu)造一個(gè)值為val的對(duì)象
    _last++;
  }
  void pop_back()//尾刪
  {
    if (empty()) return;
    verify(_last - 1, _last);
    //erase(it); verift(it._ptr, _last);
    //insert(it,val); verift(it._ptr, _last);
    //--_last;
    //不僅要把_last指針--,還需要析構(gòu)刪除的元素
    --_last;
    _allocator.destroy(_last);
  }
  T back()const//返回容器末尾元素值
  {
    return *(_last - 1);
  }
  bool full()const
  {
    return _last == _end;
  }
  bool empty()const
  {
    return _first == _last;
  }
  int size()const//返回容器中元素個(gè)數(shù)
  {
    return _last - _first;
  }
  T& operator[](int index)
  {
    if (index < 0 || index >= size())
    {
      throw "OutOfRangeException";
    }
    return _first[index];
  }
  //迭代器一般實(shí)現(xiàn)成容器的嵌套類型
  class iterator
  {
  public:
    friend class vector <T, Alloc>;
    //新生成當(dāng)前容器某一個(gè)位置元素的迭代器
    iterator(vector<T, Alloc> *pvec = nullptr
      , T *ptr = nullptr)
      :_ptr(ptr), _pVec(pvec)
    {
      Iterator_Base *itb = new Iterator_Base(this, _pVec->_head._next);
      _pVec->_head._next = itb;
    }
    bool operator!=(const iterator &it)const
    {
      //檢查迭代器的有效性
      if (_pVec == nullptr || _pVec != it._pVec)//迭代器為空或迭代兩個(gè)不同容器
      {
        throw "iterator incompatable!";
      }
      return _ptr != it._ptr;
    }
    void operator++()
    {
      //檢查迭代器有效性
      if (_pVec == nullptr)
      {
        throw "iterator incalid!";
      }
      _ptr++;
    }
    T& operator*()
    {
      //檢查迭代器有效性
      if (_pVec == nullptr)
      {
        throw "iterator invalid!";
      }
      return *_ptr;
    }
    const T& operator*()const
    {
      if (_pVec == nullptr)
      {
        throw "iterator invalid!";
      }
      return *_ptr;
    }
  private:
    T *_ptr;
    //當(dāng)前迭代器是哪個(gè)容器對(duì)象
    vector<T, Alloc> *_pVec;//指向當(dāng)前對(duì)象容器的指針
  };
  iterator begin()
  {
    return iterator(this, _first);
  }
  iterator end()
  {
    return iterator(this, _last);
  }
  //檢查迭代器失效
  void verify(T *first, T *last)
  {
    Iterator_Base *pre = &this->_head;
    Iterator_Base *it = this->_head._next;
    while (it != nullptr)
    {
      if (it->_cur->_ptr > first && it->_cur->_ptr <= last)
      {
        //迭代器失效,把iterator持有的容器指針置nullptr
        it->_cur->_pVec = nullptr;
        //刪除當(dāng)前迭代器節(jié)點(diǎn),繼續(xù)判斷后面的迭代器節(jié)點(diǎn)是否失效
        pre->_next = it->_next;
        delete it;
        it = pre->_next;
      }
      else
      {
        pre = it;
        it = it->_next;
      }
    }
  }
 
  //自定義vector容器insert方法實(shí)現(xiàn)
  iterator insert(iterator it, const T &val)
  {
    //1.這里我們未考慮擴(kuò)容
    //2.還未考慮it._ptr指針合法性,假設(shè)它合法
    verify(it._ptr - 1, _last);
    T *p = _last;
    while (p > it._ptr)
    {
      _allocator.construct(p, *(p - 1));
      _allocator.destroy(p - 1);
      p--;
    }
    _allocator.construct(p, val);
    _last++;
    return iterator(this, p);
  }
 
  //自定義vector容器erase方法實(shí)現(xiàn)
  iterator erase(iterator it)
  {
    verify(it._ptr - 1, _last);
    T *p = it._ptr;
    while (p < _last - 1)
    {
      _allocator.destroy(p);
      _allocator.construct(p, *(p + 1));
      p++;
    }
    _allocator.destroy(p);
    _last--;
    return iterator(this, it._ptr);
  }
private:
  T *_first;//起始數(shù)組位置
  T *_last;//指向最后一個(gè)有效元素后繼位置
  T *_end;//指向數(shù)組空間的后繼位置
  Alloc _allocator;//定義容器的空間配置器對(duì)象
 
  //容器迭代器失效增加代碼
  struct Iterator_Base
  {
    Iterator_Base(iterator *c = nullptr, Iterator_Base *n = nullptr)
      :_cur(c), _next(n) {}
    iterator *_cur;
    Iterator_Base *_next;
  };
  Iterator_Base _head;
 
  void expand()//擴(kuò)容
  {
    int size = _end - _first;
    //T *ptmp = new T[2*size];
    T *ptmp = _allocator.allocate(2 * size);
    for (int i = 0; i < size; ++i)
    {
      _allocator.construct(ptmp + i, _first[i]);
      //ptmp[i] = _first[i];
    }
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destroy(p);
    }
    _allocator.deallocate(_first);
    _first = ptmp;
    _last = _first + size;
    _end = _first + 2 * size;
  }
};
 
int main()
{
  vector<int>   vec(200);
 
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)前面添加一個(gè)小于偶數(shù)值1的數(shù)字
  auto it = vec.begin();
  for (; it != vec.end(); ++it) {
    if ((*it) % 2 == 0) {
      it = vec.insert(it, *it - 1);
      //it原來的位置插入了新的,需要++it兩次,才能到該偶數(shù)的后一個(gè)元素
      ++it;
    }
  }
 
  for (auto val : vec) {
    std::cout << val << " ";
  }
 
  return 0;
}

測(cè)試vector

 
#include <iostream>
 
//容器的空間配置器
template <typename T>
struct Allocator
{
  T* allocate(size_t size)//只負(fù)責(zé)內(nèi)存開辟
  {
    return (T*)malloc(sizeof(T) * size);
  }
  void deallocate(void *p)//只負(fù)責(zé)內(nèi)存釋放
  {
    free(p);
  }
  void construct(T *p, const T &val)//已經(jīng)開辟好的內(nèi)存上,負(fù)責(zé)對(duì)象構(gòu)造
  {
    new (p) T(val);//定位new,指定內(nèi)存上構(gòu)造val,T(val)拷貝構(gòu)造
  }
  void destroy(T *p)//只負(fù)責(zé)對(duì)象析構(gòu)
  {
    p->~T();//~T()代表了T類型的析構(gòu)函數(shù)
  }
};
 
template <typename T, typename Alloc = Allocator<T>>
class vector//向量容器
{
public:
  vector(int size = 10)//構(gòu)造
  {
    //_first = new T[size];
    _first = _allocator.allocate(size);
    _last = _first;
    _end = _first + size;
  }
  ~vector()//析構(gòu)
  {
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destroy(p);//把_first指針指向的數(shù)組的有效元素析構(gòu)
    }
    _allocator.deallocate(_first);//釋放堆上的數(shù)組內(nèi)存
    _first = _last = _end = nullptr;
  }
  vector(const vector<T> &rhs)//拷貝構(gòu)造
  {
    int size = rhs._end - rhs._first;//空間大小
    //_first = new T[size];
    _first = _allocator.allocate(size);
    int len = rhs._last - rhs._first;//有效元素
    for (int i = 0; i < len; ++i)
    {
      //_first[i] = rhs._first[i];
      _allocator.construct(_first + i, rhs._first[i]);
    }
    _last = _first + len;
    _end = _first + size;
  }
  vector<T>& operator=(const vector<T> &rhs)//賦值運(yùn)算符重載
  {
    if (this == &rhs)
    {
      return *this;
    }
 
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destory(p);//把_first指針指向的數(shù)組的有效元素析構(gòu)
    }
    _allocator.deallocate(_first);//釋放堆上的數(shù)組內(nèi)存
 
    int size = rhs._end - rhs._first;//空間大小
    _first = _allocator.allocate(size);
    int len = rhs._last - rhs._first;//有效元素
    for (int i = 0; i < len; ++i)
    {
      _allocator.construct(_first + i, rhs._first[i]);
    }
    _last = _first + len;
    _end = _first + size;
    return *this;
  }
  void push_back(const T &val)//尾插
  {
    if (full())
    {
      expand();
    }
    //*_last++ = val;
    _allocator.construct(_last, val);//_last指針指向的內(nèi)存構(gòu)造一個(gè)值為val的對(duì)象
    _last++;
  }
  void pop_back()//尾刪
  {
    if (empty()) return;
    verify(_last - 1, _last);
    //erase(it); verift(it._ptr, _last);
    //insert(it,val); verift(it._ptr, _last);
    //--_last;
    //不僅要把_last指針--,還需要析構(gòu)刪除的元素
    --_last;
    _allocator.destroy(_last);
  }
  T back()const//返回容器末尾元素值
  {
    return *(_last - 1);
  }
  bool full()const
  {
    return _last == _end;
  }
  bool empty()const
  {
    return _first == _last;
  }
  int size()const//返回容器中元素個(gè)數(shù)
  {
    return _last - _first;
  }
  T& operator[](int index)
  {
    if (index < 0 || index >= size())
    {
      throw "OutOfRangeException";
    }
    return _first[index];
  }
  //迭代器一般實(shí)現(xiàn)成容器的嵌套類型
  class iterator
  {
  public:
    friend class vector <T, Alloc>;
    //新生成當(dāng)前容器某一個(gè)位置元素的迭代器
    iterator(vector<T, Alloc> *pvec = nullptr
      , T *ptr = nullptr)
      :_ptr(ptr), _pVec(pvec)
    {
      Iterator_Base *itb = new Iterator_Base(this, _pVec->_head._next);
      _pVec->_head._next = itb;
    }
    bool operator!=(const iterator &it)const
    {
      //檢查迭代器的有效性
      if (_pVec == nullptr || _pVec != it._pVec)//迭代器為空或迭代兩個(gè)不同容器
      {
        throw "iterator incompatable!";
      }
      return _ptr != it._ptr;
    }
    void operator++()
    {
      //檢查迭代器有效性
      if (_pVec == nullptr)
      {
        throw "iterator incalid!";
      }
      _ptr++;
    }
    T& operator*()
    {
      //檢查迭代器有效性
      if (_pVec == nullptr)
      {
        throw "iterator invalid!";
      }
      return *_ptr;
    }
    const T& operator*()const
    {
      if (_pVec == nullptr)
      {
        throw "iterator invalid!";
      }
      return *_ptr;
    }
  private:
    T *_ptr;
    //當(dāng)前迭代器是哪個(gè)容器對(duì)象
    vector<T, Alloc> *_pVec;//指向當(dāng)前對(duì)象容器的指針
  };
  iterator begin()
  {
    return iterator(this, _first);
  }
  iterator end()
  {
    return iterator(this, _last);
  }
  //檢查迭代器失效
  void verify(T *first, T *last)
  {
    Iterator_Base *pre = &this->_head;
    Iterator_Base *it = this->_head._next;
    while (it != nullptr)
    {
      if (it->_cur->_ptr > first && it->_cur->_ptr <= last)
      {
        //迭代器失效,把iterator持有的容器指針置nullptr
        it->_cur->_pVec = nullptr;
        //刪除當(dāng)前迭代器節(jié)點(diǎn),繼續(xù)判斷后面的迭代器節(jié)點(diǎn)是否失效
        pre->_next = it->_next;
        delete it;
        it = pre->_next;
      }
      else
      {
        pre = it;
        it = it->_next;
      }
    }
  }
 
  //自定義vector容器insert方法實(shí)現(xiàn)
  iterator insert(iterator it, const T &val)
  {
    //1.這里我們未考慮擴(kuò)容
    //2.還未考慮it._ptr指針合法性,假設(shè)它合法
    verify(it._ptr - 1, _last);
    T *p = _last;
    while (p > it._ptr)
    {
      _allocator.construct(p, *(p - 1));
      _allocator.destroy(p - 1);
      p--;
    }
    _allocator.construct(p, val);
    _last++;
    return iterator(this, p);
  }
 
  //自定義vector容器erase方法實(shí)現(xiàn)
  iterator erase(iterator it)
  {
    verify(it._ptr - 1, _last);
    T *p = it._ptr;
    while (p < _last - 1)
    {
      _allocator.destroy(p);
      _allocator.construct(p, *(p + 1));
      p++;
    }
    _allocator.destroy(p);
    _last--;
    return iterator(this, it._ptr);
  }
private:
  T *_first;//起始數(shù)組位置
  T *_last;//指向最后一個(gè)有效元素后繼位置
  T *_end;//指向數(shù)組空間的后繼位置
  Alloc _allocator;//定義容器的空間配置器對(duì)象
 
  //容器迭代器失效增加代碼
  struct Iterator_Base
  {
    Iterator_Base(iterator *c = nullptr, Iterator_Base *n = nullptr)
      :_cur(c), _next(n) {}
    iterator *_cur;
    Iterator_Base *_next;
  };
  Iterator_Base _head;
 
  void expand()//擴(kuò)容
  {
    int size = _end - _first;
    //T *ptmp = new T[2*size];
    T *ptmp = _allocator.allocate(2 * size);
    for (int i = 0; i < size; ++i)
    {
      _allocator.construct(ptmp + i, _first[i]);
      //ptmp[i] = _first[i];
    }
    //delete[]_first;
    for (T *p = _first; p != _last; ++p)
    {
      _allocator.destroy(p);
    }
    _allocator.deallocate(_first);
    _first = ptmp;
    _last = _first + size;
    _end = _first + 2 * size;
  }
};
 
int main()
{
  vector<int>   vec(200);
 
  for (int i = 0; i < 10; ++i) {
    vec.push_back(i);
  }
 
  //把vec容器中的所有偶數(shù)前面添加一個(gè)小于偶數(shù)值1的數(shù)字
  auto it = vec.begin();
  for (; it != vec.end(); ++it) {
    if ((*it) % 2 == 0) {
      it = vec.insert(it, *it - 1);
      //it原來的位置插入了新的,需要++it兩次,才能到該偶數(shù)的后一個(gè)元素
      ++it;
    }
  }
 
  for (auto val : vec) {
    std::cout << val << " ";
  }
 
  return 0;
}

總結(jié)

到此這篇關(guān)于C++中vector迭代器失效問題的文章就介紹到這了,更多相關(guān)C++中vector迭代器失效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++使用一棵紅黑樹同時(shí)封裝出map和set實(shí)例代碼

    C++使用一棵紅黑樹同時(shí)封裝出map和set實(shí)例代碼

    紅黑樹(Red?Black?Tre)是一種自平衡二叉查找樹,是在計(jì)算機(jī)科學(xué)中用到的一種數(shù)據(jù)結(jié)構(gòu),典型的用途是實(shí)現(xiàn)關(guān)聯(lián)數(shù)組,下面這篇文章主要給大家介紹了關(guān)于C++使用一棵紅黑樹同時(shí)封裝出map和set的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • 如何正確的使用語句塊

    如何正確的使用語句塊

    本篇文章是對(duì)正確使用語句塊進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 自己簡(jiǎn)單封裝的一個(gè)CDialog類實(shí)例

    自己簡(jiǎn)單封裝的一個(gè)CDialog類實(shí)例

    這篇文章主要介紹了自己簡(jiǎn)單封裝的一個(gè)CDialog類,實(shí)例分析了自定義封裝CDialog類的相關(guān)技巧,比較簡(jiǎn)單易懂,需要的朋友可以參考下
    2015-04-04
  • Qt實(shí)現(xiàn)小功能之復(fù)雜抽屜效果詳解

    Qt實(shí)現(xiàn)小功能之復(fù)雜抽屜效果詳解

    在Qt自帶的控件中,也存在抽屜控件:QToolBar。但是,該控件有個(gè)缺點(diǎn):一次只能展開一個(gè)抽屜信息,無法實(shí)現(xiàn)多個(gè)展開。所以本文將自定義實(shí)現(xiàn)復(fù)雜抽屜效果,需要的可以參考一下
    2022-10-10
  • C++實(shí)現(xiàn)單鏈表的構(gòu)造

    C++實(shí)現(xiàn)單鏈表的構(gòu)造

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)單鏈表的構(gòu)造,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解

    C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解

    這篇文章主要介紹了C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • c++ 巧開平方的實(shí)現(xiàn)代碼

    c++ 巧開平方的實(shí)現(xiàn)代碼

    如果沒有計(jì)算器,我們?nèi)绾吻?的平方根
    2013-05-05
  • C語言MultiByteToWideChar和WideCharToMultiByte案例詳解

    C語言MultiByteToWideChar和WideCharToMultiByte案例詳解

    這篇文章主要介紹了C語言MultiByteToWideChar和WideCharToMultiByte案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 淺析int*p[ ]與int(*p)[ ]的區(qū)別

    淺析int*p[ ]與int(*p)[ ]的區(qū)別

    以下是對(duì)int*p[ ]與int(*p)[ ]的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • 圖文詳解c/c++中的多級(jí)指針與多維數(shù)組

    圖文詳解c/c++中的多級(jí)指針與多維數(shù)組

    多維數(shù)組與多級(jí)指針是初學(xué)者經(jīng)常感覺迷糊的一個(gè)地方。超過二維的數(shù)組和超過二級(jí)的指針其實(shí)并不多用。但只要掌握一定的方法,理解多級(jí)指針和“多維”數(shù)組完全可以像理解一級(jí)指針和一維數(shù)組那樣簡(jiǎn)單。
    2016-08-08

最新評(píng)論

油尖旺区| 大安市| 阿克陶县| 留坝县| 连山| 乌兰县| 彭阳县| 泸定县| 石首市| 白城市| 和平县| 湖北省| 宣汉县| 扎兰屯市| 兰州市| 贵阳市| 图木舒克市| 辽宁省| 炉霍县| 聂拉木县| 马鞍山市| 肃宁县| 永春县| 鄂托克旗| 武义县| 宁国市| 团风县| 南昌县| 兴业县| 微山县| 永安市| 手机| 东丽区| 马关县| 云龙县| 桐柏县| 砀山县| 杭州市| 中西区| 滁州市| 湟源县|