【C++】学习笔记[十六]

STL【一】

1、STL的诞生以及相关概念

💖诞生:

  • 长久以来,软件界一直希望建立一种可重复利用的东西
  • C++的面向对象泛型编程思想,目的就是复用性的提升
  • 大多数情况下,数据结构和算法都未能有一套标准,导致被迫从事大量重复工作
  • 为了建立数据结构和算法的一套标准,就有了STL

💖基本概念:

  • STL从广义上分为:容器、算法和迭代器
  • 容器和算法通过迭代器进行无缝衔接
  • STL几乎所有的代码都采用了模板类或者模板函数

💖STL六大组件:

  • 容器:各种数据结构,如vector、list、deque、map等,用来存放数据
  • 算法:各种常用算法
  • 迭代器:扮演了容器和算法之间的胶合剂
  • 仿函数:行为类似函数,可作为算法的某种策略
  • 适配器:一种用来修饰容器或者仿函数或者迭代器接口的东西
  • 空间配置器:负责空间的配置与管理

💖容器可以分为:

  • 序列式容器:强调值的排序,序列式容器中的每个元素均有固定的位置
  • 关联式容器:二叉树结构,各元素之间没有严格的物理上的顺序关系

💖算法分为:

  • 质变算法:在运算过程中会更改区间内的元素的内容
  • 非质变算法:在运算过程中不会更改区间内的元素内容

💖迭代器种类:

  • 输入迭代器、输出迭代器、前向迭代器、双向迭代器、随机访问迭代器
  • 常用的迭代器种类为双向迭代器和随机访问迭代器。

2、vector

vector的基本概念

功能:

  • vector数据结构和数组非常相似,也称为单端数组

vector与普通数组的区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

  • 动态扩展并不是在原空间之后延续新空间,而是找更大的内存空间,然后将原数组拷贝到新空间,再释放掉原来的空间。
  • vector容器的迭代器是支持随机访问的迭代器

vector中存放内置数据类型练习:

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

//vector容器存放内置数据类型

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

void test01()
{
	//创建vector容器
	vector<int> v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	//通过迭代器访问容器中的数据
	vector<int>::iterator itBegin = v.begin();//开始迭代器,指向容器中第一个元素的位置
	vector<int>::iterator itEnd = v.end();//结束迭代器,指向容器中最后一个元素的下个位置

	//第一种遍历方式
	while (itBegin != itEnd)
	{
		cout << *itBegin << endl;
		itBegin++;
	}

	cout << "****************************" << endl;
	//第二种遍历方式
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	cout << "****************************" << endl;

	//第三种遍历方式,利用STL中的遍历算法
	for_each(v.begin(), v.end(), myPrint);
}

int main()
{
	test01();
	return 0;
}

vector中存放自定义数据类型练习:

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

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

	string m_name;
	int m_age;
	
};

void test01()
{
	vector<Person> v;
	Person p1("aaa", 12);
	Person p2("bbb", 22);
	Person p3("ccc", 32);
	Person p4("ddd", 42);
	Person p5("eee", 52);

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

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

void test02()
{
	vector<Person*> v;
	Person p1("aaa", 12);
	Person p2("bbb", 22);
	Person p3("ccc", 32);
	Person p4("ddd", 42);
	Person p5("eee", 52);

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

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

int main()
{
	test01();
	cout << "***************************" << endl;
	test02();
	return 0;
}

vector容器嵌套容器练习代码:

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

//容器嵌套容器
void test01()
{
	vector<vector<int>> v;
	//创建小容器
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;

	//向小容器中添加数据
	for (int i = 0; i < 4; i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}

	//将小容器插入到大容器中
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);

	//通过大容器遍历所有的数据
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
	{
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
		{
			cout << *vit << " " << endl;
		}
		cout << endl;
	}
}

int main()
{
	test01();
	return 0;
}

vector容器的数据存取、插入删除等练习:

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

void printVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> v1;//默认构造(无参构造)
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	printVector(v1);


	//利用区间的方式进行构造
	vector<int> v2(v1.begin(), v1.end());
	printVector(v2);

	//n个elem的方式构造
	vector<int> v3(10, 100);//10个100
	printVector(v3);

	//拷贝构造
	vector<int> v4(v3);
	printVector(v4);
}

//vector的赋值操作练习
void test02()
{
	vector<int> v1;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	printVector(v1);

	//operator=
	vector<int> v2;
	v2 = v1;
	printVector(v2);

	//assign
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());//左闭右开区间
	printVector(v3);

	//n个元素赋值
	vector<int> v4;
	v4.assign(10, 100);
	printVector(v4);
}

//vector容器的容量和大小练习
void test03()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	printVector(v1);
	if (v1.empty())//为真 代表容器为空
	{
		cout << "v1 is null" << endl;
	}
	else
	{
		cout << "v1 is not null" << endl;
		cout << "v1 的容量为:" << v1.capacity() << endl;
		cout << "v1 的大小为:" << v1.size() << endl;
	}

	//重新指定大小
	//如果重新指定大小比原来的空间长,默认用0来填充
	v1.resize(15);
	//v1.resize(15, 100);//利用重载版本,可以指定默认填充值
	printVector(v1);


	//如果重新指定的大小比原来的空间小,那么超出的部分数据会被删除掉
	v1.resize(5);
	printVector(v1);
}

