从这篇博文起,将尝试使用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. http://blog.csdn.net/szwangdf/article/details/23432783

    http://blog.csdn.net/szwangdf/article/details/23432783

  2. webservice ssl双向认证配置

    1.在tomcat中安装axis2插件 2.生成证书,用jdk自带的keytool 服务端 keytool -genkey -alias Server -dname "CN=192.168. ...

  3. Coincidence (动态规划求最长公共子序列)(王道)

    题目描述: Find a longest common subsequence of two strings. 输入: First and second line of each input case ...

  4. Java NIO 选择器(Selector)的内部实现(poll epoll)(转)

    转自:http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java ...

  5. 解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level

    解决spring boot启动报错java.lang.NoClassDefFoundError: ch/qos/logback/classic/Level 学习了:https://blog.csdn. ...

  6. java 问题汇总(总结,重点)若忘了可以看看{不定时更新}

    在用到spring框架中时,场景如下 post 请求过去,对象接收不到参数的值(解决办法:考虑到在参数上加个@RequestBody 注解即可,有些没加的是框架帮忙处理了后默认接收的是json串) h ...

  7. 持续集成之Jenkins+Gitlab简介 [一]

    转载:http://blog.csdn.net/abcdocker/article/details/53840449 持续集成概念 持续集成Continuous Integration 持续交付Con ...

  8. 悟道—位IT高管20年的职场心经(读书笔记三)

    悟道--一位IT高管20年的职场心经 第三章 世事洞明皆学问 职场就是你的大半个世界 是你一辈子也读不完的一大本书 想明确一个道理. 看明确一件事儿, 你就向成功迈进了一步. 1.1  "四 ...

  9. 使用OpenSSL生成CSR文件,并申请全球通用SSL证书

    http://www.openssl.org 上只有OpenSSL的原代码下载,为了方便Windows用户使用OpenSSL,我们特地为您准备了OpenSSL 0.9.8.a for win32的可执 ...

  10. PLSQL中scott账户登录不上,报错ORA-01017: invalid username/password; logon denied

    问题:PLSQL中scott账户登录不上,提示上述错误 解决方法: 1. cmd——>进入c目录; 2. 输入:sqlplus/nolog; 3. connect username/passwo ...