久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

對于具有線性存儲的容器,可以使用原始指針代

Can raw pointers be used instead of iterators with STL algorithms for containers with linear storage?(對于具有線性存儲的容器,可以使用原始指針代替帶有 STL 算法的迭代器嗎?) - IT屋-程序員軟件開發(fā)技術分享
本文介紹了對于具有線性存儲的容器,可以使用原始指針代替帶有 STL 算法的迭代器嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個自定義向量容器,它在內(nèi)部存儲一個線性數(shù)組項.昨晚,我試圖為我的類實現(xiàn)自定義迭代器,以便能夠?qū)⑺鼈兣c STL 算法一起使用.我取得了一些成功,你可以在這里看到:

I have a custom vector container that internally stores item a linear array. Last night, I was trying to implement custom iterators for my class to be able to use them with STL algorithms. I have had some success that you can see in here:

自定義迭代器的實例

這樣做時,我發(fā)現(xiàn)我只能將原始指針傳遞給 STL 算法,而且它們似乎工作正常.這是沒有任何迭代器的示例:

While doing so, I discovered I can merely pass raw pointers to STL algorithm and they just seem to work fine. Here's the example without any iterators:

#include <cstddef>
#include <iostream>
#include <iterator>
#include <algorithm>

template<typename T>
class my_array{
    T* data_;
    std::size_t size_;

public:

    my_array()
        : data_(NULL), size_(0)
    {}
    my_array(std::size_t size)
        : data_(new T[size]), size_(size)
    {}
    my_array(const my_array<T>& other){
        size_ = other.size_;
        data_ = new T[size_];
        for (std::size_t i = 0; i<size_; i++)
            data_[i] = other.data_[i];
    }
    my_array(const T* first, const T* last){
        size_ = last - first;
        data_ = new T[size_];

        for (std::size_t i = 0; i<size_; i++)
            data_[i] = first[i];
    }

    ~my_array(){
        delete [] data_;
    }
    const my_array<T>& operator=(const my_array<T>& other){
        size_ = other.size_;
        data_ = new T[size_];
        for (std::size_t i = 0; i<size_; i++)
            data_[i] = other.data_[i];
        return other;
    }
    const T& operator[](std::size_t idx) const {return data_[idx];}
    T& operator[](std::size_t& idx) {return data_[idx];}
    std::size_t size(){return size_;}

    T* begin(){return data_;}
    T* end(){return data_+size_;}
};

template<typename T>
void print(T t) {
    std::cout << t << std::endl;
}

int main(){


    typedef float scalar_t;
    scalar_t list [] = {1, 3, 5, 2, 4, 3, 5, 10, 10};
    my_array<scalar_t> a(list, list+sizeof(list)/sizeof(scalar_t));

    // works!
    for (scalar_t* it = a.begin(), *end = a.end();
         it != end; ++it)
        std::cout << ' ' << *it;
    std::cout << std::endl;

    // works!
    std::for_each(a.begin(), a.end(), print<scalar_t>);
    std::cout << std::endl;

    // works!
    my_array<int> b(a.size());
    std::copy(a.begin(), a.end(), b.begin());

    // works!
    scalar_t* end = std::remove(a.begin(), a.end(), 5);
    std::for_each(a.begin(), end, print<scalar_t>);
    std::cout << std::endl;

    // works!
    std::random_shuffle(a.begin(), end);
    std::for_each(a.begin(), end, print<scalar_t>);
    std::cout << std::endl;

    // works!
    std::cout << "Counts of 3 in array = " << std::count(a.begin(), end, 3) << std::endl << std::endl;

    // works!
    std::sort(a.begin(), end);
    std::for_each(a.begin(), end, print<scalar_t>);
    std::cout << std::endl;

    // works!
    if (!std::binary_search(a.begin(), a.end(), 5))
        std::cout << "Removed!" << std::endl;

    return 0;
}

沒有迭代器的實例

