1、STL算法里的find和find_if算法的使用:

区别于联系:联系:这个两个函数都是查找某个元素位置的方式;区别:其中find值适用于查找其里面元素值的位置。而find_if值使用与函数第三个参数的为判别式时的使用。

template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}

返回区间[first,end)中第一个值等于value的元素的位置。

如果没有找到匹配元素,则返回end。

复杂度:线性复杂度。最多比较次数是:元素的总个数。

程序实例:

下面的程序在int类型的vector中搜寻元素5和12,如果搜索到,就返回其位置if欧泽输出提示信息。

main.cpp(头文件algostuff.h和上一篇博客中的相同):

  1. #include "algostuff.h"
    using namespace std;
    int main()
    {
    vector<int> intVec;
    INSERT_ELEMENTS(intVec,1,9);
    vector<int>::iterator pos;
    pos = find(intVec.begin(),intVec.end(),5);
    if(pos != intVec.end())
    cout << "The value 5 exists,and its position is " <<
    distance(intVec.begin(),pos) + 1 << endl;
    else
    cout << "The value 4 not found!" << endl;
    pos = find(intVec.begin(),intVec.end(),12);
    if(pos != intVec.end())
    cout << "The value 12 exists,and its position is " <<
    distance(intVec.begin(),pos) + 1 << endl;
    else
    cout << "The value 12 not found!" << endl;
    }

运行结果(头文件algostuff.h和上一篇博客中的相同):

二、find_if()算法

template<class InputIterator, class Predicate>
InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
{
for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
return first;
}

它在区间[first,end)中搜寻使一元判断式pred为true的第一个元素。

如果没找到,返回end。

程序实例:

下面程序找出第一个能够被3整除的元素,如果找到返回其位置。

main.cpp:

#include "algostuff.h"
using namespace std;
int main()
{
vector<int> intVec;
INSERT_ELEMENTS(intVec,1,9);
vector<int>::iterator pos;
pos = find_if(intVec.begin(),intVec.end(),
not1(bind2nd(modulus<int>(),3)));
if(pos != intVec.end())
cout << "The value divided by 3 exists,and the first value's position is " <<
distance(intVec.begin(),pos) + 1 << endl;
else
cout << "The value divided by 3 not found!" << endl;
}

运行结果:

还有一个使用例子就是:

                     // add old tracked objectsfor (auto &i : old_result_vec) {auto it = std::find_if(result_vec.begin(), result_vec.end(),[&i](bbox_t const& b) { return b.track_id == i.track_id && b.obj_id == i.obj_id; });//如果是等于迭代器的end(),则表明没有找到。bool track_id_absent = (it == result_vec.end());if (track_id_absent) {//这里的-->就是大于或等于的比较符,这里是旧的帧目标也加到检测结果里if (i.frames_counter-- > 1)result_vec.push_back(i);}else {//每个box都有存储被跟踪帧计数,最多是3帧it->frames_counter = std::min((unsigned)3, i.frames_counter + 1);}}

2、新的计时方法std::chrono

其比clock计时准确跟方便。代码:

#include <chrono>
using namespace std;
using namespace chrono;auto start = system_clock::now();
// do something...
auto end   = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout <<  "花费了" << double(duration.count()) * microseconds::period::num / microseconds::period::den << "秒" << endl;
  1. 关键词 auto 看上去很高大上,它是一个“自动类型”,可以理解成“万能类型”,想成为啥,就成为啥
  2. system_clock 是 C++11 提供的一个 clock。除此之外,还有两个clock:steady_clock 和 high_resolution_clock
  3. now( ) 表示计时的那“一瞬间”
  4. duration_cast< > 表示类型转换
  5. microseconds 表示微妙。除此之外,还有五种时间单位:hours, minutes, seconds, milliseconds, nanoseconds
  6. num 和 den分别表示分子(numerator)和分母(denominator)。在我给出的代码中,num等于1, den等于1,000,000
  7. count( ) 用来返回时间
  8. 如果想深究,查看 cplusplus

另一个代码例子:

