当前位置:网站首页>Lanqiao cup provincial training camp - commonly used STL
Lanqiao cup provincial training camp - commonly used STL
2022-07-24 11:09:00 【practical_ sharp】
The dynamic array

using namespace std;
#include<vector>
vector<T>vec;//C++ Method of directly constructing a dynamic array in

Storing custom types with dynamic arrays
struct Student {
string name; // name
int age; // Age
};
int main() {
vector<Student> class1; // class
Student stu1, stu2; // Student 1, Student 2
stu1.name = "xiaohong";
stu1.age = 12;
stu2.name = "yuhaoran";
stu2.age = 25;
class1.push_back(stu1);
class1.push_back(stu2);
return 0;
}
Constructor of dynamic array
int n = 10;
// The default initial value is 0
// Define an initial value as 0 The length of is n Dynamic array of tys
vector<int>tys(n);
// Define an initial value as 1 The length of is n Dynamic array of vec
vector<int> vec(n, 1);
The above code shows how to create a length of n The initial values of are all 1 Dynamic array of .
Two bit dynamic array

int n = 5;
vector<vector<int> > vec2;
for (int i = 0; i < n; i++) {
vector<int> x(n, 1); // One dimensional assignment is required
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;
}
Construct a n That's ok m Dynamic array of columns
//vector<vector<int> >vec(n,vector<int>(m,0));
int main(){
int n= 5,m = 5;// Definition 5 That's ok 5 Column dynamic array Printout
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;
}
The use of two-dimensional dynamic arrays
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int> > v2d;// Define a two-dimensional dynamic array
for (int i = 0; i < 5; i++) {
v2d.push_back(vector<int>());// Initialize 2D array Every dimension is empty 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)); // The first j That's ok push go in j Number
}
}
for (int i = 0; i < v2d.size(); i++) {
// Printout
for (int j = 0; j < v2d[i].size(); j++) {
cout << i + 1 << " * " << j + 1 << " = " << v2d[i][j] << "\t";
}
cout << endl;
}
return 0;
}
aggregate set

Construct a set
using namespace std;
#include<set>
set<int>myset;// When initializing set It's an empty set
set The main use of






#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++ in set It's traversing from small to large ,set It will sort automatically
The default data type has size rules set It can sort automatically
For user-defined data types , You need to use operator overloading
struct Node {
int x, y;
// An important knowledge point Built in comparison function
bool operator<(const Node &rhs) const {
if (x != rhs.x)
return x < rhs.x;
else
return x < rhs.x;
}
};
The mapping table map
map It means key To value The mapping relation of
key The set of is called the set of keys ,value The set of is called the set of values
Construct a mapping

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




stay C++ in map The traversal of is also from small to large according to the key

map Use
#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;
}
// Traverse map The element pointed to by the iterator here is a pair, Yes first and second Two member variables , Each represents a mapping
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;
}
difficulty : A two-dimensional map
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// Define a two-dimensional map "> >" There must be a space between
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 */
Print sawtooth matrix

#include<iostream>
#include<vector>
using namespace std;
int main(){
int n,m;cin>>n>>m;
vector<vector<int> >a(n,vector<int>());// Initialize a program with n Dynamic array of rows
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 there is no element in a line Then output a blank line
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;
}
Garlic gentleman solved the case

#include<iostream>
#include<set>
using namespace std;
typedef struct person{
// Define a structure Store everyone's height weight Age
int x,y,z;
// Operator overloading set It is automatically sorted Structure must define a sort operator
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;// Define a person A collection of structures
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);// Insert into collection
}
for(int i=1;i<=m;i++){
person b;
cin>>b.x>>b.y>>b.z; // Judge b Is it gathering city in
if(city.count(b)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
Garlic King's collection

#include<iostream>
#include<map>
using namespace std;
int main(){
map<string, int> T;// Establish a mapping relationship between book title and quantity
int n;cin>>n;string b;
for(int i=1;i<=n;i++){
cin>>b;T[b]++;
}
cout<<T.size()<<endl;
// Pay attention to traversing map The technique of it != T.end()
for(map<string, int>::iterator it = T.begin();it != T.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
边栏推荐
- 【C】 Recursive and non recursive writing of binary tree traversal
- 【Golang】golang中time类型的before方法
- 蓝牙技术的发展与历程
- 1184. 公交站间的距离 : 简单模拟题
- [interview: Basics 01: integer binary search]
- [FPGA]: IP core ibert
- [interview: Basics 04: insert order]
- 【Golang】golang实现urlencode urldecode函数
- 【Golang】golang中map元素的删除和清空
- Redismission inventory deduction demo
猜你喜欢
![[attack and defense world web] difficulty five-star 15 point advanced question: ics-07](/img/97/555a76be9e96629fd7379ce8612a3b.png)
[attack and defense world web] difficulty five-star 15 point advanced question: ics-07

Zero basic learning canoe panel (10) -- checkbox

1184. 公交站间的距离 : 简单模拟题

E2PROM read / write (xiicps) on PS side of zcu102 board

CSDN blog removes the uploaded image watermark

【白帽子讲Web安全】第二章 浏览器安全

自学软件测试天赋异禀——不是盖的

STM32+ESP8266+MQTT协议连接阿里云物联网平台

变频器的工作原理和功能应用

Docker builds MySQL master-slave replication
随机推荐
如何给自己的网站接入在线客服系统代码
聊聊软件测试-自动化测试框架
MySQL engine
"Low power Bluetooth module" master-slave integrated Bluetooth sniffer - help smart door lock
openresty lua-resty-logger-socket日志传输
Logic of automatic reasoning 06 -- predicate calculus
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
Collation of important MySQL configuration parameters
Druid encryption command
Analysis of Lagrange multiplier method and its duality
js树形结构,根据里层id找出它所属的每层父级集合
【Golang】golang中map元素的删除和清空
pip更新命令
Reprint: getting started with cache coherence
【Golang】golang中time类型的before方法
蓝牙技术的发展与历程
Talk about new congestion control
【直播报名】Location Cache 模块浅析及 OCP 监控、报警详解
The bean injected through @autowired can still be injected even if the class is not annotated with annotations such as @comment
[interview: Basics 05: quick sort]