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

C++索引越界的解決方法

 更新時(shí)間:2021年08月03日 16:36:09   作者:Welcom to LyAsano’s blog!  
本文主要介紹了C++索引越界的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

避免"索引越界"錯(cuò)誤的規(guī)則如下(針對(duì)C++):

  • 不要使用靜態(tài)或動(dòng)態(tài)分配的數(shù)組,改用array或vector模板
  • 不要使用帶方括號(hào)的new和delete操作符,讓vector模板為多個(gè)元素分配內(nèi)存
  • 使用scpp::vector代替std::vector,使用scpp::array代替靜態(tài)數(shù)組,并打開安全檢查(自動(dòng)在使用下標(biāo)訪問(wèn)提供了索引邊界檢查)

C++中創(chuàng)建類型T的對(duì)象的數(shù)組方式如下:

#define N 10
T static_arr[N]; //數(shù)組長(zhǎng)度在編譯時(shí)已知

int n=20;
T* dynamic_arr=new T[n]; //數(shù)組長(zhǎng)度在運(yùn)行時(shí)計(jì)算

std::vector<T> vector_arr; //數(shù)組長(zhǎng)度在運(yùn)行時(shí)進(jìn)行修改

1. 動(dòng)態(tài)數(shù)組

  采用的辦法是繼承std::vector<T>,并重載<< 、[]運(yùn)算符,提供一個(gè)能夠捕捉越界訪問(wèn)錯(cuò)誤的實(shí)現(xiàn)。

  實(shí)現(xiàn)代碼和測(cè)試如下:

//scpp_vector.h
#ifndef  _SCPP_VECTOR_
#define  _SCPP_VECTOR_

#include <vector>
#include "scpp_assert.h"

namespace scpp {

    //wrapper around std::vector,在[]提供了臨時(shí)的安全檢查:重載[] <<運(yùn)算符
    template<typename T>
    class vector : public std::vector<T> {
        public:
             typedef unsigned size_type;

             //常用的構(gòu)造函數(shù) commonly use cons
             explicit vector(size_type n=0) : std::vector<T>(n) {

             }
             vector(size_type n,const T& value) : std::vector<T>(n,value) {

             }

             template <class InputIterator> vector(InputIterator first,InputIterator last) 
                 : std::vector<T>(first,last) {

             }
             
             //Note : we don't provide a copy-cons and assignment operator  ?

            //使用scpp::vector提供更安全的下標(biāo)訪問(wèn)實(shí)現(xiàn),它可以捕捉越界訪問(wèn)錯(cuò)誤
             T& operator[] (size_type index) {
                 SCPP_ASSERT( index < std::vector<T>::size() ,
                     "Index " << index << " must be less than " << std::vector<T>::size());
                 return std::vector<T>::operator[](index);
             }

             //? difference 
             const T& operator[] (size_type index) const {
                 SCPP_ASSERT( index < std::vector<T>::size() ,
                     "Index " << index << " must be less than " << std::vector<T>::size());
                 return std::vector<T>::operator[](index);
             }

             //允許此函數(shù)訪問(wèn)這個(gè)類的私有數(shù)據(jù)
             //friend std::ostream& operator<< (std::ostream& os,const ) ?
            };
} //namespace

template<typename T>
inline  std::ostream& operator<< (std::ostream& os,const scpp::vector<T>& v) {
    for(unsigned i=0 ;i<v.size();i++) {
            os << v[i];
            if( i+1 < v.size()) os << " ";
    }
    return os;
}


#endif

//test_vector.cpp
#include "scpp_vector.h"
#include <iostream>

