c++algorithmc++17rangestdvector

How to get const references to a range of std::vector elements?


I want to get a range of elements from a std::vector<MyClass> and store them as const-ref, because I only want to read but not modify them.

#include <iostream>
#include <vector>

// A "large" user class
class MyClass 
{
public:
    int data;
    MyClass(int val) : data(val) {} 
};    

int main() 
{
    // Initialize vector to MyClass objects
    std::vector<MyClass> myClass{1, 2, 3, 4, 5, 6, 7, 8};

    // Get const reference to element with index 0
    const auto &el = myClass[0]; 

    // Get const references to a range of elements
    const unsigned int start = 1, end = 4;
    // const auto &elRange = ???

    return 0;
}

How can I achieve something like this in C++17 or lower?


Solution

  • I want to get a range of elements from a std::vector and store them as const references, because I only want to read but not modify them.

    The best would be using std::span, which requires support.

    Otherwise, you write a simple SimpleSpan class to do so. As simple as:

    // A simple span-like class for C++17
    template<typename T>
    class SimpleSpan 
    {
       T* data_;
       std::size_t size_;
    
    public:
       constexpr SimpleSpan(T* data, std::size_t size) : data_{ data }, size_{ size } {}
    
       constexpr T& operator[](std::size_t index) { return data_[index]; }
       constexpr T& operator[](std::size_t index) const { return data_[index]; }
    
       constexpr T* begin() { return data_; }
       constexpr T* end() { return data_ + size_; }
       constexpr T* begin() const { return data_; }
       constexpr T* end() const { return data_ + size_; }
    
       constexpr std::size_t size() const { return size_; }
    };
    

    and just

    // Get const references to a range of elements using our SimpleSpan
    const unsigned int start = 1, end = 4;
    SimpleSpan<const MyClass> elRange(&myClass[start], end - start);
    

    See live demo