第十六章 string类和标准模板库

泛型编程(迭代器)

迭代器的概念:

迭代器是一种遍历容器内元素的对象(指针).它使用了一种统一的访问元素的方式.

迭代器的定义形式:

container_type::iterator iterator_name;

其中container_type是容器名字, iterator是容器提供的迭代器类型, iterator_name是迭代器对象的名称.如定义一个vector容器的迭代器对象

vector<int>::iterator iter;

 迭代器的类型:

1: 输入迭代器

2: 输出迭代器

3: 正向迭代器

4: 反向迭代器

5: 随机访问迭代器

const iterator迭代器

cbegin(), cend()迭代器:表示不能改变迭代器指向的内容不能被修改.

迭代器失效

情况1:

不要在有迭代器使用的循环体类,改变迭代器的容量, 不然会造成迭代器失效.

#include<iostream>
#include<vector>
int main()
{
	using std::endl;
	using std::cout;
	std::vector<int>::iterator iter;
	std::vector<int> arr{1, 2, 3, 4, 5, 6};
	for (iter = arr.begin(); iter != arr.end(); iter++)
	{
		arr.push_back(45);
		cout << *iter << endl;
	}
	return 0;
}
#include<iostream>
#include<vector>
int main()
{
	using std::endl;
	using std::cout;
	std::vector<int> arr{1, 2, 3, 4, 5, 6};
	std::vector<int>::iterator iter = arr.begin();
	std::vector<int>::iterator it_end = arr.end();
	while (iter != it_end)
	{
		arr.push_back(56);
		cout << *iter << endl;
		iter++;
	}
	return 0;
}

像这样会造成迭代器的失效, 为了避免迭代器的失效, 应该在第一次插入数据时就退出循环.

 或者及时的更新迭代器指向.

#include<iostream>
#include<vector>
int main()
{
	using std::endl;
	using std::cout;
	std::vector<int> arr{1, 2, 3, 4, 5, 6};
	std::vector<int>::iterator iter = arr.begin();
	std::vector<int>::iterator it_end = arr.end();
	int cnt = -3;
	while (iter != arr.end())
	{
		iter = arr.insert(iter, cnt);
		cnt++;
		if (cnt >= 1)
			break;
		iter++;
	}

	for (auto c : arr) cout << c << endl;

	return 0;
}

情况2:

#include<iostream>
#include<vector>
int main()
{
	using std::endl;
	using std::cout;
	std::vector<int> arr{1, 2, 3, 4, 5, 6};

	for (auto iter = arr.begin(); iter != arr.end(); iter++)
	{
		arr.erase(iter); // 移除iter位置上的元素, 返回下一个元素的位置.
	}
	return 0;
}

修改容器的大小, 会造成迭代器失效.解决办法

#include<iostream>
#include<vector>
int main()
{
	using std::endl;
	using std::cout;
	std::vector<int> arr{1, 2, 3, 4, 5, 6};

	while (!arr.empty())
	{
		auto iter = arr.begin();
		arr.erase(iter);
	}

	return 0;
}

 案例演示

案例1: 用迭代器遍历一下string类型数据

#include<iostream>
#include<string>

int main()
{
	using std::endl;
	using std::cout;

	std::string str = "I Love China";
	for (auto iter = str.begin(); iter != str.end(); iter++)
	{
		cout << *iter;
	}

	return 0;
}

 案例2: vector容器常用操作与内存释放

#include<iostream>
#include<vector>

struct Serve_C
{
	char itemname[100];
	char itemid[100];
};

int main()
{
	Serve_C* p1 = new Serve_C;
	Serve_C* p2 = new Serve_C;

	strncpy(p1->itemname, "Servename", sizeof(p1->itemname));
	strncpy(p1->itemid, "1区", sizeof(p1->itemid));

	strncpy(p2->itemname, "Serveid", sizeof(p2->itemname));
	strncpy(p2->itemid, "100000", sizeof(p2->itemid));
	
	std::vector<Serve_C*> S_List;
	S_List.push_back(p1);
	S_List.push_back(p2);

	std::cout << S_List[0]->itemname << " " << S_List[0]->itemid << std::endl;
	std::cout << S_List[1]->itemname << " " << S_List[1]->itemid << std::endl;

	for (auto iter = S_List.begin(); iter != S_List.end(); iter++)
	{
		delete (*iter);
	}

	S_List.clear();
	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值