C语言天梯赛储备函数

来源:互联网 发布:成都工业设计软件培训 编辑:程序博客网 时间:2024/06/05 06:40
C与C++:char *strchr(const char* _Str,char _Val)
char *strchr(char* _Str,char _Ch)
头文件:#include <string.h>
功能:查找字符串_Str中首次出现字符_Val的位置
说明:返回首次出现_Val的位置的指针,返回的地址是被查找字符串指针开始的第一个与Val相同字符的指针,如果Str中不存在Val则返回NULL。
返回值:成功则返回要查找字符第一次出现的位置,失败返回NULL
memcpy;void *memcpy(void *dest, const void *src, size_t n);c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
C语言:#include<string.h>
C++:#include<cstring>
1. 函数名: atof
功 能: 把字符串转换成浮点数
名字来源:ascii to floating point numbers 的缩写
用 法: double atof(const char *nptr);
#include <stdlib.h>或者#include<cstdlib>
atoi (表示 ascii to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中。
istream& getline ( istream &is , string &str , char delim );
istream& getline ( istream& , string& );
is 进行读入操作的输入流
str 存储读入的内容
delim 终结符

cin.getline()
此函数是按行读取,其语法为:cin.getline(字符指针,字符个数N,结束符);
功能是:一次读取多个字符(包括空白字符),直到读满N-1个,或者遇到指定的结束符为止(默认的是以'\n'结束)。

 定义函数 int sscanf (const char *str,const char * format,........);
功能:从一个字符串中读进与指定格式相符的数据。
百度百科里的内容很全面:百度百科sscanf
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdlib.h>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     int i;  
  9.     unsigned int j;  
  10.     char input[] = "aaaaaaaa bbbbbbbb";  
  11.     char s[25];  
  12.     sscanf(input, "%s", s);  
  13.     printf("%s",s);  
  14.     system("PAUSE");  
  15.     return 0;  
  16. }  

char *strtok(char s[], const char *delim);
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
例如:strtok("abc,def,ghi",","),最后可以分割成为abc def ghi.尤其在点分十进制的IP中提取应用较多
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。

返回值:
从s开头开始的一个个被分割的串。当没有被分割的串时则返回字符串首地址。
所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

示例代码:
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdlib.h>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     char input[16] = "abc,d,efg";  
  9.     char *p;  
  10.     p = strtok(input, ",");  
  11.     if (p)  
  12.     {  
  13.         printf("%s\n", p);  
  14.     }  
  15.     p = strtok(NULL, ",");  
  16.     if (p)  
  17.     {  
  18.         printf("%s\n",p);  
  19.     }  
  20.     p = strtok(NULL, ",");  
  21.     if (p)  
  22.     {  
  23.         printf("%s\n", p);  
  24.     }  
  25.     system("PAUSE");  
  26.     return 0;  
  27. }  
结果:


double atof(const char *nptr);
功 能: 把字符串转换成浮点数
[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdlib.h>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     float f;  
  9.     char *str = "12345.67";  
  10.     f = atof(str);  
  11.     printf("string = %s float = %f\n", str, f);  
  12.     system("PAUSE");  
  13.     return 0;  
  14. }  
结果:
而很多题只有用字符数组表示大数才能拿满分,而且一定记得格式控制如05d 02d等等,而且一定要把数组搞大一点,用恰好的不行,并且注意long与int

min_element()和max_element

头文件:#include<algorithm>

作用:返回容器中最小值和最大值。max_element(first,end,cmp);其中cmp为可选择参数!

 

闲言少叙,上代码,一看就懂:

 

[cpp] view plain copy
print?
  1. #include<iostream>  
  2. #include<algorithm>  
  3. using namespace std;  
  4.   
  5. bool cmp(int a,int b)  
  6. {  
  7.     return a<b;  
  8. }  
  9. int main()  
  10. {  
  11.     int num[]={2,3,1,6,4,5};  
  12.     cout<<"最小值是 "<<*min_element(num,num+6)<<endl;  
  13.     cout<<"最大值是 "<<*max_element(num,num+6)<<endl;  
  14.     cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;  
  15.     cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;  
  16.     return 0;   
  17. }  


 


0 0