C++基础 笔记

it2022-05-09  30

 

1. 输出固定位数

#include <iostream> #include <iomanip> using namespace std; int main(){ double amount = 8.0/5.0; cout<<amount<<endl; cout <<setprecision (0)<< fixed<<amount <<endl; cout<<amount+0.9<<endl; cout << setprecision (1)<<amount<<endl; cout <<setprecision (2)<<amount<<endl; system("pause"); return 0; }

fixed 表示固定输出格式。

setprecision(0) 是小数点后为0位,四舍五入。

2. 开方

#include <math.h>

cout<<sqrt(2.0)<<endl;

c++ 与 C 是兼容的 math.h 是C 中的 头文件

3.  PI

#include <math.h> #defined _USE_MATH_DEFINES ... cout<<"M_PI is "<<M_PI<<endl; const double pi = 4.0*atan(1.0); cout<<"pi is "<<setprecision(10)<<pi<<endl;

4. 输出格式

d---一种左边补0 的等宽格式,比如数字12,d出来就是: 012

//setw(n) 输出宽度 cout<<setw(5)<<999<<endl;//[space][space]999 //setfill('c') 输出宽度不足时填充 //setbase(int n) 转换为n 进制 cout<<setbase(8)<<999<<endl; //hex %X, oct %o, dec %d cout<<hex<<99<<endl;

5.用两个元素的交换法a = a+b;

b = a-b;

a= a-b;

 

6.判断是否为整数

if(floor(m+0.5)==m)

floor(m)返回 m 的整数部分,因为浮点数的运算有可能存在误差,比如1变成了0.9999...所以floor的值会变成0

7.计时函数

#include<time.h> ... printf("Time used = %.21f\n",(double)clock()/CLOCKS_PER_SEC);

 

8.cin的返回值

正常读入返回 cin对象,否则返回0

输入完毕需要enter, ctrl+z,enter

为什么还有第二个enter?

 

9. 关于文件读入和标准输出

重定向的方法:

#define LOCAL ... #ifdef LOCAL freopen("data.in","r",stdin); freopen("data.out","w",stdout); #endif

更好的是在编译选项而不是程序里定义LOCAL符号。怎么定义?

不使用重定向:

FILE *fin,*fout; fin = fopen("data.in","rb"); fout = fopen("data.out","wb"); int x; fscanf(fin,"%d",&x); fprintf(fout,"%d",x); fclose(fin); fclose(fout);

 c++:

#include<fstream> using namespace std; ifstream fin("aplusb.in"); ofstream fout("aplusb.out"); ... int a, b; fin>>a>>b; fcout<<a+b<<endl;

 

10. 整数范围

int -2^31 ~ 2^31-1  略宽 -2*10^9 ~ 2*10^9

long long  -2^63 ~ 2^63-1 略窄 -10^19 ~ 10^19

 

11.头文件

C++中保留着C语言的常用头文件。可以直接用。地道一点的话去掉扩展名.h 在前面加上c

如:stdio.h -> cstdio

12.数组

比较大的数组声明应该尽量在main 的外面 为什么?

 

#define MAXN 100+10//+10是为了保险 int a[MAXN]; int main() { ... }

数组复制:

int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[10]; memcpy(b,a,sizeof(int)*10); //10是拷贝的个数 cout<<b[4]<<endl;

//初始化数组

memset(b,0,sizeof(b));

 

13.字符数组

sprintf 输出到字符串

#include<string.h> char buf[99]; sprintf(buf,"%d%d%d%d",1,2,3,4); for(int i=0;i<strlen(buf);i++){ if(strchr(s,buf[i])==NULL) ... }

strchr 返回第一个为s的index

 

14.读入字符串

fgetc(fin) 从打开的文件fin中读取一个字符

标准输入用:getchar()

返回值是int, 应该当确定不是EOF时把它转换成char

不同操作系统的回车换行符是不一致的:

windows: \r\n

Linus:\n

MacOS \r

fgets(buf,MAXN,fin)读取完整一行放在字符数组buf中

