当前位置:网站首页>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;
}
边栏推荐
- Redis cluster setup
- 自学软件测试天赋异禀——不是盖的
- E2PROM read / write (xiicps) on PS side of zcu102 board
- 对话ACE第四期:分布式数据库未来发展的挑战和机遇
- 【Golang】golang实现发送微信服务号模板消息
- Zero basic learning canoe panel (6) -- switch/indicator
- Dialogue ace phase IV: challenges and opportunities for the future development of distributed databases
- Take care of me when I meet you for the first time
- js树形结构,根据里层id找出它所属的每层父级集合
- Data visualization - White Snake 2: black snake robbery (1)
猜你喜欢

「低功耗蓝牙模块」主从一体 蓝牙嗅探-助力智能门锁

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

只会“点点点”,凭什么让开发看得起你?

Working principle and function application of frequency converter

性能测试总结(一)---基础理论篇

RS485 communication OSI model network layer

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

Selenium automated test (this one is enough) - self study

Siemens 200smart self created library and description

selenium3自动化测试(这一篇就够了)——自学篇
随机推荐
【直播报名】Location Cache 模块浅析及 OCP 监控、报警详解
CSDN blog removes the uploaded image watermark
Detailed explanation of the implementation process of redistribution watchdog
Logic of automatic reasoning 06 -- predicate calculus
【Golang】golang中map元素的删除和清空
[golang] golang实现截取字符串函数SubStr
In idea, system.getproperty ("user.dir") identifies the path of the module: the setting of the working directory
MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
Installing MySQL under Linux
乘势而上,OceanBase推动数字支付精益增长
Build resume editor based on Nocode
[interview: Basics 03: selection sort]
Read the triode easily. It turns out that it works like this
Research on parameter setting of MATLAB FFT
Collation of important MySQL configuration parameters
Value and technical thinking of vectorization engine for HTAP
在idea中System.getProperty(“user.dir“)识别到模块(module)路径的方法:Working directory的设置
Detailed explanation and example demonstration of Modbus RTU communication protocol
web咸鱼自救攻略--typescript的类没有你想象中的那么难
08【AIO编程】