![9160aa8e35cf7be132333ae26edb7299.png](https://img-blog.csdnimg.cn/img_convert/9160aa8e35cf7be132333ae26edb7299.png)
标准库函数
C++标准函数库为C++程序员们提供了一个可扩展的基础性框架。 程序员从中可以获得极大的便利,同时也可以通过继承现有类,自己编制符合接口规范的容器、算法、迭代子等方式对之进行扩展。它大致包含了如下几个组件:
1:C标准函数库,基本保持了与原有C语言程序库的良好兼容,尽管有些微变化。那就是在C++标准库中存在两套C的函数库,一套是带有.h扩展名的(比如<stdio.h>),而另一套则没有(比如<cstdio>)。这个也是本节学习的重点。
2:语言支持(language support)部分,包含了一些标准类型的定义以及其他特性的定义,这些内容,被用于标准库的其他地方或是具体的应用程序中。
3:诊断(diagnostics)部分,提供了用于程序诊断和报错的功能,包含了异常处理(exception handling),断言(assertions),错误代码(error number codes)三种方式。
4:通用工具(general utilities)部分,这部分内容为C++标准库的其他部分提供支持,当然也可以在自己的程序中调用相应功能。比如:动态内存管理工具,日期/时间处理工具。
5:字符串(string)部分,用来代表和处理文本。它提供了足够丰富的功能。事实上,文本是一个string对象,它可以被看作是一个字符序列,字符类型可能是char,或者wchar_t等等。string可以被转换成char*类型,这样便可以和以前所写的C/C++代码和平共处了。因为那时侯除了char*,没有别的。
6:容器(containers)部分,STL的一个重要组成部分,涵盖了许多数据结构。
7:算法(algorithms)部分,STL的一个重要组成部分,包含了大约70个通用算法,用于操控各种容器,同时也可以操控内建数组。
8:迭代器(iterators)部分,STL的一个重要组成部分,如果没有迭代器的撮合,容器和算法便无法结合的如此完美。事实上,每个容器都有自己的迭代器,只有容器自己才知道如何访问自己的元素。它有点像指针,算法通过迭代器来定位和操控容器中的元素。
9:输入/输出(input/output)部分,就是经过模板化了的原有标准库中的iostream部分,它提供了对C++程序输入输出的基本支持。
C标准函数库包含了丰富的函数:
![54da346310b1ced45f8ca83dade5b723.png](https://img-blog.csdnimg.cn/img_convert/54da346310b1ced45f8ca83dade5b723.png)
C语言标准库中的各个函数,类型以及宏分别在以下标准头文件中:
assert.h float.h math.h stdarg.h stdlib.h ctype.h limits.h setjmp.h stddef.h string.h errno.h locale.h signal.h stdio.h time.h
头文件声明格式为:
#include <头文件名>
输入输出函数:
标准I/O函数中包含了如下几个常用的函数:scanf、printf、getchar、putchar、gets、puts等。
int scanf(const char *format, arg_list)
scanf主要从标准输入流中获取参数值,format为指定的参数格式及参数类型
int printf(const char *format, arg_list)
printf主要是将格式化字符串输出到标准输出流中。
输入输出函数:
int getchar(void)
getchar主要是从标准输入流读取一个字符。
int putchar(int ch)
putchar(ch)主要是把字符ch写到标准流stdout中去。
char * gets(char *str)
gets主要是从标准输入流读取字符串并回显,读到换行符时退出,并会将换行符省去。
int puts(char *str)
puts主要是把字符串str写到标准流stdout中去,并会在输出到最后时添加一个换行符。
在代码栏输入以下代码,编译查看输出结果,对比和scanf和printf有什么不同:
#include <stdio.h>
void main()
{
char str[100] = {};
gets(str);
puts(str);
}
数学函数:
头文件<math.h>中说明了数学函数和宏。
![eb482a44e7e6a06e3bf4f483af07ad7a.png](https://img-blog.csdnimg.cn/img_convert/eb482a44e7e6a06e3bf4f483af07ad7a.png)
数学函数:
![6a53aea6362edffccbd898999bf4999b.png](https://img-blog.csdnimg.cn/img_convert/6a53aea6362edffccbd898999bf4999b.png)
在代码栏输入以下代码,编译查看输出结果,思考高中的几何知识:
#include <iostream>
using namespace std;
#include <math.h>
void main()
{
float a = 3,b = 4,c;
c = sqrt(pow(a,2)+pow(b,2));
cout<<"斜边是"<<c<<endl;
system("pause");
}
数值转换函数:array to float
atof函数用于把字符串s转换成double类型,该函数等价于strtod。
![c82a061238f9d7cb5c360413122a4ebf.png](https://img-blog.csdnimg.cn/img_convert/c82a061238f9d7cb5c360413122a4ebf.png)
atoi函数用于把字符串s转换成int类型,该函数等价于strtol。
![64134c5f62bd7a8e935d1e2386759e6f.png](https://img-blog.csdnimg.cn/img_convert/64134c5f62bd7a8e935d1e2386759e6f.png)
数值转换函数:
atol函数用于把字符串s转换成long类型,该函数等价于strtol。
![b405e572b789aa59c64b8bd346b03698.png](https://img-blog.csdnimg.cn/img_convert/b405e572b789aa59c64b8bd346b03698.png)
strtod函数用于把字符串s的前缀转换成double类型。
![5ebd46b0e56d2645fb4c55ddc78b0daf.png](https://img-blog.csdnimg.cn/img_convert/5ebd46b0e56d2645fb4c55ddc78b0daf.png)
数值转换函数:
strtol函数用于把字符串的前缀转换成long类型。
![209b883dc1674ab4f43b9846b0c27013.png](https://img-blog.csdnimg.cn/img_convert/209b883dc1674ab4f43b9846b0c27013.png)
strtoul函数的功能与strtol函数相同,只是结果为unsigned long类型。
![c971f923166a312d73232ee017dc58ec.png](https://img-blog.csdnimg.cn/img_convert/c971f923166a312d73232ee017dc58ec.png)
数值转换函数:
在代码栏输入以下代码,编译查看输出结果:
#include <iostream>
using namespace std;
void main()
{
int iValue = 125;
char str[100] = {};
itoa(iValue ,str,10);
cout<<str<<endl;
system("pause");
}
内存操作函数 memory copy move
在C++语言中提供了很多对内存操作的函数
![3d07e47100bfadb52f989fe5be1236ae.png](https://img-blog.csdnimg.cn/img_convert/3d07e47100bfadb52f989fe5be1236ae.png)
memcpy函数用于把字符串src中的count个字符拷贝到dest中,并返回dest。
![9906c1b42b279ca51f62b5e1cff82a73.png](https://img-blog.csdnimg.cn/img_convert/9906c1b42b279ca51f62b5e1cff82a73.png)
memmove函数的功能与memcpy相似,不同之处是当发生对象重叠时,该函数仍能正确执行。
内存操作函数 compare比较
![7a2fa014b8232c369b201b4d8eb83c71.png](https://img-blog.csdnimg.cn/img_convert/7a2fa014b8232c369b201b4d8eb83c71.png)
memcmp函数用于把buf1的前count个字符与buf2相比较。
![a6b261bb0c34f63728bb95350875e7f8.png](https://img-blog.csdnimg.cn/img_convert/a6b261bb0c34f63728bb95350875e7f8.png)
memset函数用于把dest中的前count个字符替换为c,并返回dest。
内存操作函数
在代码栏输入以下代码,编译查看输出结果:
#include <iostream>
using namespace std;
void main()
{
char str1[100] = {};
char str2[100] = "helloworld";
memcpy(str1,str2,5);
cout<<str1<<endl;
system("pause");
}
字符串操作函数
在头文件<string.h>中定义了两组字符串函数。第一组函数的名字以str开头;第二组函数的名字以mem开头。
strcpy函数用于把字符串strSource(包括'0')拷贝到字符串strDestination中,并返回strDestination。
![7f393e4df425ba1f33a1c7ade89a3478.png](https://img-blog.csdnimg.cn/img_convert/7f393e4df425ba1f33a1c7ade89a3478.png)
strncpy函数用于把字符串strSource中最多count个字符拷贝到字符串strDest中,并返回strDest。
![c89757d9b4bf16167cd17272efe33b09.png](https://img-blog.csdnimg.cn/img_convert/c89757d9b4bf16167cd17272efe33b09.png)
字符串操作函数 catenate 链接
strcat函数用于把字符串strSource连结到strDestination的尾部,并返回strDestination。
![ed0f3a54fe52fa98d7b371df5826b508.png](https://img-blog.csdnimg.cn/img_convert/ed0f3a54fe52fa98d7b371df5826b508.png)
strncat函数用于把字符串strSource中最多count个字符连接到字符串strDest的尾部,并以'0'结束,返回strDest。
![b879b8b3a5781e0b87bbd5de1b7f7c8d.png](https://img-blog.csdnimg.cn/img_convert/b879b8b3a5781e0b87bbd5de1b7f7c8d.png)
字符串操作函数
strcmp函数用于比较字符串string1和string2; 当string1<string2时它返回一个负值;当string1>string2时它返回一个正数;当string1== string2时它返回0。
![6a2c123d0125d3c1426256c081867e33.png](https://img-blog.csdnimg.cn/img_convert/6a2c123d0125d3c1426256c081867e33.png)
strncmp函数用于将字符串string1中最多count个字符与字符串string2相比较。当string1<string2时它返回一个负数;当string1>string2时它返回一个正数;当string1==string2时它返回0。
![31a0991dd7e8c1952dbec003ed474d9d.png](https://img-blog.csdnimg.cn/img_convert/31a0991dd7e8c1952dbec003ed474d9d.png)
字符串操作函数
strrchr函数用于返回一个指向字符串string中字符c最后一次出现的位置的指针。如果string中不包含c,那么函数返回NULL。
![2b0e2296d6fb353a7e9183471b18bd65.png](https://img-blog.csdnimg.cn/img_convert/2b0e2296d6fb353a7e9183471b18bd65.png)
strspn函数用于返回字符串string中,由字符串strCharSet中的字符构成的第一个子字符串的长度。
![0cb0605ca4e16bc464ce7fd3a0b3a3a8.png](https://img-blog.csdnimg.cn/img_convert/0cb0605ca4e16bc464ce7fd3a0b3a3a8.png)
strcspn函数用于返回字符串string中由不在字符串strCharSet中的字符组成的第一个字符串的长度。
![15be1ec3aa0652dc66efd6d1cdaa3cf7.png](https://img-blog.csdnimg.cn/img_convert/15be1ec3aa0652dc66efd6d1cdaa3cf7.png)
strpbrk函数用于返回指向字符串strCharSet中的任意字符第一次出现在字符串string中的位置的指针。
![e287286a7911edee30dbc9f9f654ddbc.png](https://img-blog.csdnimg.cn/img_convert/e287286a7911edee30dbc9f9f654ddbc.png)
strstr函数用于返回指向字符串strSearch第一次出现在字符串string中的位置的指针。
![d90281c7ec7ea3248a2213110621c9f0.png](https://img-blog.csdnimg.cn/img_convert/d90281c7ec7ea3248a2213110621c9f0.png)
strlen函数用于返回字符串string的长度。
![63a3868df35d5ea822dfa0fddb375071.png](https://img-blog.csdnimg.cn/img_convert/63a3868df35d5ea822dfa0fddb375071.png)
在代码栏输入以下代码,编译查看输出结果:
#include <iostream>
using namespace std;
void main()
{
char str1[100] = {};
char str2[100] = "helloworld";
strcpy(str1,str2);
if(0==strcmp(str1,str2))
cout<<"str1==str2"<<endl;
system("pause");
}
函数的递归
直接或间接调用自己的函数被称为递归函数。这种调用方式被称之为函数的递归调用.
#include <iostream>
using namespace std;
void putValues(int _iValue)
{
if( _iValue < 10 )
{
cout<< _iValue++<<endl;
putValues(_iValue);
}else
{
return;
}
}
void main()
{
putValues(4);
system(“pause”);
}
递归函数必须定义一个停止条件,否则函数会永远递归下去,这被称作无限递归错误。
在putValues的情况下 停止条件是_iValue大于等于10,下面是个无限递归的例子:
#include <iostream>
using namespace std;
void putValues(int _iValue)
{
cout<< _iValue++<<endl;
putValues(_iValue);
}
int main()
{
putValues(4);
return 0;
}
注意:如果使用函数的递归调用,那么一定注意正确给出递归的退出条件
间接调用自己的递归函数如下使用:
#include <iostream>
using namespace std;
void putValues(int _iValue);
void tempFun(int _iValue)
{
putValues(_iValue);
}
void putValues(int _iValue)
{
if( _iValue < 10 )
{
cout<< _iValue++ <<endl;
tempFun( _iValue );
}
else
return ;
}
void main()
{
putValues(4);
system("pause");
}
本章介绍了标准库函数的使用,为我们将来编写的程序提供了很大的方便。
还介绍了关于函数的递归调用,熟练掌握两种函数递归的调用形式,并能够正确给定函数的递归出口。
学习资料领取:
https://pan.baidu.com/s/1VX_zb_eqWH_coos1ZB9QQw
提取码:w8st
学习从来都不是一个人的事,如果你感觉小编的这篇文章对你有帮助的话,可以关注小编首页和小编来一起学习C/C++语言,小编会持续更新后续的内容哦!!