ctype.h

isalpha(c)判断是否为字母

isdigit(c) 是否为数字

isprint(c) 是否为符号

toupper(c)返回大学形式

 

15. 结构体

struct Point{double x,y;};

struct Point a;

or

typedef struct{double x, y;}Point;

Point a;

16.判断素数,简便的方法

int is_prime(int x) { int i; //i 要从2开始,否则误判1为非素数 for(i=2;i*i<=x;i++)//只要考虑 sqrt(x)以下 if(x%i==0) return 0; return 1; }

进一步的避免重复计算i*i 和 x 传入负数

#include<assert.h> int is_prime(int x) { int i,m; assert(x>=0); if(x==1) return 0; m=floor(sqrt(x)+0.5); for(i=2;i<=m;i++) { if(x%i==0) return 0; return 1; } }

当 x<0 会异常退出,是否也是不可取的?

 

17. char[][] 和 char[]排序

int m_compare(const void* first, const void* second){ char* a = (char*)first; char* b = (char*) second; return strcmp(a,b); } int m_compare2(const void* first,const void* second){ char* a = (char*) first; char* b = (char*) second; return *a-*b; } ... char dict[MAX_IN][8], sortDict[MAX_IN][8]; ... qsort(dict,i,sizeof(dict[0]),m_compare); qsort(sortDict[j],strlen(sortDict[j]),sizeof(char),m_compare2); 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *)); 参数: 1 待排序 数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的 指针,用于确定排序的顺序   18. 队列 #include<queue> queue<int>q; q.push(1); cout<<q.front()<<endl; q.pop(); cout<<q.empty()<<endl;

19.stack

#include<stack> stack<int> s; s.push(1); s.top(); //not same with queue, is TOP s.pop(); s.empty();

20. 产生随机数

#include<stdlib.h> #include<time.h> double random(){ return (double)rand()/RAND_MAX; }//generate [0,1] int random(int m){ return (int)(random()*(m-1)+0.5) }//generate [0,m-1] //use srand(time(NULL)); int x=random(10); rand()%n //notice the n's range, RAND_MAX is >=32767

 21. quick sort

#include <iostream> using namespace std; void Qsort(int a[],int low,int high) { if(low >= high){ return; } int first = low; int last = high; int key = a[first]; while(first < last) { while(first < last && a[last]>=key) --last; a[first] = a[last]; while(first<last && a[first]<=key) ++first; a[last] = a[first]; } a[first] =key; Qsort(a,low,first -1); Qsort(a,last+1,high); } int main() { int a[]={57,68,59,52,72,28,96,33,24}; Qsort(a,0,sizeof(a)/sizeof(a[0])-1);//need minus 1 for(int i=0;i<9;i++) {cout<<a[i]<<" ";} system("pause"); return 0; }

 

 22. binary search

int lower_bound(int* A,int x, int y, int v) { int m; while(x<y) { m= x+(y-x)/2; if(A[m] >=v) y=m; else x=m+1; } return x; }

23. STL sort, lower_bound, upper_bound

int a[]={57,68,59,52,72,28,96,33,24}; sort(a,a+9); for(int i=0;i<9;i++) {cout<<a[i]<<" ";} cout<<endl<<"upper bound "<<upper_bound(a,a+9,52)-a<<endl;//return 4 cout<<endl<<"lower bound "<<lower_bound(a,a+9,52)-a<<endl; //return 3 int myints[] = {1, 2, 3, 3, 4, 6, 7}; vector<int> v(myints,myints+7); vector<int>::iterator low,up; sort (v.begin(), v.end()); low=lower_bound (v.begin(), v.end(), 5); ^ up= upper_bound (v.begin(), v.end(), 20); ^ cout << "lower_bound at position " << int(low- v.begin()) << endl; cout << "upper_bound at position " << int(up - v.begin()) << endl;

 

 

 

 

转载于:https://www.cnblogs.com/lauraxia/p/3734149.html

相关资源:C语言基础学习笔记

最新回复(0)