尽量使用标准库函数

尽量使用标准库函数,不要“发明”已经存在的库函数。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #define ARRAY_SIZE 10
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6
 7 using namespace std;
 8
 9 //利用类模板生成实例
10 typedef vector < int > IntArray;
11
12 //显示数组
13 void put_array(int x[],int size) {
14     for(int i=0;i<size;i++)
15         cout<<x[i]<<" ";
16 }
17
18 //显示vector容器中的元素
19 void put_vector(IntArray v)
20 {
21     IntArray::iterator theIterator;
22
23     for (theIterator=v.begin();theIterator!=v.end();++theIterator){
24         cout<<(*theIterator)<<" ";
25     }
26 }
27
28 //在main()函数中测试find()_end()算法
29 int main(int argc, char** argv) {
30 {
31 //--------------------------------------------
32 //    find_end()算法对普通数组的处理
33 //---------------------------------------------
34     int x[ARRAY_SIZE]={1,3,5,7,9,2,4,6,8,10};
35     cout << "x[]: ";
36     put_array(x,ARRAY_SIZE);
37     cout<<endl;
38     int y[]={5,7,9};
39     cout << "y[]: ";
40     put_array(y,3);
41     cout<<endl;
42
43     // find_end()算法查找,并显示查找结果
44     int *p=find_end(x,x+ARRAY_SIZE,&y[0],&y[2]);
45     if (p != x + ARRAY_SIZE)  {  //查到
46         cout << "The first element that matches :" ;
47         put_array(y,3);
48         cout<< " is at location in x" << p - x<< endl;
49     }
50     else  {           //未查到
51          cout << "The sequence does not contain any elements";
52          cout<< " with value " ;
53          put_array(&x[3],3);
54     }
55
56 //--------------------------------------------
57 //    find_end()算法对vector容器的处理
58 //---------------------------------------------
59    //声明intvector容器对象
60     IntArray intvector;
61
62     //向intvector容器中插入元素
63     for (int i=1; i<=10; i++) {
64         intvector.push_back(i);
65     };
66
67     //显示intvector容器中的元素值
68     cout << "intvector: ";
69     put_vector(intvector);
70     cout<<endl;
71
72     IntArray temp;
73     temp.push_back(5);
74     temp.push_back(6);
75     temp.push_back(7);
76     cout << "temp: ";
77     put_vector(temp);
78     cout<<endl;
79
80     // find_end()算法查找,并显示查找结果
81     IntArray::iterator pos;
82     pos=find_end(intvector.begin(),intvector.end(),temp.begin(),temp.end());
83
84     if (pos != intvector.end())  {  //查到
85         cout << "The first element that matches ";
86         put_vector(temp);
87         cout<< " is at location in intvector " <<pos - intvector.begin()<< endl;
88     }
89     else  {           //未查到
90          cout << "The sequence does not contain any elements";
91          cout<< " with value ";
92         put_vector(temp);
93         cout<< endl ;
94     }
95         return 0;
96 }
97 }

原文地址:https://www.cnblogs.com/borter/p/9418301.html

时间: 2024-08-01 16:24:44

尽量使用标准库函数的相关文章

C语言中最常用标准库函数

标准头文件包括: <asset.h>      <ctype.h>       <errno.h>       <float.h> <limits.h>      <locale.h>       <math.h>        <setjmp.h> <signal.h>     <stdarg.h>      <stddef.h>      <stdlib.h>

C/C++语言的标准库函数malloc/free与运算符new/delete的区别

概括地说 1.malloc与free是C++/C的标准库函数,new/delete是C++的运算符,它们都可用于申请动态内存和释放内存. 2.对于非内部数据类型的对象而言,只用malloc/free无法满足动态对象的要求.对象在创建的同时,要自动执行构造函数,对象在消亡之前要自动执行析构函数.而由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加于malloc/free. 3.因此C++语言需要一个能完成动态内存分配和初始化工作的运算符

范围for语句 &amp;&amp; 列表初始值&amp;&amp; 标准库函数begin和end

范围for语句: 引入的意义:简化传统for的编写,主要用于遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for( 声明: 给定序列) { 执行的操作. } 其中,“给定序列”部分是一个对象,如一个string的对象.“声明”部分用于定义一个变量,该变量将被用于访问序列中的基础元素.每次迭代,“声明”部分的变量会被初始化为“给定序列”部分的下一个元素值. 一个范围for循环的简单例子: 1 string str(“some string”); 2 for(auto c:

C语言提供了几个标准库函数 itoa() atoi()

C语言提供了几个标准库函数C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio.h> # include <stdlib.h> void main (void) { int num = 100; char str[25]; itoa(num, str, 10); printf("The number 'num' is %d and the strin

文件I/O之C标准库函数和系统库函数区别

1.首先C标准库函数是工作在系统库函数之上的.C标准库函数在读写文件时候都有一个文件流指针.FILE*fp=NULL;// fp=fopen(F_PATH,"r"); fp文件流指针,其指向结构体如下图所示. 文件描述符指向磁盘文件,在进行文件读写操作时候是先读写到缓冲区,然后再调用系统应用层API write函数进行写操作,write将文件内容写到内核缓冲区,然后再调用内核层API sys_write进行写操作.到这样可以减少I/O操作,提高读写操作. 值得说明的是:使用C语言标准库

标准库函数atoi的实现

标准库函数atoi用于将字符串类型的数据转换为整形数据:在转换过程中要考虑空指针.空字符串"".正负号,溢出等情况 这里是将字符串str转换为32位整型,其正数的最值为0x7FFFFFFF,负数的最小值为0x80000000(可参考有符号类型的最小负数的补码的由来),通过这两个值来判断是上溢还是下溢,此外,用一个全局变量来表明是否是非法输入的问题. 代码如下:(vs2010调试正确) 1 //实现atoi_32函数 2 //正数数的最大值为0x7FFFFFFF,最小值为0x800000

C++/C语言的标准库函数与运算符的区别new/delete malloc/free

malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.对于非内部数据类型的对象而言,光用malloc/free无法满足动态对象的要求. 对象在创建的同时要自动执行构造函数,对象消亡之前要自动执行析构函数.由于malloc/free是库函数而不是运算符,不在编译器控制权限之内,不能把执行构造函数

c++11 标准库函数 std::move 和 完美转发 std::forward

c++11 标准库函数 std::move 和 完美转发 std::forward #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <map> // C++中还有一个被广泛认同的说法,那就是可以取地址的.有名字的就是左值,反之,不能取地址的.没有名字的就是右值. // 相对于左值,右值表示字面常量.表达式.函数的非

Python标准库函数之time的使用

Python标准库函数之time的使用 信息来自CSDN博主「西瓜很甜*」的原创文章,如有疑问请参照原文:原文链接:https://blog.csdn.net/weixin_44690846/article/details/88987989 !!!使用time库函数前需导入time包!!! 1 import time 功能 time.asctime([t])接受时间元组.通过函数 gmtime() 或 localtime() 返回的时间值,返回一个可读的形式为’Sun Jun 20 23:21: