简体   繁体   中英

Simplest way to assign std::span to std::vector

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?

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

You can also use thestd::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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM