笔记嘻嘻

最近在刷题啦啦啦,发现了一个好(划掉)网站: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访问。

 
c++数组:
要从数组a复 制k个元素到数组b,可以这样做:memcpy(b,a,sizeof(int)*k)。
当然,如果数组a和b 都是浮点型的,复制时要写成“memcpy(b,a,sizeof(double)*k)”。
另外需要注意的是, 使用memcpy函数要包含头文件string.h。
如果需要把数组a全部复制到数组b中,可以写得简单 一些:memcpy(b,a,sizeof(a))。
memset(a,0,sizeof(a))”的作用是把数组a清零,它也在string.h中定义。
 
C++字符串
在C语言中,字符串其实就是字符数组——可以像处理普通数组一样 处理字符串,只需要注意输入输出和字符串函数的使用。
为了方便书写,C语言 允许用直接的方法表示字符,例如,“a”代表的就是a的ASCII码。不过,有一些字符直接表 示出来并不方便,例如,回车符是“\n”,而空字符是“\0”,它也是C语言中字符串的结束标 志。其他例子包括“\\”(注意必须有两个反斜线)、“\'”(这个是单引号),甚至还有的字符 有两种写法:“\"”和“"”都表示双引号。
 
sprintf和strchr:strchr的作用是在一个字符串中查找单个字符。
sprintf:之前用过printf和fprintf。printf输出到屏幕,fprintf输出到文件,而sprintf输出到字符串。多数情况下,屏幕总是可以输出的,文件一般也能写(除非磁盘满或者硬件损坏),但字符串就不一定了:应该保证写入的字符串有足够的空间。
多大才算足够大呢?字符个数加1,因为C语言的字符串是以空字符“\0”结尾的。
strlen(s)返回的就是结束标记之前的字符个数。
posted @ 2019-01-25 11:27  芩溪儿  阅读(219)  评论(2编辑  收藏  举报