Blue Bridge Cup c++ group commonly used tips

 1. Blue Bridge Cup--Mathematics--Dynamic Programming Notes

 2. Commonly used basic knowledge of C++ Blue Bridge Cup

Continuously updating --------------------------------------------

Table of contents

Common Start Templates 

Conversion between int and string

Convert digits method for numeric strings 

usage of typedefs  

Simple use of hashing

Input and output of commonly used containers

Fixed number of characters in a string

 Conversion between bases

​​​​​​


Common Start Templates 

#include<bits/stdc++.h>//万能头文件

using namespace std;//命名空间

typedef long long LL;//使用typedef关键字来定义自己习惯的数据类型名称

int main(){

	return 0;
}

Conversion between int and string

string to int:

This is the most common general int num = stoi(s) converted to int type

And long num = stoll(s); long long num = stoll(s);

The ideal conversion to floating-point type is double num = stod(s) float num = stof(s);

Note that only the letters behind sto will change according to the conversion type, which is still easy to remember.

int to string:

This is directly string num = to_string(num) simple violence hahaha


Convert digits method for numeric strings 

When a character string is similar to scientific notation, etc., the floating-point type defaults to 7 effective digits. If it is too large, it will be converted to the type of scientific notation, such as 1.2e+10. What should I do if I need to display all the digits?

You need to use setprecision(8). If you want to display as many digits as you like, you can write as many digits as you like. For example, if you want to display eight digits, fill in 8

It can be like this cout<<setprecision(8)<<b1; b1 is one of my floating point constants

At the same time, if you want to keep the exact number of decimal places, you need to do this

    cout.setf(ios::fixed);//Set the precision of the decimal point
    cout<<setprecision(2)<<b1;

Just add a statement that preserves the precision of the decimal point in front of it.

Friends, you can try it


usage of typedefs  

The blogger did some questions and found that the most commonly used typedef usage is

typedef long long ll;

What's the use? In fact, it can be easier to define constants (that is, stealing chicken hahahaha) and write less code

For example, you can

ll num;//这就是一个long long 类型的变量了

Simple use of hashing

//字符的次序问题(哈希)
//给26个字母附上顺序
//解决字符谁先谁后的问题
for (int i = 1; i <= 26; i++) { 
     char ch; 
     cin >> ch;
     a[ch - 'a'] = i;
   
 }

Input and output of commonly used containers

 The input is similar to vector with push_back();

                                set uses insert();

                                make_pair()

The vector output is the same as the array

pair output: 

for (auto it = nums.begin();it != nums.end();it++)
    {
    cout << it->first << it->second ;
    }

set output:

//正序输出 从小到大 map是自动排序的
for (set<int>::iterator it = res.begin(); it != res.end(); it++) {
        cout << *it << endl;
}
//反序输出
for(auto iter = mapStr.rbegin(); iter != mapStr.rend(); ++iter) {
	undefined
	cout<<iter->second.c_str()<<endl;
}

Fixed number of characters in a string

#include <iostream>
#include <algotirhm>
#include <string>
using namespace std;
int main()
{
    string temp = "aaabcdaaa!!!";
    int num = count(temp.begin(),temp.end(),'a');
    cout <<"在字符串" << temp << "中," <<"字母a出现的次数是" << num << endl;
    return 0 ;
}

 Conversion between bases

    //十进制转十六进制
    int num = 10;  
    char str[100];  
    itoa(num, str, 16);  
    printf("%s\n", str);  
    return 0
//十六进制转十进制
   char str[30] = "2030300 This is test";
   char *ptr;
   long ret;
   ret = strtol(str, &ptr, 10);
    //十进制转八进制 十六进制
   string s1,s2;
   int a=30;
   stringstream ss;
   ss<<oct<<a; //10进制转成八进制读入流中,再以字符串输出
   ss>>s1; //这里也可以 string s(s1.str());
   cout<<s1<<endl;  //输出:36
   ss.clear();  //不清空可能出错
   ss<<hex<<a;  //10进制转成十六进制读入流中,再以字符串输出
   ss>>s2;
   cout<<s2<<endl; //输出:1e

Guess you like

Origin blog.csdn.net/qq_56263094/article/details/123473218