Day03 C++STL入门基础知识一

1.迭代器

1.1理解要点

  1. 迭代器是用来访问STL容器的工具,可以理解为我们所说的指针
  2. 迭代器一般多用于C++11,但后来逐渐被auto取代
  3. 这里利用set容器演示迭代器的基础使用

1.2样例展示

int main() {
	//text01();
	set<int>a = { 4,56,1314,520,233 };
	for (set<int>::iterator it = a.begin(); it != a.end(); it++) {
		cout << *it << endl;
	}
	return 0;
}

在这里插入图片描述

这里的it就是迭代器的意思,大家先了解,后面会讲到的!

2.auto

2.1理解要点

  • 利用auto可以简化遍历STL容器的代码量
  • auto可以由编译器自行判断所需要的变量类型
  • 由于auto过于香,本文后续访问、遍历、赋值会大量使用auto哦!

2.2 示例展示

2.2.1 例1
#include<stdio.h>
#include<algorithm>
#include<set>
int main() {
	//text01();
	set<int>a = { 4,56,1314,520,233 };
	for (auto it = a.begin(); it != a.end(); it++) {
		cout << *it << endl;
	}
	return 0;
}

在这里插入图片描述

2.2.2 例2

对于C++11,容器遍历还提供了一种更加简便的方式(前提是该容器封装了begin和end)

#include<stdio.h>
#include<algorithm>
#include<set>
int main() {
	set<int>arr = { 1,54,67,78,23,4,0 };
	for (auto& p : arr) {
		cout << p << endl;
	}
	return 0;
}

在这里插入图片描述

2.2.3 例3

①C++17中,还可以使用结构体绑定的功能
②对于结构化绑定的赋值顺序,一般是结构体中变量的声明顺序
③因为我用vs2019报错捏,我就用牛客上的试了试

#include<bits/stdc++.h>
using namespace std;
struct my_struct {	//	定义一个结构体,class类也行
	int my_int;
	string my_string;
	double my_double;
};
int main() {
	my_struct s;
	s.my_int = 520, s.my_string = "2023新年快乐!", s.my_double = 13.14;
	auto [a, b, c] = s;
	cout << a << "  " << b << "  " << c << endl;
	return 0;
}

在这里插入图片描述

2.2.4 例4
  • 这有什么用呢,这可有大用处,对于pair和tuple或者map的使用场景中大有用处,先拿熟悉的pair做展示叭
#include<bits/stdc++.h>
using namespace std;
int main() {
	pair<string, long long> p = make_pair("hahaha", 10);
	auto& [x, y] = p;
	cout << "x = "<< x << "   " << "y = "<< y << endl;
	x = "fighting", y = 123;
    cout << "x = "<< x << "   " << "y = "<< y << endl;
}

在这里插入图片描述

3.Lambda函数

3.1 理解要点

  1. 一般形式

[ ] ( ) —> return_type { }

  1. 符号含义
  • [ ]是捕捉列表,用于捕捉父级的变量
    • [var] 表示值传递方式捕捉变量var
    • [=] 表示值传递方式捕捉所有父作用域的变量(包括this)
    • [&var] 表示引用传递捕捉变量var
    • [&] 表示引用传递方式捕捉所有父作用域的变量(包括this)
    • [this] 表示值传递方式捕捉当前的this指针
    • [=,&a,&b] 表示引用传递的方式捕捉变量a和b,以值传递方式捕捉其他所有变量
    • [&,a,this] 表示以值传递方式捕捉变量a和this,引用传递方式捕捉其他变量
  • ( ) 是传参列表,传入的参数规则和普通函数一样,值得注意的是如果是什么都不传可以省略这个传参列表
  • ->return-type是函数的返回类型,如果无法返回或让编译器自己判断自己则可以省略
  • { }是函数体,没啥好说的

3.2 例题

1.递归方式
将lambda本身当成值来传参`

int main(){
	auto dfs = [&](auto&& self, int x) -> int {	
		return x > 0 ? x + self(self, x - 1) : 0;		
	};   
	cout << dfs(dfs, 10);
}

在这里插入图片描述

  1. 常见用处:最常见就是在sort函数内,用sort如何排序
#include<stdio.h>
#include<algorithm>
#include<string.h>
int main() {
	int arr[6] = { 2,0,34,16,98,24 };
	sort(arr, arr + 6, [](int a, int b) {
		return a > b;
		});			//	从大到小排序
	for (auto& p : arr)
		cout << p << " ";
}

在这里插入图片描述

  1. 进阶形式
#include<stdio.h>
#include<algorithm>
#include<string.h>
int main() {
	int a = 1, b = 2, c = 3, d = 4, e = 5;
	auto awa = [&, a, c](int x, int& y) -> int {
		b = 6, x = 7, y = 8;
		return a + b + c + d + e;
	};
	cout << "函数调用前:";
	cout << a << " " << b << " " << c << " " << d << " " << e << endl;
	cout << "awa(d,e)=" << awa(d, e) << endl;
	cout << "函数调用后:";
	cout << a << " " << b << " " << c << " " << d << " " << e << endl;
	return 0;
}

在这里插入图片描述

4.vector基础声明方式和访问遍历方式

vector是常用的stl容器之一,我们刚学可以理解为一个数组,可以使用下标访问,能动态开辟插入元素。
头文件为#include

4.1 基础声明方式

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	vector<int> v1;
	//直接声明一个int类型的让容器

	vector<int> v2(2);
	//构造函数声明一个长度为5的容器

	vector<int> v3(4, 3);
	//构造函数声明一个长为4,所有值为3的容器

	vector<int> v4 = { 3,4,5,6,7 };
	//或者直接初始化也可以

	int arr[4] = { 1,2,3,4 };
	vector<int> v5(arr + 1, arr + 4);
	//利用数组初始下标为arr+1到arr+4的值
	
	return 0;
}

4.2 访问/遍历方式

  1. 下标访问(最常用的方式)
int main() {
	vector<int> v1 = { 11,22,33,44,55,66 };
	cout << v1[1] << endl;
	cout << v1[3] << endl;
	cout << v1[5] << endl;
	//输出22 44 66
	return 0;
}

在这里插入图片描述

2.用for来遍历

  • 第一种
int main() {
	vector<int> v = { 3,4,5,6,7 };
	for (int i = 0; i < 4; i++) {
		cout << v[i] << "  ";
	}
	//输出3  4  5  6
	return 0;
}

在这里插入图片描述

  • 第二种
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
	vector<int> v = { 3,4,5,6,7 };
	for (vector<int>::iterator it = v.begin(); it != v.end();it++){ 
			cout << *it << "  ";
	}
	//输出3  4  5  6  7
	return 0;
}

在这里插入图片描述

  1. 在这里插入图片描述

5. ^ _ ^

今天鼠标很不给力,写到最后电池没电了,笔记本触屏版截图也不方便QAQ,今天就先写到这叭,被迫停笔了哈哈哈哈哈哈哈哈哈哈哈
二〇二三年,希望大家有诗有梦,且歌且行!对于我自己呢,希望更勇敢,更坚定,在尝试更多从未涉及的领域里突破挑战;更自信,更上进,在自己不断奋进的同时,把我们班班风带好;更乐观,更向上,敢于抵抗身边的流言蜚语,无条件相信自己;更优秀,更强大,努力学习专业知识,向大佬萌不断学习!
愿安,
愿清澈明朗,
愿平安顺遂,
祝你,祝我,祝我们,
都好在新的一年!

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卫冕711

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

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

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

打赏作者

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

抵扣说明:

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

余额充值