10

I wanted to do this

#include <vector>
#include <span>

struct S
{
    std::vector<int> v;
    void set(std::span<int> _v)
    {
        v = _v;
    }
};

But it does not compile. What are the alternatives?

JeJo
  • 30,635
  • 6
  • 49
  • 88
tuket
  • 3,232
  • 1
  • 26
  • 41

2 Answers2

15
v.assign(_v.begin(), _v.end());
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
3

You can also use the std::vector::insert as follows:

v.insert(v.begin(), _v.begin(), _v.end());

Note that, if the v should be emptied before, you should call v.clear() before this. However, this allows you to add the span to a specified location in the v.

(See a demo)

JeJo
  • 30,635
  • 6
  • 49
  • 88