C语言天梯赛储备函数

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
  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. }  
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int i;
	unsigned int j;
	char input[] = "aaaaaaaa bbbbbbbb";
	char s[25];
	sscanf(input, "%s", s);
	printf("%s",s);
	system("PAUSE");
	return 0;
}

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中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。

示例代码:
  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. }  
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	char input[16] = "abc,d,efg";
	char *p;
	p = strtok(input, ",");
	if (p)
	{
		printf("%s\n", p);
	}
	p = strtok(NULL, ",");
	if (p)
	{
		printf("%s\n",p);
	}
	p = strtok(NULL, ",");
	if (p)
	{
		printf("%s\n", p);
	}
	system("PAUSE");
	return 0;
}
结果:


double atof(const char *nptr);
功 能: 把字符串转换成浮点数
  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. }  
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	float f;
	char *str = "12345.67";
	f = atof(str);
	printf("string = %s float = %f\n", str, f);
	system("PAUSE");
	return 0;
}
结果:
而很多题只有用字符数组表示大数才能拿满分,而且一定记得格式控制如05d 02d等等,而且一定要把数组搞大一点,用恰好的不行,并且注意long与int

min_element()和max_element

头文件:#include<algorithm>

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

 

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

 

  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. }  
#include<iostream>
#include<algorithm>
using namespace std;

bool cmp(int a,int b)
{
	return a<b;
}
int main()
{
	int num[]={2,3,1,6,4,5};
	cout<<"最小值是 "<<*min_element(num,num+6)<<endl;
	cout<<"最大值是 "<<*max_element(num,num+6)<<endl;
	cout<<"最小值是 "<<*min_element(num,num+6,cmp)<<endl;
	cout<<"最大值是 "<<*max_element(num,num+6,cmp)<<endl;
	return 0; 
}


 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值