C与C++:char *strchr(const char* _Str,char _Val)
char *strchr(char* _Str,char _Ch)
头文件:#include <string.h>
功能:查找
字符串_Str中首次出现字符_Val的位置
返回值:成功则返回要查找字符第一次出现的位置,失败返回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
char *strtok(char s[], const char *delim);
cin.getline()
此函数是按行读取,其语法为:cin.getline(字符指针,字符个数N,结束符);
功能是:一次读取多个字符(包括空白字符),直到读满N-1个,或者遇到指定的结束符为止(默认的是以'\n'结束)。
定义函数 int sscanf (const char *str,const char * format,........);
功能:从一个字符串中读进与指定格式相符的数据。
百度百科里的内容很全面: 百度百科sscanf
- #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;
- }
#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中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。
示例代码:
- #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;
- }
#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);
功 能: 把字符串转换成浮点数
double atof(const char *nptr);
功 能: 把字符串转换成浮点数
- #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;
- }
#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为可选择参数!
闲言少叙,上代码,一看就懂:
- #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;
- }
#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;
}