using namespace std;
int main() {
    //usage-創(chuàng)建一個(gè)具有指定數(shù)量的vector:scpp::vector<int> v(n); 把n個(gè)vector元素都初始化為一個(gè)值:scpp::vector<int> v(n,val)
    //方法3:scpp::vector<int> v; v.reserve(n),表示開始的vector是空的,對(duì)應(yīng)的size()為0,
    //并且開始添加元素時(shí),在長(zhǎng)度達(dá)到n之前,不會(huì)出現(xiàn)導(dǎo)致速度降低的容量增長(zhǎng)現(xiàn)象
    scpp::vector<int> vec;
    for(int i=0;i< 3;i++){
        vec.push_back(4*i);
    }
    cout << "The vector is : "<< vec <<endl;

    for(int i=0;i <= vec.size();i++) {
        cout << "Value of vector at index " << i << " is " << vec[i] << endl;
    }
    return 0;
}

  我們直接使用scpp::vector而盡量不與std::vector交叉使用。

2.靜態(tài)數(shù)組

  靜態(tài)數(shù)組是在棧上分配內(nèi)存,而vector模板是在構(gòu)造函數(shù)中用new操作符分配內(nèi)存的,速度相對(duì)慢些,為保證運(yùn)行時(shí)效率,建議使用array模板(同樣也是棧內(nèi)存),實(shí)現(xiàn)代碼和測(cè)試如下:

//scpp_array.h
#ifndef _SCPP_ARRAY_H_  
#define _SCPP_ARRAY_H_

#include "scpp_assert.h"

namespace scpp {

//wrapper around std::vector,在[]提供了臨時(shí)的安全檢查
//fixed-size array
template<typename T,unsigned int N>
class array {
    public:
         typedef unsigned int size_type;

         //常用的構(gòu)造函數(shù) commonly use cons
        array() {}
        explicit array(const T& val) {
            for(unsigned int i=0;i < N;i++) {
                     m_data[i]=val;
                 }
        }
                 
        size_type size() const { 
            return N;
        } //must use const if we use the size()
             
        //Note : we don't provide a copy-cons and assignment operator  ?

        T& operator[] (size_type index) {
             SCPP_ASSERT( index < N,
                     "Index " << index << " must be less than " << N);
             return m_data[index];
         }

         //? difference 
        const T& operator[] (size_type index) const {
             SCPP_ASSERT( index < N ,
                     "Index " << index << " must be less than " << N);
             return m_data[index];
        }

         //模擬迭代器的begin和end方法
         //訪問(wèn)方法accessors
        T* begin() { 
            return &m_data[0];
        }

        const T* begin() const { 
            return &m_data[0];
        }

         //返回越過(guò)數(shù)組尾部的迭代器
        T* end() { 
             return &m_data[N];
        }

        const T* end() const { 
             return &m_data[N];
        }
    private:
        T m_data[N];
    };
} //namespace scpp

template<typename T,unsigned int N>
inline  std::ostream& operator<< (std::ostream& os,const scpp::array<T,N>& v) {
    for(unsigned int i=0 ;i< N;i++) {
            os << v[i];
            if( i+1 < v.size()) os << " ";
    }
    return os;
}
#endif

//test_array.cpp
#include "scpp_array.h"
#include <iostream>
#include <algorithm> //sort algorithm
using namespace std;
int main() {
    //use vector/array class instead of static array or dynamic array
    scpp::array<int,5u > arr(0); 
    arr[0]=7;
    arr[1]=2;
    arr[2]=3;
    arr[3]=9;
    arr[4]=0;

    cout << "Array before sort : " << arr << endl;
    sort(arr.begin(),arr.end());
    cout << "Array after sort : "<< arr << endl;

    arr[5]=8;
    return 0;
}

