小知识点❤

数字π的表示    const double pi=acos(-1.0);
priority_queue <data_type> priority_queue_name;
如:priority_queue q;//默认是大顶堆
上边这个是排的最大值

priority_queue<int, vector, greater > q;
这个才是最小值得在栈顶
注意 q.empty()如果队列为空 返回true 反之返回false
去重操作
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<vector>
using namespace std;
vector<int>a;
int main()
{
    
    
	int n; cin >> n;
	for (int i = 0; i < n; i++)
	{
    
    
		int t; cin >> t;
		a.push_back(t);
	}
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	for (int i = 0; i < a.size()&&i<=500; i++)
	{
    
    
		cout << a[i] << " ";
	}

}

4.map!

map<string,int>s;就像是数组一样,a[i]=j,在map中i是string j是int
s[]=j 表示这个串有j个重复的
未完........

最好是从0开始的数组这样不出错否则就算你里边没有和x相等的,他照样等于1;
一个有序数组num【6】,你想判断x=7在这个数组里出现了几次

int pos1=lower_bound(num,num+6,7)-num;  
  返回数组中第一个大于或等于被查数的值的下标 
	int pos2=upper_bound(num,num+6,7)-num;    
	返回数组中第一个大于被查数的值的下标、
	pos3=lower_bound(num,num+6,7)
	pos4=upper_bound(num,num+6,7)
	所以 次数=pos4-pos3;

floor() 函数
头文件是<math.h>
向下取整,floor(x)获得不大于x参数且最靠近参数x的整数,x参数可以是整数(正负)和浮点数(正负)。
①求最大公约数的:辗转相除法
int gcd(int a, int b)
{
    
    
	return b == 0 ? a : gcd(b, a % b);
}
②最小公倍数
最小公倍数 = 两数的乘积 / 最大公约数
int gcd(int a, int b)
{
    
    
	return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    
    
	int m, n; int t;
	while (cin >> m >> n)
	{
    
    
		t=gcd(m,n);
		cout << endl;
		cout << "最小公倍数:" << m * n / t << endl;
	}
}



8.c++课本上的

int a;cin>>dec>>a;cout<<a<<endl;那个dec加不加都一样,因为都是默认十进制的
cin>>hex>>a;输入十六进制的数
cin>>oct>>a;输入八进制
cout<<hex<<a;按照十六进制输出a 
cout<<oct<<a;按八进制输出;
加头文件<iomanip>
setw()的应用;
cout<<setw(10)<<12<<endl;
结果就是:
        12//前面八个空格。意思就是输出的数和空格一共十个空
cout<<setw(10)<<setfill('?')<<12;
????????12//空格变成setfill里面的字符
cout<<setw(10)<<setfill('?')<<setiosflags(ios::right)<<12;
12????????  

1.
if (b & 1) 这个是判断奇偶数的
如果b是奇数就运行 如果是偶数就不运行
2.
b>>=1;是b=b/2;
b<<=1;b=b*2;
3
<< 左移:末尾补 0,原数乘 2

比如十进制数 10,在末位补 0 等于 100,相当于原数乘 10
所以 x << 1 就是将二进制的 x 左移一位,
比如 4 的二进制为 100,末尾补 0 等于 1000 即十进制数的 8.

>> 右移:高位补 0,原数除以 2

比如十进制数的 8 二进制表示为 1000,x >> 1  0100
即十进制数的 4 y = x << 1,x 不发生改变,只是将移位后的值赋给了 y。

<<= 左移并赋值,>>= 右移并赋值:

对变量进行移位运算后将得到的结果再赋给原来的变量值,
比如 x <<= 1 就是对 x 左移 1 位后将结果再赋给 x
**& 按位与**
**| 按位或**
**^ 按位异或**
**~ 取反** 
set 的那些begin啥的前面都得+*
*s.rbegin();代表set最后一个元素
一个数组a[0--n-1],下面这个就是输出它的max
cout<<*max_element(a,a+n);
min也同理
标准库的string有一个substr函数用来截取子字符串。

一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),
第二个是截取的长度。
vector
1.v.insert(v.begin(),c); 输入c ,但是最前面的!
2.v.pop_back(); 尾部 pop
set
1.  s.find(b) == s.end() 在set中如果没有b的话 就相等
2. s.count(A);若set中有A,返回1 else 返回0

14.数字原理

在这里插入图片描述

15.++ iterator用法

C++ iterator用法

16. sort函数的复习使用!

包括重写cmp

#include <iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct mouse
{
    
    
    string color;
    int weight;
};
int cmp(mouse a,mouse b)
{
    
    
    return a.weight<b.weight;
}
int main()
{
    
    
    mouse m[1000];
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
    
    
        cin>>m[i].weight>>m[i].color;
    }
    sort(m,m+n,cmp);
     for(int i=0;i<n;i++)
        cout<<m[i].color<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_45988242/article/details/105824845