笔记嘻嘻
最近在刷题啦啦啦,发现了一个好(划掉)网站:www.cplusplus.com/reference
虽然网站比较简陋,也不知道是不是正版API但是很好用就是啦(看网站名字好像是哈哈哈)
全英文的看着不习惯嘤嘤嘤,不过培养一下习惯也不错嘻嘻(反正是假期)
(上次记得笔记因为没有保存都没了哭唧唧,从现在开始记吧。。)
C++ queue:
Member functions (constructor) Construct queue (public member function ) empty Test whether container is empty (public member function ) size Return size (public member function ) front Access next element (public member function ) back Access last element (public member function ) push Insert element (public member function ) emplace Construct and insert element (public member function ) pop Remove next element (public member function ) swap Swap contents (public member function )
注意!!划重点!!访问队尾不是rear(虽然一直这么叫),是back,是back,是back!
C++ stack:
Member functions (constructor) Construct stack (public member function ) empty Test whether container is empty (public member function ) size Return size (public member function ) top Access next element (public member function ) push Insert element (public member function ) emplace Construct and insert element (public member function ) pop Remove top element (public member function ) swap Swap contents (public member function )
C++ scanf
scanf("%d%d%d",&a,&b,&c);
注意,scanf读string的情况
scanf遇到空格就会结束,所以读不到带空格的字符串
使用gets()可以解决,gets会把空格读进来,遇到回车符才会结束
网上有人说gets()不让用了,查了下
/* gets example */ #include <stdio.h> int main() { char string [256]; printf ("Insert your full address: "); gets (string); // warning: unsafe (see fgets instead) printf ("Your address is: %s\n",string); return 0; }
确实警告了,自己试验了可以运行,不过可能是我的编译器太老了。。
看下fgets()
1 char str[100]; 2 fgets(str,100,stdin);//stdin键盘输入 3 fputs(str,stdout);//输出
(官方api涉及到文件的输入,看起来比较复杂不好理解,就不贴了。)
C++ printf
直接看例子
int main(){ printf("%f\n",8.0/5.0);//1.600000 printf("%.1f\n",8/5);//1.6 printf("%.2f\n",8/5);//1.60 printf("%.0f\n",8.0/5.0);//2 printf("%d\n",8.0/5.0);//-1717986918 非常奇怪的现象 int a=8.0/5.0; printf("%d\n",a);//1 return 0; }
C++ sin函数
/* sin example */ #include <stdio.h> /* printf */ #include <math.h> /* sin */ #define PI 3.14159265 int main () { double param, result; param = 30.0; result = sin (param*PI/180); printf ("The sine of %f degrees is %f.\n", param, result ); return 0; } Edit & Run
由例子看出,sin(包括cos和tan),传入的参数都是弧度,不是角度。
C++ 数据范围
C/C++:在32位或64位机器中,int占4个字节,即32位。
int能表示的最大正整数为:0111 1111 1111 1111 1111 1111 1111 1111 (有符号数)对应的10进制数为231-1=2147483647,对应的十六进制表示为:0x7FFFFFFF。
同理,最小负整数为:1000 0000 0000 0000 0000 0000 0000 0000 (补码),对应的原码(补码的补码)也是1000 0000 0000 0000 0000 0000 0000 0000,对应的十六进制表示为0x80000000,而C/C++规定该值为-231=-2147483648。
所以int类型(整数类型)的范围为-2^31 ~ 2^31-1,即-2147483648~2147483647,十六进制表示:0x80000000~0x7FFFFFFF。
关于浮点数,懒得整理,觉得一片博客写的很好:https://blog.csdn.net/black_kyatu/article/details/79257346
方便求最大值最小值的库函数
#include<limits.h> #include<iostream> using namespace std; int main(){ cout<<INT_MAX<<endl<<INT_MIN<<endl<<LONG_MIN<<endl<<LONG_MAX<<endl; cout<<"-----------------------"<<endl; cout<<LLONG_MIN<<endl<<LLONG_MAX<<endl; return 0; }
C++ pair
贴一个总结的不错的帖子:https://www.jb51.net/article/117837.htm
类模板:template<class T1,class T2> struct pair
参数:T1是第一个值得数据类型,T2是第二个值的数据类型。
功能:pair将一对值组合成一个值,
这一对值可以具有不同的数据类型(T1和T2),
两个值可以分别用pair的两个公有函数first和second访问。