Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can std::vector be converted to std::span?

Tags:

c++

c++20

In the docs on std::span constructors, there isn't one that accepts an std::vector.

Then, how come this code (source page) compiles?

// createSpan.cpp

#include <algorithm>
#include <iostream>
#include <span>
#include <vector>

int main() {

    std::cout << std::endl;
    std::cout << std::boolalpha;

    std::vector myVec{1, 2, 3, 4, 5};
    
    std::span mySpan1{myVec};                                        // (1)
    std::span mySpan2{myVec.data(), myVec.size()};                   // (2)
    
    bool spansEqual = std::equal(mySpan1.begin(), mySpan1.end(),
                                 mySpan2.begin(), mySpan2.end());
    
    std::cout << "mySpan1 == mySpan2: " << spansEqual << std::endl;  // (3)

    std::cout << std::endl;
    
}

i.e. which constructor of std::span was invoked at (1)?

like image 958
Leedehai Avatar asked Nov 18 '20 20:11

Leedehai


People also ask

What is the difference between std::vector and vector?

Difference between std::vector and std::array in C++Array stores a fixed-size sequential collection of elements of the same type and it is index based. Vector is dynamic in nature so, size increases with insertion of elements. As array is fixed size, once initialized can't be resized. Vector occupies more memory.

What is STD span?

With C++20, the answer is quite easy: Use a std::span. A std::span stands for an object that can refer to a contiguous sequence of objects. A std::span, sometimes also called a view, is never an owner. This contiguous memory can be a plain array, a pointer with a size, a std::array, a std::vector, or a std::string.

What is an std::vector?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.

Is std::vector fast?

A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.

Is there an STD::span constructor that accepts a vector?

In the docs on std::span constructors, there isn't one that accepts an std::vector. Then, how come this code ( source page) compiles?

What is a span in Python?

Let's start with std::span! What is a Span? The class template span describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero. Spans are sometimes called "views" because they don't own the sequence of objects.

What is a span in C++?

A span provides a safe way to iterate over, and index into, objects that are arranged back-to-back in memory such as objects stored in a built-in array, std::array, or std::vector. If you typically access a sequence of back-to-back objects using a pointer and an index, a span is a safer, lightweight alternative.

How do I convert a vector to a set in Python?

Get the vector to be converted. Create an empty set, to store the result. Iterate through the vector one by one, and insert each element into the set. Print the resultant set.


1 Answers

It's the constructor (7) in the overload set:

template< class R >
explicit(extent != std::dynamic_extent)
constexpr span( R&& r );

From the explanation:

Constructs a span that is a view over the range r; the resulting span has size() == std::ranges::size(r) and data() == std::ranges::data(r).

There are further restriction on when this signature takes part in overload resolution, but that's the essence. And std::vector satisfies these requirements.

It's a good thing that this is kept only as constrained as necessary, because it allows custom (non-std) containers to allow for implicit conversion to std::span instances.

like image 65
lubgr Avatar answered Oct 08 '22 15:10

lubgr