if (!cur_frame.empty()) {steady_end = std::chrono::steady_clock::now();//if (std::chrono::duration<double>(steady_end - steady_start).count() >= 1) {if (std::chrono::duration<double>(steady_end - steady_start).count() >= 1) {current_det_fps = fps_det_counter;current_cap_fps = fps_cap_counter;steady_start = steady_end;fps_det_counter = 0;fps_cap_counter = 0;}

3、其中std::unique_lock与std::lock_guard区别及多线程应用实例

C++多线程编程中通常会对共享的数据进行写保护,以防止多线程对共享数据成员进行读写时造成资源争抢导致程序出现未定义的行为(不过这个自己通过很多实验都没有重现这个资源未定义的错误)。通常的做法就是修改共享数据的的时候回进行加锁--mutex。在使用锁的时候通常是在对共享数据进行修改之前进行lock操作。在写完之后再进行unlock操作,如果由于lock之后而忘记unkock。则会导致死锁。

为此C++11中引入里std::unique_lock与std::lock_guard两种数据结构。通过对lock和unlock进行一次博的封装自动实现unlock的功能。

其中为什么需要使用锁这个概念,主要是为了让某个线程一起等待某个条件,只有当某个条件具备时,等待线程才被唤醒。其主要是为了多个线程直接进行协作。

其中锁住的方式有两种:一个是直接使用wait()来进行线程锁住。另一个是使用while跟wait()来进行锁住。其如下:

while (!pred()) wait(lck);

例子:

#include <iostream>                // std::cout
#include <thread>                // std::thread
#include <mutex>                // std::mutex, std::unique_lock
#include <condition_variable>    // std::condition_variablestd::mutex mtx; // 全局互斥锁.
std::condition_variable cv; // 全局条件变量.
bool ready = false; // 全局标志位.void do_print_id(int id)
{std::unique_lock <std::mutex> lck(mtx);while (!ready) // 如果标志位不为 true, 则等待...cv.wait(lck); // 当前线程被阻塞, 当全局标志位变为 true 之后,// 线程被唤醒, 继续往下执行打印线程编号id.std::cout << "thread " << id << '\n';
}void go()
{std::unique_lock <std::mutex> lck(mtx);ready = true; // 设置全局标志位为 true.cv.notify_all(); // 唤醒所有线程.
}int main()
{std::thread threads[10];// spawn 10 threads:for (int i = 0; i < 10; ++i)threads[i] = std::thread(do_print_id, i);std::cout << "10 threads ready to race...\n";go(); // go!for (auto & th : threads)th.join();return 0;
}

另一个是:

#include <iostream>                // std::cout
#include <thread>                // std::thread, std::this_thread::yield
#include <mutex>                // std::mutex, std::unique_lock
#include <condition_variable>    // std::condition_variablestd::mutex mtx;
std::condition_variable cv;int cargo = 0;
bool shipment_available()
{return cargo != 0;
}// 消费者线程.
void consume(int n)
{for (int i = 0; i < n; ++i) {std::unique_lock <std::mutex> lck(mtx);cv.wait(lck, shipment_available); 这里的结构相当于while(!shipment_available){cv.wait()},要加个条件判断std::cout << cargo << '\n';cargo = 0;}
}int main()
{std::thread consumer_thread(consume, 10); // 消费者线程.创建一个线程并传入一个10的参数,// 主线程为生产者线程, 生产 10 个物品.其是下面的代码就是控制子线程的执行,当调用notif_one()后子线程就执行for里的一次代码for (int i = 0; i < 10; ++i) {while (shipment_available()) //这里是等待子线程wait()后面的代码执行完即cargo被置为0即表示运行完一次for里的代码。{std::this_thread::yield();  //这里是进行一下延时。}std::unique_lock <std::mutex> lck(mtx);cargo = i + 1;cv.notify_one();  //这里对wait进行解锁一次。}consumer_thread.join();return 0;
}

4、for的在c++11后新增的类型lambda的的用法:

// 第三种用法:简化数组遍历语法(从vs2008开始支持)
for each(auto item in vecNum)
{  strText.Format("%d", item);  AfxMessageBox(strText);
}  // 第四种用法:STL函数
std::for_each(vecNum.begin(), vecNum.end(), [](int item){  CString strText;  strText.Format("%d", item);  AfxMessageBox(strText);  });  // 第五种用法:C++11新增加的(VS2012支持)
for(auto item : vecNum)
{  strText.Format("%d", item);  AfxMessageBox(strText);
}  

5、在vector里判断某个元素是否存在方法;

       vector<string> vStr;vStr.pushback("hh");int nRet = std::count(vStr.begin(), vStr.end(), "xiaochun");//判断vector中是否有 "xiaochun" 这个元素

处理上面的方法,还可以使用上面的find函数。

6、判断map变量里的某个元素是否存在:

    1.判断某元素是否存在map<int, string> mapDemo;bool bRet = mapDemo.count(100);//判断mapDemo中是否有 key = 100 的元素       2.查找某个元素map<int, string>::iterator iter = mapDemo.find(100);if (iter != m_Int.end()){//找到了}else{//没找到}

C++11版本后的一写新语法及使用方法for、vector、map相关推荐

  1. win10更新1809版本后运行Dev-cpp, dos控制台字符乱码解决方法

    win10更新1809版本后运行Dev-cpp, dos控制台字符乱码解决方法 参考文章: (1)win10更新1809版本后运行Dev-cpp, dos控制台字符乱码解决方法 (2)https:// ...

  2. 更新Mathtype版本后,在WORD中无法使用的解决方法

    更新Mathtype版本后,在WORD中无法使用的解决方法 我之前使用的Mathtype版本是6.9的,最近更新了7.4之后打开WORD会出现禁用宏的提示. 点击启用内容,word会直接崩溃,我想应该 ...

  3. Mysql数据库从5.6.28版本升到8.0.11版本部署项目时遇到的问题及解决方法

    这篇文章主要介绍了Mysql数据库从5.6.28版本升到8.0.11版本过程中遇到的问题及解决方法,解决办法有三种,每种方法给大家介绍的都很详细 mysql数据库版本从5.6.28升到8.0.11过程 ...

  4. mysql 5.6升级8.0_Mysql数据库从5.6.28版本升到8.0.11版本部署项目时遇到的问题及解决方法...

    MysqL数据库版本从5.6.28升到8.0.11过程中部署项目时遇到的问题和解决方法,具体介绍如下所示: 首先这个项目用到了hibernate4.2.0,链接MysqL5.6.28没问题,换到8.0 ...

  5. win 7 系统ie浏览器升级11版本后,f12功能不可用的问题

    自从把ie8升级成11后,f12功能就不可用了.浏览器兼容模式也无法使用. 解决办法:下载windows补丁 IE11-Windows6.1-KB3008923-x64.msu 下载地址: 64位:h ...

  6. 解决JDK13版本后IntelliJ IDEA导入新字体文件

    由于JDK13版本不再单独生成jre文件,IDEA也不再单独生成jre文件,旧方法导入字体不再适用 . 以下是博主在JDK13版本IntelliJ IDEA导入自定义字体的方法: 先找出本机的Inte ...

  7. mysql8.0.11启动不了_8.0.11版本Mysql遇到MySQL 服务无法启动的解决方法

    转:https://blog.csdn.net/iyayaqiqi/article/details/80536110 系统环境:win10(1803),64位 MySQL版本:8.0.11免安装版 M ...

  8. Windows 11 - 打开操作安全中心提示 “需要使用新应用以打开此 Windowsdefender 链接“ 解决方案(电脑更新了 Win11 系统版本后,想关闭病毒防护盾牌的时候提示打不开)

    前言 注意:此问题出现在 Windows 11 系统 上,其他系统可作为参考. 当您点击任务栏上 此图标,想要关闭安全中心防护设置时, 出现了如下图所示问题,本文可以快速帮您解决: 第一步 搜索 Po ...

  9. 解决启动不了VMware虚拟机以及VM升级到新的版本后出现黑屏的问题

    解决启动不了VMware虚拟机以及VM升级到新的版本后出现黑屏的问题 参考文章: (1)解决启动不了VMware虚拟机以及VM升级到新的版本后出现黑屏的问题 (2)https://www.cnblog ...

  10. ML.NET 发布0.11版本:.NET中的机器学习,为TensorFlow和ONNX添加了新功能

    微软发布了其最新版本的机器学习框架:ML.NET 0.11带来了新功能和突破性变化. 新版本的机器学习开源框架为TensorFlow和ONNX添加了新功能,但也包括一些重大变化, 这也是发布RC版本之 ...

最新文章

  1. 用cxSelect插件补充一下回显过滤项功能
  2. java nep_Java 9 揭秘(1. 入门介绍)
  3. ssh-scan处理手记
  4. android 活动之间传递参数,Android:使用适配器在活动之间传递参数
  5. CSS 滤镜学习小结
  6. linux suse最新版本,SUSE Linux Enterprise Server 15 正式发布
  7. Android 自定义RecyclerView 实现真正的Gallery效果
  8. oracle linux下数据迁移到不同服务器
  9. JavaScript学习笔记 1
  10. C#中常见的数据结构简单介绍
  11. 苹果6s强制删除id锁_苹果解锁软件 屏幕解锁及AppleID解锁
  12. 利息基础理论 - 寿险精算(2)
  13. Cups打印机驱动延迟Bug解决
  14. 2011 年11 月系统集成项目管理工程师上午考试(刘俊平老师)
  15. 在salesforce中实现鼠标悬停显示提示框效果,并对显示框内容进行微缩页面布局
  16. 协同过滤推荐之基于近邻协同过滤(一)
  17. 自定义复选框checkbox样式
  18. HCIA/HCIP使用eNSP模拟STP实验与STP根桥、根端口、指定端口选举方法
  19. 成功解决:curl: (7) Failed connect to github-production-release-asset-2e65be.s3.amazonaws.com:443; 拒绝连接
  20. net start mysql 提示:发生系统错误 2。 系统找不到指定的文件。

热门文章

  1. Friends第七季第八季, Candler Monica终于结婚了,Joey爱上了Rachel
  2. Seventh season eighteenth episode,Joey got an award??????
  3. WAIC直击:星环科技以大数据AI技术构建新型数字底座
  4. pip install xx 时遇到There was a problem confirming the ssl certificate...的解决办法
  5. Codeforces 417 D. Cunning Gena
  6. MICCAI2019论文分享 PART①
  7. 写一个获取非行间样式的函数
  8. windows7计算机启动修复怎么办,Win7电脑开机提示启动修复无法进入系统怎么办?...
  9. 【详细】Python实现病毒仿真器
  10. Java Reference Objects or How I Learned to Stop Worrying and Love OutOfMemoryError