Search code examples
c++vectorstldeque

How to copy entire vector into deque using inbuilt function? (in C++)


I want to know that is there any inbuilt function to do this task

vector<int> v;
deque<int> d;
for(auto it:v){
    d.push_back(it);
}

I just know this way to copy the values of a vector in deque and I want to know is there any inbuilt function to perform this task


Solution

  • As Pepijn Kramer said in the comments 1 and 2, you can use the overload (2) for the assign member function that takes a range

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

    or use the iterator-range constructor, overload (5)

    std::deque<int> d{v.begin(),v.end()};
    

    Or in C++23, you can do

    auto d = std::ranges::to<std::deque>(v);