蓝桥杯省赛训练营——常用STL

动态数组

在这里插入图片描述

using namespace std;
#include<vector>
vector<T>vec;//C++中直接构造一个动态数组的方法

在这里插入图片描述

用动态数组存储自定义类型

struct Student {
    string name;  // 名字
    int age;      // 年龄
};
int main() {
    vector<Student> class1;  // 班级
    Student stu1, stu2;      // 学生1,学生2
    stu1.name = "xiaohong";
    stu1.age = 12;
    stu2.name = "yuhaoran";
    stu2.age = 25;
    class1.push_back(stu1);
    class1.push_back(stu2);
    return 0;
}

动态数组的构造函数

int n = 10;
//默认的初始值为0
//定义一个初始值为0 的长度为n的动态数组tys
vector<int>tys(n);
//定义一个初始值为1 的长度为n的动态数组vec
vector<int> vec(n, 1);

上面这段代码说明了创建一个长度为n的初始值全为1的动态数组。

二位动态数组

在这里插入图片描述

int n = 5;
vector<vector<int> > vec2;
for (int i = 0; i < n; i++) {
    vector<int> x(n, 1); //必须一维一维的赋值
    vec2.push_back(x);
}
for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec2[i].size(); i++) {
        cout << vec2[i][j] << " ";
    }
    cout << endl;
}

构造一个n行m列的动态数组


//vector<vector<int> >vec(n,vector<int>(m,0));
int main(){
	int n= 5,m = 5;//定义5行5列动态数组 打印输出
	vector<vector<int> >vec(n,vector<int>(m,0));
	for(int i=0;i<vec.size();i++){
		for(int j=0;j<vec[0].size();j++)
			cout<<vec[i][j]<<" ";
		cout<<endl;
	}
	return 0;
}

二维动态数组的使用

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<vector<int> > v2d;//定义一个二维动态数组
    for (int i = 0; i < 5; i++) {
        v2d.push_back(vector<int>());//初始化二维数组 每一维都是一个空的vector<int>()
    }
    for (int i = 0; i < v2d.size(); i++) {
        for (int j = 0; j <= i; j++) {
            v2d[i].push_back((i + 1) * (j + 1));  //第j行push进去j个数
        }
    }
    for (int i = 0; i < v2d.size(); i++) { //打印输出
        for (int j = 0; j < v2d[i].size(); j++) {
            cout << i + 1 << " * " << j + 1 << " = " << v2d[i][j] << "\t";
        }
        cout << endl;
    }
    return 0;
}

集合set

在这里插入图片描述

构造一个集合

using namespace std;
#include<set>
set<int>myset;//初始化的时候 set是一个空集合

set的主要用法

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述

#include <iostream>
#include <string>
#include <set>
using namespace std;
int main() {
    set<string> country;
    country.insert("China");
    country.insert("America");
    country.insert("France");
    set<string>::iterator it;
    for (it = country.begin(); it != country.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    country.erase("America");
    country.erase("England");
    if (country.count("China")) {
        cout << "China in country." << endl;
    }
    country.clear();
    return 0;
}

C++中set是从小到大遍历的,set会自动排序

默认数据类型是有大小规则的 set可以自动排序

对于用户自定义的数据类型,就需要用到运算符重载

struct Node {
    int x, y;
    //比较重要的一个知识点 内置比较函数
    bool operator<(const Node &rhs) const {
        if (x != rhs.x) 
            return x < rhs.x;
         else 
            return x < rhs.x;
    }
};

映射表map

map表示的是key到value的映射关系

key的集合称为键的集合,value的集合称为值的集合

构造一个映射

在这里插入图片描述

#include<map>
using namespace std;
map<stirng,int>types;

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在C++中map的遍历也是按照键的从小到大遍历的

在这里插入图片描述

map的使用

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
    map<string, int> dict;
    dict["Tom"] = 1; // or write as dict.insert(make_pair("Tom",1));
    dict["Jone"] = 2;
    dict["Mary"] = 1;
    if (dict.count("Mary")) {
        cout << "Mary is in class " << dict["Mary"];
        dict["Mary"] = 5;
    }
    //遍历map 这里迭代器指向的元素是一个pair,有first和second两个成员变量,分别代表一个映射
    for (map<string, int>::iterator it = dict.begin(); it != dict.end(); it++) {
        cout << it->first << " is in class " << it->second << endl;
    }
    dict.clear();
    return 0;
}

难点:二维map

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
	//定义一个二维map "> >"之间一定要有一个空格
    map<int, map<string, int> > info;
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        int class_id;  string name;
        cin >> class_id >> name;
        info[class_id][name]++;
    }
    for (map<int, map<string, int> >::iterator it1 = info.begin(); it1 != info.end(); it1++) {
        for (map<string, int>::iterator it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
            cout << "There are " << it2->second << " people named " << it2->first << " in class " << it1->first << endl;
        }
    }
    return 0;
}

/*
6                                                                             
1 liu                                                                         
2 bai                                                                         
2 liu                                                                         
3 yan                                                                         
4 yan                                                                         
1 bai                                                                         
There are 1 people named bai in class 1                                       
There are 1 people named liu in class 1                                       
There are 1 people named bai in class 2                                       
There are 1 people named liu in class 2                                       
There are 1 people named yan in class 3                                       
There are 1 people named yan in class 4       
*/

打印锯齿矩阵

在这里插入图片描述

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

int main(){
	int n,m;cin>>n>>m;
	vector<vector<int> >a(n,vector<int>());//初始化一个具有n行的动态数组
	int x,y;
	for(int i=1;i<=m;i++){
		cin>>x>>y;
		a[x-1].push_back(y);
	}
	for(int i=0;i<a.size();i++){
		//如果某行没有任何元素 则输出一个空行
		if(a[i].size() == 0){
			cout<<endl;
			continue;
		}
		for(int j=0;j<a[i].size()-1;j++) cout<<a[i][j]<<" ";
		cout<<a[i][a[i].size()-1]<<endl;
	}
	return 0;
}

蒜头君破案

在这里插入图片描述

#include<iostream>
#include<set>
using namespace std;

typedef struct person{  //定义一个结构体 存放每个人的身高 体重 年龄
	int x,y,z;
	//运算符重载 set里面自动排序 结构体必须定义一个排序算子
	bool operator<(const person &p) const{
		if(x != p.x) return x < p.x;
		else if(y != p.y) return y < p.y;
		else return z < p.z;
	}
}person;

int main(){
	set<person>city;//定义一个person结构体的集合
	int n,m;cin>>n>>m;
	int x,y,z;
	for(int i=1;i<=n;i++){
		person a;cin>>a.x>>a.y>>a.z;
		city.insert(a);//插入到集合中
	}
	for(int i=1;i<=m;i++){
		person b;
		cin>>b.x>>b.y>>b.z; //判断b是否在集合city中
		if(city.count(b)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

蒜头君的藏书

在这里插入图片描述

#include<iostream>
#include<map>
using namespace std;

int main(){
	map<string, int> T;//建立一个书名到数量的映射关系
	int n;cin>>n;string b;
	for(int i=1;i<=n;i++){
		cin>>b;T[b]++;
	}
	cout<<T.size()<<endl;
	//注意遍历map的技巧 it != T.end()
	for(map<string, int>::iterator it = T.begin();it != T.end();it++){
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}
发布了97 篇原创文章 · 获赞 101 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/practical_sharp/article/details/104734638