迭代器的相关知识

迭代器

#include <iostream>
#include <string>
#include <list>
using namespace std;

// 迭代器是STL框架的六大组件之一
//迭代器 遍历string ---- iterator 像指针一样类型,有可能是指针,也有可能不是指针,但是它的用法像指针一样。
// 
//string、vector不喜欢用iterator, 因为[]更好用。 然而,list\map\set...只能用迭代器访问(因为它们的底层空间不是连续的数组,无法用[] )。
// iterator 是所有容器通用的访问方式,并且用法都是类似的。

1、迭代器与范围for

void test_string4()
{
	string s("hello");
	string::iterator it = s.begin(); //s.begin() 返回的是s的第一个字符的位置
	while (it != s.end()) // s.end()返回的是s的最后一个字符的下一个字符的位置(即 '/0')
						  //迭代器给的位置范围为:[s.begin(), s.end()) 左闭右开
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//范围for ---- 自动迭代,自动判断结束
	//范围for的底层是迭代器
	for (auto& ch : s) //依次取s中的每个字符赋值给ch;auto& 就可以通过 ch 修改 s
	{
		cout << ch << " ";
	}
	cout << endl;


	//iterator遍历链表
	list<int> lt(10, 1); //用10个1去初始化链表 lt
	list<int>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		++lit;
	}
	cout << endl;

	// 范围for(适用于所有容器)
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

}

2、其余类型的迭代器

void PrintString(const string& str)
{
	//const迭代器
	string::const_iterator it = str.begin();
	while (it != str.end())
	{
		//*it = 'x';
		cout << *it << " ";
		++it;
	}
	cout << endl;

	//const 反向 迭代器
	string::const_reverse_iterator rit = str.rbegin();
	while (rit != str.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;


}

void test_string5()
{
	string s("hello");
	//反向迭代器
	string::reverse_iterator rit = s.rbegin(); // s.rbegin 为 字符串s反向的第一个字符位置
	while (rit != s.rend())  // s.rend() 为 字符串s反向的最后一个字符的下一个字符的位置 {位置范围是左开右闭}
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;

	PrintString(s);

}
//从遍历的角度来分析:四种迭代器
// iterator/const_iterator
// reverse_iterator/const_rverse_iterator

3、string插入

void test_string6()
{
	string s("hello");
	s.push_back('-');
	s.push_back('-');
	s.append("world");
	cout << s << endl;

	string str("我来了");
	s += '@';
	s += str;
	s += "!!!";
	cout << s << endl;

    //迭代器的区间
	s.append(str.begin(),str.end());
	cout << s << endl;

	//添加 字符串的某一段
	s.append(++s.begin(), --s.end());
	cout << s << endl;

	string copy(s.begin()+5,s.end()-5);
	cout<< copy <<endl;
}


4、string的扩容

```cpp
void test_string7()
{
	/*string s1;
	string s2("111111111111");
	cout<< s1.max_size() <<endl;
	cout << s2.max_size() << endl;

	cout<< s1.capacity() <<endl;
	cout << s2.capacity() << endl;
*/

	//string的扩容机制
	string s;
	//reverse 逆置,反转    reserve 保留,开空间
	//s.reserve(500); //提前确定好string的容量(提前把空间给开好)

	s.resize(500,'1');  //开空间+初始化 (不仅确定了_capacity,也确定了_size)
	size_t sz = s.capacity();
	cout<< sz <<endl;
	cout <<"making s grow:"<<endl;
	for (int i=0;i<1000;++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout<< "capacity changed " << sz << '\n';
		}
	}
}

int main()
{
	test_string7();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值