STL_Iterator

迭代器iterator是一个很神奇的东西。

STL的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以胶合剂(iterator)将他们撮合在一起。




#pragma once


#include<algorithm>
#include<vector>
#include<list>
#include<string>
void TestVector()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(4);
	v.push_back(3);
	v.push_back(2);

	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	//STL排序头文件在algorithm
	sort(v.begin(), v.end());

	for (it=v.begin(); it < v.end(); it++)
	{
	cout << *it << " ";
	}
	cout << endl;
}

void TestList()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//替换函数 
	replace(l.begin(), l.end(), 3, 4);

	it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
}

void Test3()
{
	list<string> ls;
	ls.push_back("aa");
	ls.push_back("bb");
	ls.push_back("cc");
	ls.push_back("dd");

	list<string>::iterator it = ls.begin();
	while (it != ls.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//find
	it = find(ls.begin(), ls.end(), "aa");
	//it = find(ls.begin(), ls.end(), "ee"TL);
	if (it != ls.end())//如果查找失败(不存在),it就会返回ls.end()
		cout << *it << endl;
	else
		cout << "find false" << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值