如何在结果列表的开头插入生成的列表

How to insert generated list at the beginning of the result list?

本文关键字:列表 插入 开头 结果      更新时间:2023-10-16

我有一个类似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
    }

如何在结果列表的开头插入生成的列表?有没有比循环或通过串联创建新的,然后将结果设置为结果列表更好的方法?

std::list<T>::splice成员函数是实现功能的最有效方法

list<T> result;
    for(int i=0;i<10;i++){
       list<T> temp=generated_list();
        result.splice(result.begin(), temp);
    }