C++ quick start tutorial

/*

*@author: LeeG

*@date: 2020-12-10

*/

1. The difference between C language and C++

© Process-oriented language : Process-oriented programming is to analyze the steps to solve the problem, and then implement these steps step by step, and call them one by one when using them.
(C++) Object-oriented language : Object-oriented programming is to decompose the problem into various objects. The purpose of creating objects is not to complete a step, but to describe the behavior of something in the entire problem-solving step.

2. Introduction to C++ header files and common header files

#include <iostream> 
using namespace std;
int main(){
	//please write your code!
	return 0;
}
#include <iostream>
#include <cmath>		//数学公式 例如绝对值abs()、幂函数pow()...
#include <string>		//C++字符串
#include <string.h>		//C语言库,对应C++是cstring
#include <algorithm>	//常用函数,max()、min()、abs()、swap()、sort()...
#include <bits/stdc++.h> //万能头文件
using namespace std;
int main(){
    //please write your code!
    return 0;
}

For more header files and C++ standard template library STL, see the link below:

3. C++ input and output

#include <iostream> 
using namespace std;
int main(){
	int a;
	char b;
	float c;
	cin>>a>>b>>c;
	cout<<"1:"<<a<<b<<c<<endl;
	
	string str;
	cin>>str;
	cout<<"2:"<<str<<endl;
	
	getline(cin, str);
	cout<<"3:"<<str<<endl;
	
	//思考1 上述的cin>>str和getline(cin, str); 有什么区别? 
	//思考2 如果要输入 19:05  怎么输入?
	//思考3 如果题目要求输入一串数字,以回车结束输入,怎么输入?		 
	return 0;
}

Thinking 3 is very important, often encountered when writing questions! ! !

Thinking 3 summary, see the article:

4. Code format specification

Why is this emphasized so many times? When you write code and there is a bug, clean, uncluttered code can help you find it quickly. It will also be much more psychologically comfortable. Not only that, but when you review your code in the future or when others read your code, improve the readability~

#include <bits/stdc++.h>
#include <vector>
using namespace std;

struct Graph{
	int edge;
	int weight;
	Graph(int e, int w){
		edge = e;
		weight = w;
	}
};

int main(){
	int n, m, sx, sy;
	cin>>n>>m>>sx>>sy;
	int cs[n];		//记录救援队数目 
	int judge[n];	//记录该城市是否达到过
	int dotlist[n];	//记录权值 
	int pre[n];		//记录行走路径 
	int path[n];	//记录最短路径条数
	memset(path, 0, sizeof(path)); 
	memset(pre, 0, sizeof(pre));
	memset(dotlist, 0, sizeof(dotlist));
	memset(judge, 0, sizeof(judge));
	int res[n];
	fill(res, res+n, 501); 
	for(int i = 0; i < n; i++){
		cin>>cs[i];
	}
	vector<Graph> s[n];
	for(int i = 0; i < m; i++){
		int a, b, c;
		cin>>a>>b>>c;
		s[a].push_back(Graph(b, c));
		s[b].push_back(Graph(a, c));
	}
	res[sx] = 0;
	dotlist[sx] = cs[sx];
	path[sx] = 1;
	int flag = 0;			//记录目前城市的下标 
	for(int i = 0; i < n; i++){
		int min = 502;
		for(int j = 0; j < n; j++){
			if(!judge[j] && res[j] < min){
				min = res[j];
				flag = j;
			}
		}
		int num = res[flag];
		judge[flag] = 1;
		for(int j = 0; j < s[flag].size(); j++){
			int e = s[flag][j].edge;
			int w = s[flag][j].weight;
			if(!judge[e] && num + w < res[e]){
				res[e] = num + w;
				dotlist[e] = dotlist[flag] + cs[e];
				pre[e] = flag;
				path[e] = path[flag];
			}else if(num + w == res[e]){
				path[e] += path[flag];
				if(dotlist[flag] + cs[e] > dotlist[e]){
					dotlist[e] = dotlist[flag] + cs[e];
					pre[e] = flag;
				}
			}
		}
	}
	memset(res, 0, sizeof(res));
	int r = sy, k = 0;
	while(r != sx){
		res[k++] = pre[r];
		r = pre[r];
	}
	cout<<path[sy]<<' '<<dotlist[sy]<<endl;
	for(int i = k-1; i >= 0; i--){
		cout<<res[i]<<' ';
	}
	cout<<sy;
	return 0;
} 

5. Commonly used summary in the process of writing C++ questions

  1. Use of functions memset() and fill()

  2. Character and number conversion stringstream

  3. Common string operations length(), substr(), etc.

  4. The use of sorting function sort() and (temporarily unacceptable) structure sorting

    /*
    小李老师因为有事情要忙,交给小王一个任务,这个任务就是帮他把几个学生排好序
    
    排序规则按照年级降序排序,如果年级相同则按照姓名升序排序,如果姓名相同则按照年龄降序排序。
    
    输入
    第一行给出学生总人数N,
    接下来N行给出每个学生信息,格式:年级 姓名 年龄
    
    输出
    按照排序规则输出学生信息
    
    输入样例 1 
    
    2
    2 wps 20
    2 lsn 8
    
    输出样例 1
    lsn 8
    wps 20
    */
    
  5. The pits that are easy to encounter in the process of solving problems!

Guess you like

Origin blog.csdn.net/weixin_44723496/article/details/110959163
Recommended