0
点赞
收藏
分享

微信扫一扫

C++ 迭代器 iterator 的用法

飞鸟不急 2022-01-31 阅读 41
#include "project4.h"
#include <cstdio>//stdio.h
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct conf
{
	char itemName[40];
	char itemCount[100];
};
// * pitem 是char 型的指针。 & confList是confList的地址
// (*pos)->itemName 与 pitem 比较 ,(*pos)->itemName 表示是指针指向的字段变量

char* getinfo(vector<conf*>& confList, const char* pitem)
{
	for (auto pos = confList.begin(); pos!=confList.end();++pos)
	{
		if (_strcmpi((*pos)->itemName, pitem)==0)// _strcmpi 比较二者是否相等,相等则返回0. A->B 则A 为指针,-> 是成员提取
		{
			return (*pos)->itemCount;
		}
	}
	return nullptr;
}

// 指针的两个例子

//int main()
//{
//	int a = 3;
//	int* b = &a;  ///!!!
//	cout << "a:" << a << endl; //a:3
//	cout << "b:" << b << endl; //b:012FFEB8
//	*b = 10; 
//	cout << "&a:" << &a << endl;//&a:012FFEB8
//	cout << "b:" << b << endl; //b:012FFEB8
//	cout << "a:" << a << endl; //a : 10
//	system("pause");
//	//分析:
//	//b是a的指针,指向a的地址。(也就是a与b相连,只要修改* b的值,a的值也跟着改动)
//}

//int main()
//{
//	int  var = 20;   // 实际变量的声明
//	//int* ip;        // 指针变量的声明 
//	//ip = &var;     // 在指针变量中存储 var 的地址 !!! 与下面 int* ip = &var; 一样
//
//	int* ip = &var;
//     
//	cout << "Value of var variable: ";
//	cout << var << endl;
//
//	// 输出在指针变量中存储的地址
//	cout << "Address stored in ip variable: ";
//	cout << ip << endl;
//
//	// 访问指针中地址的值
//	cout << "Value of *ip variable: ";
//	cout << *ip << endl;
//
//	return 0;
//}

//Value of var variable : 20
//Address stored in ip variable : 0xbfc601ac
//Value of * ip variable : 20

int main()
{
	//迭代器
	vector<int> iv = { 100,200,300 };
	//vector<int>::iterator iter;//定义迭代器,也必须是vector<int> 有点像指针,指向某个元素
	//iter = iv.begin(); // 返回迭代器,指向iv的第一个元素iv[0]
	//iter = iv.end(); 指向最后一个元素 再往后一个空位置

	vector<int> iv2 = { 100,200,300 };
	/// <summary>
	/// 传统用法
	/// </summary>
	/// <returns></returns>
	//for (vector<int>::iterator iter = iv2.begin(); iter != iv2.end(); iter++)
	//{
	//	cout << *iter << endl;
	//}


	/// <summary>
	/// 反向迭代器
	/// </summary>
	/// <returns></returns>
	//for (vector<int>::reverse_iterator riter = iv2.rbegin(); riter != iv2.rend(); riter++)
	//{
	//	cout << *riter << endl;
	//}
	
	//容器类名::iterator  迭代器名;  迭代器(STL迭代器)iterator
	vector<int>::iterator iter = iv.begin();
	iter++;
	cout << *iter << endl; // 200 从100 往下迭代一个 iter--指向上一个

	//const_iterator 常量指针,只能读,不能改写。删除第一个元素的方法。

	while (!iv.empty())
	{
		auto iter = iv.begin();
		iv.erase(iter); //不断删除第一个元素
	}

	//转成大写
	cout << "----------------以下为转成大写-------------------------" << endl;
	string str("I love you!");
	for (auto iter = str.begin(); iter != str.end(); ++ iter)
	{
		*iter = toupper(*iter); //转成大写
	}
	cout << str << endl;

	cout << "------------------以下为指针迭代器----------------------" << endl;

	//先建立2个结构体,在main文件之前已经定义

	conf* pconf1 = new conf; //给pconf1开辟内存空间


	//strcpy_s(name1,name2.size()+1,name2.c_str()); 将name2 复制到name1
	strcpy_s(pconf1->itemName, sizeof(pconf1->itemName), "ServerName"); 
	 

	strcpy_s(pconf1->itemCount, sizeof(pconf1->itemCount), "1区");

	conf* pconf2 = new conf; //给pconf1开辟内存空间

	strcpy_s(pconf2->itemName, sizeof(pconf2->itemName), "ServerID");

	strcpy_s(pconf2->itemCount, sizeof(pconf2->itemCount), "10000");

	vector<conf*>confList;

	//add到指针迭代器
	confList.push_back(pconf1); //[0]
	confList.push_back(pconf2); //[1]

	//cout << "---------------以下为指针迭代器-------------------------" << endl;

	char* p_temp = getinfo(confList, "ServerName"); //getinfo返回值类型为char* 指针

	if (p_temp != nullptr)
	{
		cout << p_temp << endl; //1区 *p_temp是指针
	}


	cout << "---------------释放内存-------------------------" << endl;

	std::vector <conf*> ::iterator pos;
	for (pos=confList.begin();pos != confList.end();++pos)
	{
		delete (*pos); // *pos 才是那个指针 此时 还有数,只是内存地址无效了
	}
	confList.clear(); //清空迭代器
	return 0;
}

在这里插入图片描述

举报

相关推荐

0 条评论