//vector的插入和删除练习
void test04()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1);
	}

	printVector(v1);

	//尾删
	v1.pop_back();
	printVector(v1);

	//插入第一个参数是迭代器
	v1.insert(v1.begin(), 100);
	printVector(v1);
	v1.insert(v1.begin(), 2, 1000);
	printVector(v1);

	//删除 第一个参数也是迭代器
	v1.erase(v1.begin());
	printVector(v1);

	v1.erase(v1.begin(), v1.end());//删除区间内的数据
	printVector(v1);

	//清空
	v1.clear();
	printVector(v1);
}

//vector数据存取练习
void test05()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1);
	}
	
	//利用[]访问
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1[i] << " ";
	}
	cout << endl;
	
	//at方式访问
	for (int i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";
	}
	cout << endl;

	//获取第一个元素和最后一个元素
	cout << "第一个元素:" << v1.front() << endl;
	cout << "最后一个元素:" << v1.back() << endl;
}

//vector互换容器练习
void test06()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i + 1);
	}

	cout << "交换前打印:" << endl;
	printVector(v1);

	vector<int> v2;
	for (int i = 10; i >= 1; i--)
	{
		v2.push_back(i);
	}
	printVector(v2);

	cout << "交换后打印:" << endl;
	v1.swap(v2);
	printVector(v1);
	printVector(v2);

	//实际用途:巧用swap可以收缩内存空间’
	vector<int> v;
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
	}
	cout << "容量:" << v.capacity() << endl;
	cout << "大小:" << v.size() << endl;

	v.resize(3);
	cout << "容量:" << v.capacity() << endl;
	cout << "大小:" << v.size() << endl;

	//巧用swap收缩内存
	vector<int>(v).swap(v);

	//vector<int>(v)--->匿名对象
	//匿名对象特点:当前行执行完之后系统会立马回收掉

	cout << "容量:" << v.capacity() << endl;
	cout << "大小:" << v.size() << endl;
}

//vector预留空间
void test07()
{
	vector<int> v;

	//利用reserve预留空间
	v.reserve(100000);

	int num = 0;//统计开辟空间的次数
	int* p = nullptr;

	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);

		if (p != &v[0])
		{
			p = &v[0];
			num++;
		}
	}
	cout << "num:" << num << endl;
}


int main()
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	//test06();
	test07();
	return 0;
}

3、string

stringchar* 的区别

  • char* 是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串。

练习代码:

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

//string的构造函数练习
void test01()
{
	string s1;//默认构造

	const char* str = "hello world";
	string s2(str);
	cout << "s2:" << s2 << endl;

	string s3(s2);
	cout << "s3:" << s3 << endl;

	string s4(10, 'a');
	cout << "s4:" << s4 << endl;
}


//string的赋值操作练习
void test02()
{
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello C++");
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello C++", 5);
	cout << "str5 = " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(10, 'w');
	cout << "str7 = " << str7 << endl;
}

//字符串拼接练习
void test03()
{
	string str1 = "wo";
	str1 += "ai wan youxi ";
	cout << "str1:" << str1 << endl;

	str1 += ':';
	cout << "str1 = " << str1 << endl;

	string str2 = "lol dnf";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I ";
	str3.append("love ");
	cout << "str3 = " << str3 << endl;

	str3.append("game abcd", 4);
	cout << "str3 = " << str3 << endl;

	//str3.append(str2);
	str3.append(str2,0,3);//从第0个位置开始截取3个字符
	cout << "str3 = " << str3 << endl;
}

//string的查找操作练习
void test04()
{
	string str1 = "abcdefede";
	int pos = str1.find("de");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串" << endl;
		cout << "pos = " << pos << endl;
	}

	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;

	//rfind()和find()的区别:rfind是从右往左查,find是从左往右查
}

//string的替换练习
void test05()
{
	string str1 = "abcdefg";
	str1.replace(1, 3, "1111");//从1号位置 替换三个字符为1111
	cout << "str1 = " << str1 << endl;
}

//string字符串的比较练习
void test06()
{
	string str1 = "hello";
	string str2 = "xello";
	if (str1.compare(str2) == 0)
	{
		cout << "str1==str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1>str2" << endl;
	}
	else
	{
		cout << "str1<str2" << endl;
	}

}

//string存取练习
void test07()
{
	string str = "hello";
	cout << "str = " << str << endl;

	//通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	//通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	//修改单个字符
	str[0] = 'x';
	cout << "str = " << str << endl;

	str.at(1) = 'x';
	cout << "str = " << str << endl;
}

//string的插入和删除
void test08()
{
	string str = "hello";
	//插入
	str.insert(1, "111");
	cout << "str = " << str << endl;

	//删除
	str.erase(1, 3);
	cout << "str = " << str << endl;
}

//string子串截取练习
void test09()
{
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "zhangsan@sina.com";

	//从邮件的地址中获取到用户名的信息
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username = " << username << endl;
}

int main()
{
	/*test01();
	cout << "**************************" << endl;
	test02();
	cout << "**************************" << endl;
	test03();*/

	/*test04();
	test05();*/

	//test06();
	//test07();

	//test08();
	test09();
	return 0;
}

🤦‍♀️🤦‍♀️🤦‍♀️

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值