如何把vector转换成list
可以使用list的构造函数将vector转换为list,示例如下:
#include <vector>
#include <list>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
list<int> lst(vec.begin(), vec.end()); // 将vec转换为lst
return 0;
}
也可以使用循环将vector中的元素逐个插入到list中,示例如下:
#include <vector>
#include <list>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
list<int> lst;
for (int i = 0; i < vec.size(); ++i) {
lst.push_back(vec[i]); // 将vec中的元素逐个插入到lst中
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/bI79 著作权归作者所有。请勿转载和采集!