欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > list_

list_

2024/12/22 0:18:08 来源:https://blog.csdn.net/m0_63056769/article/details/144436893  浏览:    关键词:list_

 1.对象创建

//
// Created by 徐昌真 on 2024/12/12.
//
#include <iostream>
#include <list>using namespace std;void Print(list<int> &my_list) {for ( list<int>::iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {//默认构造函数list<int> list_1;cout << "list_1: ";Print(list_1);//初始化列表list<int> list_2_1 = {1,2,3,4,5};  //有等号cout << "list_2_1: ";Print(list_2_1);list<int> list_2_2 ( {1,2,3,4,6} );  //没等号cout << "list_2_2: ";Print(list_2_2);//迭代器list<int> list_3( list_2_1.begin(), list_2_1.end() );cout << "list_3: ";Print(list_3);//全0初始化( 指定初始化大小 )list<int> list_4(10);cout << "list_4: ";Print(list_4);//a个blist<int> list_5(8, 6);cout << "list_5: ";Print(list_5);//拷贝构造函数list<int> list_6(list_5);cout << "list_6: ";Print(list_6);return 0;
}

2.赋值操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int> &my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {9,8,5,2,1,1};cout << "list_1: ";Print(list_1);// 1. 等号 = 赋值list<int> list_2;list_2 = list_1;cout << "list_2: ";Print(list_2);// 2. assign(迭代器)list<int> list_3;list_3.assign(list_1.begin(), list_1.end() );cout << "list_3: ";Print(list_3);// 3. 初始化列表list<int> list_4;list_4.assign({1,2,3,4,5});cout << "list_4: ";Print(list_4);// 4. 初始化a 个 blist<int> list_5;list_5.assign(6,8);cout << "list_5: ";Print(list_5);//assign一下之前的东西看看list_5.assign(6,7);  //直接重置cout << "list_5: ";Print(list_5);return 0;
}

输出

3.大小操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}// 1. empty
// 2. size
// 3. resize
int main() {list<int> list_1;cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;cout << endl;list_1 = {1,2,3,4,5};cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;cout << endl;list_1.resize(20);  //全0扩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);cout << endl;list_1.resize(22,985);  //指定数字扩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);cout << endl;list_1.resize(3);  //缩容cout << "empty: " << list_1.empty() << endl;cout << "size: " << list_1.size() << endl;Print(list_1);return 0;
}

输出

4.数据插入

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>
#include <vector>using namespace std;void Print(const list<int>& my_list){for (list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ) {cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1;vector<int> vec= {6,6,6,6,6,6};// 1. push_frontlist_1.push_front(3);list_1.push_front(2);list_1.push_front(1);Print(list_1);  //1 2 3// 2. push_backlist_1.push_back(4);list_1.push_back(5);list_1.push_back(6);Print(list_1);  //1 2 3 4 5 6// 3. insert// (1)  迭代器( 迭代器 + 值)list<int>::iterator iter = list_1.begin();//iter++;        //允许//iter += 1;  //不允许list_1.insert( iter, 0);Print(list_1);  //0 1 2 3 4 5 6// (2)  迭代器( 迭代器 + 数量 + 值)iter = list_1.end();list_1.insert( iter, 8,8);Print(list_1);  //0 1 2 3 4 5 6 8 8 8 8 8 8 8 8// (3)  迭代器( 迭代器 + 迭代器起始位置 + 迭代器末尾位置)     在iter这个迭代器里面 插入从 迭代器起始位置 + 迭代器末尾位置的值 这个迭代器可以来自别的list   居然也可以来自vector 太强了iter = list_1.end();list_1.insert( iter, vec.begin(), vec.end());Print(list_1);  //0 1 2 3 4 5 6 8 8 8 8 8 8 8 8 0 1 2 3 4 5 6 8 8 8 8 8 8 8 8return 0;
}

不理解

输出

5.删除操作

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int>& my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {-1,9,8,5,2,1,1,-1};Print(list_1);  //-1 9 8 5 2 1 1 -1// 1. pop_frontlist_1.pop_front();Print(list_1);  //9 8 5 2 1 1 -1// 2. pop_backlist_1.pop_back();Print(list_1);  //9 8 5 2 1 1//erase//(1)  传入删除的位置(迭代器)list<int>::iterator iter = list_1.begin();list<int>::iterator ret = list_1.erase( iter );Print(list_1);  //8 5 2 1 1cout << *ret << endl;  //8//(2) 传入删除区域(迭代器)iter = list_1.end();  //指向末尾元素的下一个--iter;--iter;ret = list_1.erase( iter, list_1.end() );Print(list_1);  //8 5 2 1cout << *ret << endl;  //3  为什么 ???return 0;
}

输出

6.数据访问

我的疑问 : 是不是顺序表存储元素时分配的内存是连续的 而链表是分开的 所以链表不支持随机访问

回答:

是的,你的理解是正确的:

  1. 顺序表(如 vector:它存储元素的内存是连续的,这意味着每个元素都紧接着前一个元素。由于内存是连续的,vector 可以通过索引直接访问任意位置的元素,因此支持随机访问,访问时间是常数时间 O(1)

  2. 链表(如 list:它是由一系列节点组成的,每个节点包含数据和指向下一个节点的指针。由于内存不一定是连续的,所以不能像 vector 那样直接通过索引快速访问某个位置的元素。要访问链表中的某个元素,必须从头节点开始遍历,直到找到目标元素,因此链表不支持随机访问,访问时间是线性的 O(n)

总结:

  • 顺序表:支持随机访问,内存连续,访问速度快O(1)
  • 链表:不支持随机访问,内存不连续,访问速度慢O(n)
//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){  //输出cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int VisitItemByIndex( list<int>& my_list, int index ){list<int>::const_iterator iter = my_list.begin();while ( index ){++iter;--index;}return *iter;
}int main() {list<int> list_1 = {1,2,3,4,5,6};Print(list_1);  //1 2 3 4 5 6 //frontcout << "list_1.front(): " << list_1.front() << endl;//backcout << "list_1.back(): " << list_1.back() << endl;//迭代器cout << VisitItemByIndex( list_1, 2 );return 0;
}

7.逆序

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( const list<int>& my_list ){for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {1,2,3,4,5};Print(list_1);list_1.reverse();Print(list_1);return 0;
}

8.排序(原理 归并排序)

//
// Created by 徐昌真 on 2024/12/14.
//
#include <iostream>
#include <list>using namespace std;void Print( list<int>& my_list){  //输出cout << "list_1: ";for ( list<int>::const_iterator iter = my_list.begin(); iter != my_list.end(); ++iter ){cout << *iter << ' ';}cout << endl;
}int main() {list<int> list_1 = {1,2,3,4,2,4,56,3,2};list_1.sort();Print(list_1);return 0;
}

输出

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com