c++ forward_list 的使用

/*C++11 forward_list的使用*/

bool signal(const int&v)
{
	if (v > 10 && v < 30)
	{
		return true;
	}
	return false;
}

bool signal2(const int&v1,const int &v2)
{
	if (v1 > v2)
	{
		return true;
	}
	return false;
}

void main5_6()
{
	int arr[] = { 2, 3, 4, 5 };
	int size = sizeof(arr) / sizeof(int);

	std::forward_list<int> myList(arr, arr + size);

	auto it = myList.before_begin();//指向头一个元素的前一个位置,这个是单链表,方便插入数据
	it++;
	while (it != myList.end())
	{
		
		cout <<*it<< endl;
		it++;
	}

	if (myList.empty())
	{
		cout << "Empty 1" << endl;
	}
	else
	{
		cout << "Not Empty 1" << endl;
	}
	
	
	forward_list<int>::reference re = myList.front();// 返回第一个元素的引用;
	
	cout << re << endl;
	re = 22;
	it = myList.begin();
	cout << *it << endl;

	//myList.~forward_list();   //销毁整个list
	
	cout <<" *****" << endl;
	myList.push_front(12); //在头部添加一个元素
	////myList.pop_front();

	it = myList.before_begin();
	myList.insert_after(it, 99); //在迭代器后面开始添加元素
	
	//myList.insert_after(it, arr, arr + size); //插入数组的元素
	//myList.insert_after(it, 10, 100);//插入10个值为100的元素
	
	it = myList.begin();
	//myList.erase_after(it);  //删除it位置的元素
	auto it2 = myList.end();
	/*it2++;
	it2++;
	it2++;*/

	//myList.erase_after(it, it2); //删除it下一个元素到it2上一个元素之间的所有元素

	myList.remove(99); //删除值为99的元素

	
	//myList.reverse();  //所有元素反序
	myList.sort();   //所有元素重新排序,默认是升序
	//myList.sort(signal2); //从大道小排序

	/*myList.remove_if(signal);*/

	std::forward_list<int> myList2 = {7,100,111};//要保证也是降序,
	
	myList2.sort();

	myList.merge(myList2);  //只支持升序
	it = myList.begin();
	while (it != myList.end())
	{
		cout << *it << endl;
		it++;
	}




	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值