入门之语法

这篇博客介绍了C++编程中的一些实用技巧,包括使用freopen重定向输入输出,getchar与scanf的区别,以及lower_bound和upper_bound的二分查找应用。还涉及到排序、优先队列、结构体运算符重载和二分查找在自定义数据结构中的使用。同时,讲解了取地址运算符`&`和间接寻址运算符`*`的用法。
摘要由CSDN通过智能技术生成

比赛里一定要记住太常用了!

输入输出

 freopen("1.in","r",stdin);//从1.in读取文件

//也可以只写一句用于读入

 freopen("2.out","w",stdout);//在2.out输出文件

 freopen("1.txt","r",stdin);//从1.txt读取文件

 freopen("2.txt","w",stdout);//在2.txt输出文件

vscode wsl 必用代码

zzrh@DESKTOP-EQ98LTQ:/mnt/d/112233$ g++ test.cpp -g -o 0
zzrh@DESKTOP-EQ98LTQ:/mnt/d/112233$ ./0
zzrh@DESKTOP-EQ98LTQ:/mnt/d/112233$ ./0 <1.in  >1.out

字符串读入getchar(),scanf(”%c",&char)会读入空格和'\n'

scanf("%s",s

cin>>不会读入空格和'\n'

scanf函数在除scanf("%c",&char)之外的所有情况都不会把回车符作为输入字符在输入缓存中读取,但scanf("%c",&char)也不会读取tab、空格,而是把他们作为分隔符在输入缓存中忽略。

int main()
{
    int i,j,m,n,c=0;
    scanf("%d%d",&n,&m);
    getchar();
    for(i=0;i<=n-1;i++)
    {
        for(j=0;j<=m-1;j++)
        {
            scanf("%c",&a[i][j]);
        }
        getchar();
    }

lower_bound( begin,end,num):

从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):

从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

int y=lower_bound(a,a+10,5)-a;

 sort(b+1,b+1+n);
 b=unique(b+1,b+1+n)-b-1;
 for(int i=1;i<=n;i++) { a[i]=lower_bound(b+1,b+1+n,a[i])-b; }

vector

sort(b.begin(), b.end());
binary_search(b.begin(), b.end(), tmp) // // 对vector进行二分查找
struct Node//结构体定义以及运算符重载
{
    int x,y;
    bool operator<(const Node &rhs)const
    {
        if(x == rhs.x)
            return y < rhs.y;
        else
            return x < rhs.x;
    }
};
//二分查找函数
if(binary_search(nodes,nodes+n,tmp))
    cout << "Yes" << endl;
else
    cout << "No" << endl;
//在对结构体进行二分操作之前,肯定需要先对其排序。对结构体排序就需要用到运算符重载,对结构体中的<进行重载,排序可以直接用sort排序,或者是定义一个结构体类型的set,set会对其去重排序。然后就可以使用二分查找了。
vector <long long > a,w,v[maxn],mx1;
vector <vector<long long > >f;
f.resize(m+1);
sort(f.begin()+1,f.end());//如果习惯数组式从一开始 就这样 避免出错
!binary_search(f.begin()+1,f.end(),w)

struct 与自定义

struct Item{
	int q,p,t;
	bool operator<(const Item& a) const{
	if (t!=a.t) return t>a.t;
	return  q >a.q;
	} 
}b,r;

struct Item{
	int q,p,t;
}b,r;
bool operator<(Item a,Item b) {
if (a.t!=b.t) return a.t>b.t;
return  a.q>b.q;
} 

队列具体使用见onezero 

优先队列与堆

//小根堆priority_queue<int, vector<int>, greater<int>>s;

//greater表示按照递增(从小到大)的顺序插入元素

优先队列具体使用见。数据结构例4-1

pair

typedef pair<int,int>P;
P a[maxn];
a[++tot]=make_pair(x,y);
a[++tot]=P(x,y);  
sort(a+1,a+1+tot); 

map

if(T.find(n)!=T.end())return T[n];
if(T[n])return T[n];
T.clear();
T[n]=1;

y1 y2 j1 j2 的oj问题

#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

 #include<bits/stdc++.h>

的替代

#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <deque>
#include <queue>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
 
using namespace std;
 
#ifdef __WIN32__
    #define LLD "%I64d"
#else
    #define LLD "%lld"
#endif
 
#ifdef __WIN32__
    #define ULLD "%I64u"
#else
    #define ULLD "%llu"
#endif
 
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893
                         
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;
 

取地址运算符 &

& 是一元运算符,返回操作数的内存地址。例如,如果 var 是一个整型变量,则 &var 是它的地址。该运算符与其他一元运算符具有相同的优先级,在运算时它是从右向左顺序进行的。

您可以把 & 运算符读作"取地址运算符",这意味着,&var 读作"var 的地址"。

间接寻址运算符 *

第二个运算符是间接寻址运算符 *,它是 & 运算符的补充。* 是一元运算符,返回操作数所指定地址的变量的值。

#include <iostream>
 
using namespace std;
 
int main ()
{
   int  var;
   int  *ptr;
   int  val;

   var = 3000;

   // 获取 var 的地址
   ptr = &var;

   // 获取 ptr 的值
   val = *ptr;
   cout << "Value of var :" << var << endl;
   cout << "Value of ptr :" << ptr << endl;
   cout << "Value of val :" << val << endl;

   return 0;
}
/*
Value of var :3000
Value of ptr :0xbff64494
Value of val :3000
*/

  int x = 0;
    //声明时,变量前加 "基本类型 &" :声明引用变量。它是某个已存在变量的别名
    //即该引用变量名称与原始变量名称都代表同一个变量(地址相同)。
    int &y = x;
    //声明时,变量前加 "基本类型 *" :声明指针变量。它的值是另一个变量的地址。
    int *z;
    z = &x;

inline void merge(const V&A,const V&B,V&C)

组合数板子

void init(){
	fac[0]=1;
	for(int i=1;i<N;i++)fac[i]=fac[i-1]*i%mod;
	ifac[N-1]=Pow(fac[N-1],mod-2);
	for(int i=N-2;i>=0;i--)ifac[i]=ifac[i+1]*(i+1)%mod;
}
ll C(int n,int m){
	if(m<0||n<m)return 0;
	return fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}

分数求逆元 以及如何 将逆元转换成分数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值