0
点赞
收藏
分享

微信扫一扫

C++ list的使用(蓝桥杯比赛必备知识)

茗越 2022-04-13 阅读 50

 

目录

list的介绍

list的使用

constructor

list()

list(size_type, const value_type& val = value_type())

list(InputIterator first, InputIterator last)

list(const list& x)

iterator

begin/cbegin

end/cend

rbegin/crbegin

rend/crend

list capacity

empty

size

list element access 

front

back

list modifiers

push_back

pop_back

push_front

pop_front

insert 

erase

swap

clear

operator=

remove

splice

sort

list的迭代器失效

list和vector的对比 


list的介绍

我们再来回顾一下双向链表的结构:

之前我写过这样的博客哦~    

双向链表的实现_暴走的橙子~的博客-CSDN博客_双向链表实现 

list的使用

constructor

list()

举个栗子:

int main()
{
	list<int> l;
	return 0;
}

我们调试一下来看:

list(size_type, const value_type& val = value_type())

举个栗子:

int main()
{
	list<int> l(5, 10);
	return 0;
}

我们调试一下来看:

list(InputIterator first, InputIterator last)

举个栗子:

int main()
{
	list<int> l1(5, 10);
	list<int> l2(l1.begin(), l1.end());
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

       

list(const list& x)

举个栗子:

int main()
{
	list<int> l1(5, 10);
	list<int> l2(l1);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

iterator

begin/cbegin

 

end/cend

rbegin/crbegin

rend/crend

针对上面的迭代器使用举个栗子:

int main()
{
	list<int> l = { 1,2,3,4,5 };//C++11的语法
	//正向迭代器
	list<int>::iterator it1 = l.begin();
	while (it1 != l.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	//反向迭代器
	list<int>::reverse_iterator it2 = l.rbegin();
	while (it2 != l.rend())
	{
		cout << *it2 << " ";
		it2++;
	}
	cout << endl;
	return 0;
}

运行结果:

list capacity

empty

举个栗子:

int main()
{
	list<int> l1;
	cout << "l1.empty():" << l1.empty() << endl;

	list<int> l2(5, 10);
	cout << "l2.empty():" << l2.empty() << endl;
	return 0;
}

运行结果:

size

举个栗子:

int main()
{
	list<int> l1;
	cout << "l1.size():" << l1.size() << endl;

	list<int> l2(5, 10);
	cout << "l2.size():" << l2.size() << endl;
	return 0;
}

运行结果:

list element access 

front

举个栗子:

int main()
{
	list<int> l = { 1,2,3,4,5 };//C++11的语法
	cout << l.front() << endl;
}

运行结果:

back

举个栗子:

int main()
{
	list<int> l = { 1,2,3,4,5 };//C++11的语法
	cout << l.back() << endl;
}

运行结果:

list modifiers

push_back

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

pop_back

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.pop_back();
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

push_front

 

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_front(10);
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

pop_front

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.pop_front();
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

insert 

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	auto it = find(l.begin(), l.end(), 2);
	l.insert(it, 20);
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

erase

举个栗子:

int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	auto it = find(l.begin(), l.end(), 3);
	l.erase(it);
	for (auto e : l)
	{
		cout << e << " ";
	}
	cout << endl;

	list<int> l2 = { 1,2,3,4,5,6,7,8,9,10 };
	l2.erase(l2.begin(), l2.end());
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

swap

举个栗子:

int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
	l1.push_back(5);

	list<int> l2;
	l2.push_back(10);
	l2.push_back(20);
	l2.push_back(30);
	l2.push_back(40);
	l2.push_back(50);

	l1.swap(l2);

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

clear

举个栗子:

int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
	l1.push_back(5);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;

	l1.clear();

	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

operator=

举个栗子:

int main()
{
	list<int> l1 = { 1,2,3,4,5 };
	list<int> l2;
	l2 = l1;
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

remove

举个栗子:

int main()
{
	list<int> l1 = { 1,2,2,4,5,5,6,2,9,2 };
	l1.remove(2);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

splice

举个栗子:

int main()
{
	//void splice (iterator position, list& x, iterator i);
	list<int> l1 = { 1,2,3,4,5 };
	list<int> l2 = { 10,20,30,40,50 };
	auto it1 = find(l1.begin(), l1.end(), 2);
	auto it2 = find(l2.begin(), l2.end(), 20);
	l1.splice(it1, l2, it2);
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto e : l2)
	{
		cout << e << " ";
	}
	cout << endl;

	//void splice (iterator position, list& x);
	list<int> l3 = { 1,2,3,4,5 };
	list<int> l4 = { 10,20,30,40,50 };
	auto it3 = find(l3.begin(), l3.end(), 2);
	l3.splice(it3, l4);
	for (auto e : l3)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto e : l4)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

sort

举个栗子:

int main()
{
	list<int> l1 = { 1,2,2,4,5,5,6,2,9,2 };
	l1.sort();
	for (auto e : l1)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

list的迭代器失效

错误例子:

int main()
{
	list<int> l1 = { 1,2,3,4,5 };
	list<int>::iterator it = l1.begin();
	while (it != l1.end())
	{
		//在erase执行以后,it所指向的节点已被删除,因此it无效,在
		//  下一次使用it时,必须先给其赋值
		l1.erase(it);
		it++; //it已经无效了,不能再使用迭代器++了,it不像string和vector是指针,在这里他是一个对象
	}
	return 0;
}

运行一下:

正确写法: 

int main()
{
	list<int> l1 = { 1,2,3,4,5 };
	list<int>::iterator it = l1.begin();
	while (it != l1.end())
	{
		l1.erase(it++); //it++后,it在erase前被更新,用它的返回值的迭代器(原来的it)来完成erase
	}
	return 0;
}

运行一下:

list和vector的对比 

说明:本质上vector和list是互补的两个数据结构。

看到这里希望大家给博主点个关注~  

下一篇更新list的模拟实现(这个还是有点小复杂的)~

                         

举报

相关推荐

0 条评论