到此這篇關(guān)于C++索引越界的解決方法的文章就介紹到這了,更多相關(guān)C++索引越界內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ???????C語(yǔ)言實(shí)現(xiàn)單鏈表基本操作方法

    ???????C語(yǔ)言實(shí)現(xiàn)單鏈表基本操作方法

    這篇文章主要介紹了???????C語(yǔ)言實(shí)現(xiàn)單鏈表基本操作方法,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • C/C++實(shí)現(xiàn)馬踏棋盤算法

    C/C++實(shí)現(xiàn)馬踏棋盤算法

    這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)馬踏棋盤算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C語(yǔ)言的數(shù)字游戲算法效率問(wèn)題探討實(shí)例

    C語(yǔ)言的數(shù)字游戲算法效率問(wèn)題探討實(shí)例

    這篇文章主要介紹了C語(yǔ)言的數(shù)字游戲算法效率問(wèn)題探討實(shí)例,需要的朋友可以參考下
    2014-04-04
  • wxWidgets自定義按鈕的方法

    wxWidgets自定義按鈕的方法

    這篇文章主要為大家詳細(xì)介紹了wxWidgets自定義按鈕的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • 在C語(yǔ)言里單引號(hào)和雙引號(hào)的區(qū)別

    在C語(yǔ)言里單引號(hào)和雙引號(hào)的區(qū)別

    這篇文章主要介紹了在C語(yǔ)言里單引號(hào)和雙引號(hào)的區(qū)別,本文通過(guò)代碼的實(shí)例和注釋的詳細(xì)的說(shuō)明了單引號(hào)和雙引號(hào)的概念與區(qū)別,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++ cin.getline及getline()用法詳解

    C++ cin.getline及getline()用法詳解

    這篇文章主要介紹了C++ cin.getline用法及C++ getline()的兩種用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • C語(yǔ)言基礎(chǔ) strlen 函數(shù)

    C語(yǔ)言基礎(chǔ) strlen 函數(shù)

    這篇文章主要介紹了C語(yǔ)言基礎(chǔ) strlen 函數(shù),在C 語(yǔ)言中,char 字符串也是一種非常重要的數(shù)據(jù)類型,我們可以使用 strlen 函數(shù)獲取字符串長(zhǎng)度,這就是C語(yǔ)言strlen 函數(shù)的作用,下面我們來(lái)簡(jiǎn)單介紹該內(nèi)容,需要的朋友可以參考以下
    2021-10-10
  • 詳解C++11中的線程鎖和條件變量

    詳解C++11中的線程鎖和條件變量

    C++ 11允許開發(fā)者們以標(biāo)準(zhǔn)的、不依賴于平臺(tái)的方式編寫多線程程序。這篇文章概述了標(biāo)準(zhǔn)庫(kù)對(duì)于線程和同步操作機(jī)制的支持。這些都是非常重要的知識(shí),希望讀者們可以認(rèn)真看一下
    2021-06-06
  • C++?STL中五個(gè)常用算法使用教程及實(shí)例講解

    C++?STL中五個(gè)常用算法使用教程及實(shí)例講解

    本文主要介紹了C++?STL算法中常見的五個(gè)算法的使用教程并附上了案例詳解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • 解析C++編程中異常相關(guān)的堆棧展開和throw()異常規(guī)范

    解析C++編程中異常相關(guān)的堆棧展開和throw()異常規(guī)范

    這篇文章主要介紹了C++編程中異常相關(guān)的堆棧展開和throw()異常規(guī)范,throw()規(guī)范部分文中結(jié)合了C++11標(biāo)準(zhǔn)的新特性來(lái)講,需要的朋友可以參考下
    2016-01-01

最新評(píng)論

米泉市| 云林县| 咸宁市| 淮阳县| 大洼县| 化州市| 通州市| 通城县| 威信县| 姜堰市| 黔东| 天津市| 慈利县| 兴安县| 运城市| 富裕县| 原阳县| 绥滨县| 晋江市| 湘阴县| 丰镇市| 吴堡县| 潜江市| 新晃| 泊头市| 南丹县| 栾城县| 陵川县| 苍梧县| 鄯善县| 诸城市| 平昌县| 海城市| 永新县| 栾城县| 蒙城县| 桃园市| 祁东县| 天津市| 鄂托克旗| 阳山县|