C++语言基础 例程 范型编程简介

简介: 贺老师的教学链接  本课讲解曾经的查找//曾经的查找:顺序查找#include <iostream>using namespace std;int main( ){ int d[10]={2,7,4,8,12,1,3,5,9,11},i,key,index=-1; cout<<"Input a key you want to search:\

贺老师的教学链接  本课讲解


曾经的查找

//曾经的查找:顺序查找
#include <iostream>
using namespace std;
int main( )
{
    int d[10]={2,7,4,8,12,1,3,5,9,11},i,key,index=-1;
    cout<<"Input a key you want to search:\n";
    cin>>key;
    for(i=0; i<10; i++)
        if(key == d[i])
        {
            index = i;
            break;
        }
    if(index >= 0)
        cout<<"The index of the key is "<<index<<".\n";
    else
        cout<<"Not found.\n";
    return 0;
}

//曾经的查找:有序表中的二分查找
#include <iostream>
using namespace std;
int main()
{
    int d[10] = {1,3,9,12,32,41,45,62,75,77};
    int low, high,mid,key,index=-1;
    cin>>key;
    low=0,high=9;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(d[mid]==key)
        {
            index=mid;
            break;
        }
        else if(d[mid]>key)
            high=mid-1;
        else
            low=mid+1;
    }
    if(index >= 0)
        cout<<"在:"<<index<<".\n";
    else
        cout<<"没有找到.\n";
}

可以这样做
//可以这样做:数组上的find函数
#include <iostream>
#include <algorithm>
using namespace std;
#define SIZE 100
int iarray[SIZE];
int main()
{
    iarray[20] = 50;
    int* ip = find(iarray, iarray + SIZE, 50);
    if (ip == iarray + SIZE)
        cout << "50 not found in array" << endl;
    else
        cout << *ip << " found in array" << endl;
    return 0;
}

//可以这样做:利用向量容器、迭代器、算法
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> intVector(100);
int main()
{
    intVector[20] = 50;
    vector<int>::iterator intIter =
        find(intVector.begin(), intVector.end(), 50);
    if (intIter != intVector.end())
        cout << "Vector contains value " << *intIter << endl;
    else
        cout << "Vector does not contain 50" << endl;
    return 0;
}


目录
相关文章
|
23天前
|
安全 算法 C++
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits处理弱枚举和强枚举
【C/C++ 泛型编程 应用篇】C++ 如何通过Type traits处理弱枚举和强枚举
46 3
|
25天前
|
安全 算法 编译器
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
【C++ 泛型编程 进阶篇】深入探究C++模板参数推导:从基础到高级
240 3
|
25天前
|
算法 编译器 数据库
【C++ 泛型编程 高级篇】使用SFINAE和if constexpr灵活处理类型进行条件编译
【C++ 泛型编程 高级篇】使用SFINAE和if constexpr灵活处理类型进行条件编译
243 0
|
25天前
|
设计模式 程序员 C++
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
【C++ 泛型编程 高级篇】C++模板元编程:使用模板特化 灵活提取嵌套类型与多容器兼容性
241 2
|
3天前
|
编译器 C++
C++编程之美:探索初始化之源、静态之恒、友情之桥与匿名之韵
C++编程之美:探索初始化之源、静态之恒、友情之桥与匿名之韵
16 0
|
3天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
12天前
|
程序员 C++
C++语言模板学习应用案例
C++模板实现通用代码,以适应多种数据类型。示例展示了一个计算两数之和的模板函数`add&lt;T&gt;`,可处理整数和浮点数。在`main`函数中,展示了对`add`模板的调用,分别计算整数和浮点数的和,输出结果。
11 2
|
24天前
|
存储 移动开发 安全
【C/C++ 口语】C++ 编程常见接口发音一览(不断更新)
【C/C++ 口语】C++ 编程常见接口发音一览(不断更新)
22 0
|
24天前
|
算法 编译器 C++
【C++ 模板编程 基础知识】C++ 模板类部分特例化的参数顺序
【C++ 模板编程 基础知识】C++ 模板类部分特例化的参数顺序
21 0
|
存储 算法 程序员
【C/C++ 线性表 简介】C/C++中的线性表探索:从标准库到自定义实现
【C/C++ 线性表 简介】C/C++中的线性表探索:从标准库到自定义实现
39 0