C++(STL)学习(一)

基本概念

STL(标准模板库)
STL从广义上分为:容器、算法、迭代器,容器和算法之间通过迭代器进行无缝链接。
STL六大组件:容器、算法、迭代器、仿函数、适配器、空间配置器
容器:各种数据结构(类模板)
算法:各种常用的算法(函数模板)
迭代器:所有的容器都有自己的迭代器

容器

序列式容器
关联式容器

算法

质变算法:运算过程中改变区间内的元素的内容,例如拷贝,替换,删除等
非质变算法:运算过程中不会改变区间内的元素的内容,例如查找、计数、遍历等

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

void MyPrint(int val)
{
	cout << val << endl;
}



//  普通指针也是属于一种迭代器
void test01()
{
	int arr[5] = { 1,2,3,4,5 };
	int *p = arr;
	for (int i = 0; i < 5; i++)
	{
		//	cout << arr[i] << endl;
		cout << *p++ << endl;
	} 
}

void test02()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	//  遍历1
	/*vector<int>::iterator itBegin = v.begin();

	vector<int>::iterator itEnd = v.end();  //  指向最后一个元素的下一个位置

	while (itBegin != itEnd)
	{
		cout << *itBegin << endl;
		itBegin++;
	}*/

	//  遍历2
	/*for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << endl;
	}*/

	//  遍历3
	for_each(v.begin(), v.end(), MyPrint);

}

//  自定义数据类型

class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}

	string m_name;
	int m_age;
};


void test03()
{
	/*vector<Person>v;
	Person p1("aaaa", 10);
	Person p2("bbbb", 20);
	Person p3("cccc", 30);
	Person p4("dddd", 40);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	//  遍历
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it).m_name;
		cout << it->m_age << endl;
	}*/

	vector<Person *>v;
	Person p1("aaaa", 10);
	Person p2("bbbb", 20);
	Person p3("cccc", 30);
	Person p4("dddd", 40);

	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);

	//  遍历
	for (vector<Person *>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (**it).m_name;
		cout << (*it)->m_age << endl;
	}



}


int main()
{
	//	test02();
	test03();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walkerrev_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值