C++-STL(10)-list 双向链表

链表:缺点 :无法访问位置 没有Index的概念。数组可以[index].
           优点:在序列已知的任何位置插入或删除元素。可以合并、去重、交换

一样的增删改查上代码
 

void listtest()
{
    cout << "list*******************" << endl;
    cout << "定义 list < string> strlist" << endl;
    list < string > strlist;
    cout << "插入 1.头,push_front;2.尾,push_back;3.指定位置(,,"")" << endl;
    strlist.push_front("frist");
    strlist.push_back("end");
    auto iter = begin(strlist);
    strlist.insert(++iter,3, "a");
    for (auto it = strlist.begin(); it != strlist.end(); it++)
    {
        cout << *it << endl;
    }
    cout << "****************" << endl;
    int a[5] = { 1, 3, 2, 4, 2 };
    int b[7] = { 10, 30, 20, 30, 30, 40, 40 };
    list<int> lst1(a, a + 5);
    list<int> lst2(b, b + 7);
    cout << "1)"; Print(lst1.begin(), lst1.end());  //输出:1)1 2 2 3 4
    lst1.remove(2);                                 //删除所有和A(2)相等的元素
    cout << "2)"; Print(lst1.begin(), lst1.end());  //输出:2)1 3 4
    lst2.pop_front();                                //删除第一个元素
    cout << "3)"; Print(lst2.begin(), lst2.end());  //输出:3)30 20 30 30 40 40
    lst2.unique();                                   //删除所有和前一个元素相等的元素
    cout << "4)"; Print(lst2.begin(), lst2.end());  //输出:4)30 20 30 40
    lst2.sort();
    cout << "5)"; Print(lst2.begin(), lst2.end());  //输出:4)30 20 30 40
    lst1.merge(lst2);                               //合并 lst2 到 lst1 并清空 lst2
    cout << "6)"; Print(lst2.begin(), lst2.end());
    cout << "6)"; Print(lst1.begin(), lst1.end());
    }

 

原文链接: https://www.cnblogs.com/jasmineTang/p/14369293.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++-STL(10)-list 双向链表

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/327697

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午4:22
下一篇 2023年3月1日 下午4:22

相关推荐