当前位置:网站首页>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;
}
边栏推荐
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程
- Siemens 200smart self created library and description
- Four components and working principle of frequency converter
- MySQL query field matches the record of a rule
- MySQL paging
- Data visualization - White Snake 2: black snake robbery (1)
- 爬虫与反爬:一场无休止之战
- 【白帽子讲Web安全】第一章 我的安全世界观
- read_csv 报错:‘gbk‘ codec can‘t decode byte 0xb4 in position 274: illegal multibyte sequence
- Build resume editor based on Nocode
猜你喜欢

Download path of twincat3 versions

Zynq TTC usage

数据可视化-《白蛇2:青蛇劫起》(1)

Zero basic learning canoe panel (7) -- file selection (pathdiaglog)

Analysis of Lagrange multiplier method and its duality
![[FPGA]: use of MicroBlaze](/img/f4/5114bf4bde10adaa22c7441350575c.png)
[FPGA]: use of MicroBlaze

TwinCAT3各版本下载路径

Publish local images to Alibaba cloud

No one knows what ingredients tiktok's latest popular product pink sauce contains

High speed ADC test experience
随机推荐
自学软件测试天赋异禀——不是盖的
[interview: Basics 02: bubble sort]
CSDN blog removes the uploaded image watermark
变频器的工作原理和功能应用
Installing MySQL under Linux
【Golang】golang实现sha256加密函数
selenium3自动化测试(这一篇就够了)——自学篇
08【AIO编程】
Collation of important MySQL configuration parameters
自动推理的逻辑06--谓词演算
向量化引擎对HTAP的价值与技术思考
Introduction to kubernetes Basics
[interview: Basics 04: insert order]
Simply understand MODBUS function code and partition
Data visualization - White Snake 2: black snake robbery (1)
E2PROM read / write (xiicps) on PS side of zcu102 board
Cub school learning - Kernel Development
Zero basic learning canoe panel (8) -- hex/text editor
[FPGA]: IP core DDS
LDR6028充电OTG直播线直播声卡音频转接器最具性价比方案