vector赋值deque
时间: 2023-08-24 07:09:21 浏览: 66
可以使用`std::vector`的构造函数或`assign()`方法将`std::deque`赋值给`std::vector`。下面是两种方法的示例代码:
方法一:使用构造函数
```cpp
#include <iostream>
#include <vector>
#include <deque>
int main() {
std::deque<int> myDeque = {1, 2, 3, 4, 5};
// 使用vector的构造函数将deque赋值给vector
std::vector<int> myVector(myDeque.begin(), myDeque.end());
// 打印vector的元素
for (const auto& element : myVector) {
std::cout << element << " ";
}
std::cout << std::endl;
return 0;
}
```
方法二:使用`assign()`方法
```cpp
#include <iostream>
#include <vector>
#include <deque>
int main() {
std::deque<int> myDeque = {1, 2, 3, 4, 5};
std::vector<int> myVector;
// 使用assign()方法将deque赋值给vector
myVector.assign(myDeque.begin(), myDeque.end());
// 打印vector的元素
for (const auto& element : myVector) {
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)