我的問題如下:

  1. 這是否總是適用于具有線性存儲的容器?例如,我知道這不適用于鏈表.
  2. 如果它們在這種情況下確實有效,我為什么還要經(jīng)歷實現(xiàn)迭代器的麻煩?我知道迭代器如何概括我的代碼等等,但是如果這個簡單的數(shù)組就是我所需要的一切,那么我就看不到重點了.
  3. 如果這種方法始終有效,我正在做的事情有哪些負面問題?一方面,我可以看到我正在破壞數(shù)據(jù)封裝.
  1. Does this always work for containers that have linear storage? I know that this would not work for linked-lists for example.
  2. If they do work in this situation, why should I ever go through the hassle of implementing iterators anyway? I know how iterators generalize my code and whatnot, but if this simple array is all I ever need then I don't see the point.
  3. What are the negative issues of what I'm doing if this approach would always work? For one thing, I can see I'm breaking data encapsulation.

推薦答案

基于運算符重載的迭代器的特性之一是指針已經(jīng)是隨機訪問迭代器.在 STL 的早期,這是一個巨大的設計勝利,因為它可以更輕松地將算法與現(xiàn)有代碼一起使用(并使程序員更熟悉界面).包裹一個數(shù)組是完全合理的,添加 typedef T* iterator;typedef const T* const_iterator,從你的 begin()&array[size] 返回 &array[0]從您的 end() 開始,然后將您的容器與任何基于迭代器的算法一起使用.正如您已經(jīng)意識到的,這適用于任何元素在內(nèi)存中是連續(xù)的容器(例如數(shù)組).

One of the features of iterators being based on operator-overloading, is that pointers are already random-access iterators. This was a big design win in the early days of STL, as it made it easier to use algorithms with existing code (as well as making the interface more familiar to programmers). It's perfectly reasonable to wrap an array, add typedef T* iterator; typedef const T* const_iterator, return &array[0] from your begin() and &array[size] from your end(), and then use your container with any iterator-based algorithm. As you already realise, this will work for any container where elements are contiguous in memory (such as an array).

如果滿足以下條件,您可能會實現(xiàn)真正的"迭代器:

You might implement 'real' iterators if:

  • 您有一個不同形狀的容器(例如樹或列表);
  • 您希望能夠在不使迭代器失效的情況下調(diào)整數(shù)組大小;
  • 您想在迭代器使用中添加調(diào)試檢查,例如檢查迭代器是否在失效或容器被刪除后使用,或進行邊界檢查;
  • 您想引入類型安全性,并確保人們不會意外地將任意 T* 分配給 my_array::iterator.
  • You have a different-shaped container (such as a tree or list);
  • You want to be able to resize the array without invalidating the iterators;
  • You want to add debugging checks to your iterator use, for example to check if the iterator is used after being invalidated or after the container has been deleted, or to bounds-check;
  • You want to introduce type-safety, and make sure people can't accidentally assign an arbitrary T* to a my_array::iterator.

我想說僅憑最后一個優(yōu)勢就值得為其編寫一個簡單的包裝類.如果你不利用 C++ 的類型系統(tǒng)讓不同類型的東西有不同的類型,你不妨切換到 Javascript :-)

I'd say this last advantage alone is well worth writing a trivial wrapper class for. If you don't take advantage of C++'s type system by making different kinds of thing have different types, you might as well switch to Javascript :-)

這篇關于對于具有線性存儲的容器,可以使用原始指針代替帶有 STL 算法的迭代器嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 欧美一级视频在线观看 | xx性欧美肥妇精品久久久久久 | 日韩精品一区二区三区中文字幕 | 久草在线中文888 | 美女黄网 | 亚洲天堂精品久久 | 中文字幕亚洲一区二区va在线 | 成人免费一区二区三区牛牛 | 亚洲综合中文字幕在线观看 | 三级视频国产 | 在线日韩| 男人天堂久久 | 久久久久久国产精品免费免费狐狸 | 国产精品精品视频一区二区三区 | 最新日韩欧美 | www.久久艹 | 国产精品国产三级国产播12软件 | 久久成人高清视频 | av成人在线观看 | 国产精品日韩一区 | 在线观看www视频 | 亚洲高清在线观看 | 日韩高清国产一区在线 | www亚洲成人 | 日韩欧美中文字幕在线观看 | 狠狠婷婷综合久久久久久妖精 | 久久久国 | 免费黄色片视频 | 亚洲一区二区三区免费视频 | 天天看片天天干 | 日韩2020狼一二三 | 日本欧美国产在线 | 亚洲欧洲在线观看视频 | 国产一级成人 | 国产精品视频网址 | 成人在线播放网站 | 一区二区手机在线 | 久久网一区二区 | 国产a视频| 国产高清在线 | 中文字幕 国产 |