我有一个像list<T> result;
这样的列表,在for循环中我会在每个传递中生成另一个列表,如
list<T> result;
for(int i=0;i<10;i++){
list<T> temp=generated_list();
// here I want to add at the beginning of the result list values from temp list
}
如何在结果列表的开头插入生成的列表?有没有比循环更好的方法或通过连接创建新的,然后将结果设置为结果列表?
答案 0 :(得分:5)
std::list<T>::splice
会员功能是做你想做的最有效的方式
list<T> result;
for(int i=0;i<10;i++){
list<T> temp=generated_list();
result.splice(result.begin(), temp);
}