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就是迭代器的意思,大家先了解,后面会讲到的!
#include
#include
#include
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;
}
对于C++11,容器遍历还提供了一种更加简便的方式(前提是该容器封装了begin和end)
#include
#include
#include
int main() {
set<int>arr = { 1,54,67,78,23,4,0 };
for (auto& p : arr) {
cout << p << endl;
}
return 0;
}
①C++17中,还可以使用结构体绑定的功能
②对于结构化绑定的赋值顺序,一般是结构体中变量的声明顺序
③因为我用vs2019报错捏,我就用牛客上的试了试
#include
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;
}
#include
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;
}
[ ] ( ) —> return_type { }
1.递归方式
将lambda本身当成值来传参`
int main(){
auto dfs = [&](auto&& self, int x) -> int {
return x > 0 ? x + self(self, x - 1) : 0;
};
cout << dfs(dfs, 10);
}
#include
#include
#include
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 << " ";
}
#include
#include
#include
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;
}
vector是常用的stl容器之一,我们刚学可以理解为一个数组,可以使用下标访问,能动态开辟插入元素。
头文件为#include
#include
#include
#include
#include
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;
}
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
#include
#include
#include
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;
}
今天鼠标很不给力,写到最后电池没电了,笔记本触屏版截图也不方便QAQ,今天就先写到这叭,被迫停笔了哈哈哈哈哈哈哈哈哈哈哈
二〇二三年,希望大家有诗有梦,且歌且行!对于我自己呢,希望更勇敢,更坚定,在尝试更多从未涉及的领域里突破挑战;更自信,更上进,在自己不断奋进的同时,把我们班班风带好;更乐观,更向上,敢于抵抗身边的流言蜚语,无条件相信自己;更优秀,更强大,努力学习专业知识,向大佬萌不断学习!
愿安,
愿清澈明朗,
愿平安顺遂,
祝你,祝我,祝我们,
都好在新的一年!