从这篇博文起,将尝试使用C++11来写常用算法与数据结构。

本篇博文以最简单的顺序查找作为系列博文的起点,并作约定如下:

1,变量名 : varList ; 函数名 : SequentialFind ;

2,尽量描写算法本身,因而均不含模板,数据类型均为int;

3,所有代码均在同一个cpp中;

4,代码均在 vs2013 与 g++ 4.8.2 中编译通过;

5,g++编译命令:g++ -std=c++11 sequential_find.cpp 。

顺序查找没什么好说的,这里主要看看C++11的lambda,auto新语义。

时间复杂度:O(n)

空间复杂度:O(1)

show me the code !

// #if __cplusplus < 201103L
// #error "must be compiled under c++11 support platform!!!"
// #endif
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cassert>
using namespace std; int SequentialFind(const int varList[], const int size, const int target)
{
if (!varList || size < )
{
return -;
}
int index = -;
for (int i = ; i < size;i++)
{
if (varList[i] == target)
{
index = i;
break;
}
}
return index;
} void test()
{
//case counter
int testCase = ;
//find function object
auto findFunc = SequentialFind;
//show case result lambda function
auto showFunc = [&testCase](){cout << "case[" << testCase++ << "] ok... "<<endl; }; cout << "test begin : " << endl << endl; //case empty list
{
assert(- == findFunc(nullptr, , ));
showFunc();
}
//case wrong list size
{
const int testList[] = { ,,,,, , ,,,-};
assert(- == findFunc(testList, , ));
showFunc();
}
//case not found
{
const int testList[] = { , , , , , , , , , - };
const int size = sizeof(testList) / sizeof(int);
const int target = -;
assert(- == findFunc(testList, , ));
showFunc();
}
//case found at begin position
{
const int testList[] = { , , , , , , , , , - };
const int size = sizeof(testList) / sizeof(int);
const int target = ;
assert( == findFunc(testList, size, target));
showFunc();
}
//case found at random position
{
const int testList[] = { , , , , , , , , , - };
const int size = sizeof(testList) / sizeof(int);
const int target = ;
assert( == findFunc(testList, size, target));
showFunc();
}
//case found at end position
{
const int testList[] = { , , , , , , , , , - };
const int size = sizeof(testList) / sizeof(int);
const int target = -;
assert(size - == findFunc(testList, size, target));
showFunc();
} cout <<endl<< "test done ! " << endl << endl;
} int main(int argc, char* argv[])
{
test();
return ;
}

C++11写算法之顺序查找的更多相关文章

  1. C++11写算法之二分查找

    同样的,二分查找很好理解,不多做解释,要注意二分查找的list必须是排好序的. 这里实现了两种二分查找的算法,一种递归一种非递归,看看代码应该差不多是秒懂.想试验两种算法,改变一下findFunc函数 ...

  2. C语言查找算法之顺序查找、二分查找(折半查找)

    C语言查找算法之顺序查找.二分查找(折半查找),最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 顺序查找 /*顺序查找 顺序查找是在一个已知无(或有序)序队列中找出与给定关键字相同的 ...

  3. Java中的查找算法之顺序查找(Sequential Search)

    Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...

  4. javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)

    javascript数据结构与算法---检索算法(顺序查找.最大最小值.自组织查询) 一.顺序查找法 /* * 顺序查找法 * * 顺序查找法只要从列表的第一个元素开始循环,然后逐个与要查找的数据进行 ...

  5. 数据结构与算法之PHP查找算法(顺序查找)

    对于查找数据来说,最简单的方法就是从列表的第一个元素开始对列表元素逐个进行判断,直到找到了想要的结果,或者直到列表结尾也没有找到,这种方法称为顺序查找. 一.基本写法 顺序查找的实现很简单.只要从列表 ...

  6. 查找算法(顺序查找、二分法查找、二叉树查找、hash查找)

    查找功能是数据处理的一个基本功能.数据查找并不复杂,但是如何实现数据又快又好地查找呢?前人在实践中积累的一些方法,值得我们好好学些一下.我们假定查找的数据唯一存在,数组中没有重复的数据存在. (1)顺 ...

  7. C++11写算法之插入排序

      插入排序,是指将从1 –> size-1的数一个个插入到前面已经排序好的数组中. 时间复杂度:O(n^2) , O(nlgn) (lgn指使用二分查找插入点位置) 空间复杂度:O(1) // ...

  8. C++11写算法之选择排序

    选择排序,顾名思义,指从数组后面将最小的值找出来,然后与最前面(指当前位置)值进行交换. 时间复杂度:O(n^2) 空间复杂度:O(1) 此处应用了C++11的auto , lambda , stat ...

  9. C++11写算法之冒泡排序

    冒泡排序很形象,指从数组后面将更小的值慢慢浮到前面去,每遍历一趟使得最小值浮到最前面(指当前位置). 这里有点小技巧,当某一次遍历过程中发现无交换,则说明此时数组已经排序完成,可提前退出. 时间复杂度 ...

随机推荐

  1. Modify Headers模拟不同地域进行网页测试

    今天要简单讲一下Modify Headers这个Firefox插件,记录一下我是怎么使用它的. Modify Headers: https://addons.mozilla.org/zh-CN/fir ...

  2. <img>元素底部为何有空白及其解决方案

    一.为什么<img>元素底部会有空白? 要理解这个问题,首先要弄明白CSS对于 display: inline 元素的 vertical-align 各个值的含义.vertical-ali ...

  3. iOS小技巧 - 如何生成范围随机数

    生成[0, N-1]的随机数 NSUInteger r = arc4random_uniform(N); 生成[1, N]的随机数 NSUInteger r = arc4random_uniform( ...

  4. [Flutter] Creating & Updating State in a Flutter Application

    To create a Stateful widget: 1. Create a StatefulWidget 2. Create a State class SGreeting extends St ...

  5. MPTCP 源码分析(三) 子路径选择

    简述:      支持MPTCP的链路中存在多条子路径,因此在发送数据的时候需要选择最优路径来进行操作. MPTCP利用内核通知链对MPTCP中各子路径进行增加路径.删除路径.修改路径优先级的操作.M ...

  6. 通过 react-native-keyboard-aware-scroll-view 解决键盘遮盖输入框的问题

    1.安装 yarn add react-native-keyboard-aware-scroll-view 2.引入 import { KeyboardAwareScrollView } from ' ...

  7. HTML 5 中WebStorage实现数据本地存储

    webstorage 分sessionStorage和localstorage,sessionStorage是暂时保存,localStorage是永久保存. sessionStorage假设浏览器关闭 ...

  8. Unity3d-XML文件数据解析&amp;JSON数据解析

    1.XML文件数据解析:(首先须要导入XMLParser解析器,The latest released download from:http://dev.grumpyferret.com/unity/ ...

  9. iOS之基于FreeStreamer的简单音乐播放器(模仿QQ音乐)

    代码地址如下:http://www.demodashi.com/demo/11944.html 天道酬勤 前言 作为一名iOS开发者,每当使用APP的时候,总难免会情不自禁的去想想,这个怎么做的?该怎 ...

  10. (一)Redis笔记——简介 、key 、数据类型

    1.  Redis是什么.特点.优势 Redis是一个开源的使用C语言编写.开源.支持网络.可基于内存亦可持久化的日志型.高性能的Key-Value数据库,并提供多种语言的API. 它通常被称为数据结 ...