zoukankan      html  css  js  c++  java
  • stl常用的查找算法

    #include<iostream>
    using namespace std;
    #include"vector"
    #include"algorithm"
    #include"functional"
    
    
    bool lowThree(int &iNum)
    {
    	return (iNum > 3);
    }
    int main()
    {
    	vector<int> v1;
    	v1.push_back(1);
    	v1.push_back(2);
    	v1.push_back(2);
    	v1.push_back(6);
    	v1.push_back(8);
    
    	
    	// adjacent_find在iterator对标识元素范围内,查找一对相邻重复元素,
    	//找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。
    	vector<int> ::iterator it = adjacent_find(v1.begin(), v1.end());
    	cout << *it << endl;
    	//初步结论,在stl算法中的谓词不能用数字代替,而函数可以用数字代替
    
    	//在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。
    	bool b1 = binary_search(v1.begin(), v1.end(), 6);
    	cout << b1 << endl;
    
    	//利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。
    	int i1 = count(v1.begin(), v1.end(), 5);
    	cout << i1 << endl;
    	//参数中要求是谓词。可以直接写一个回调函数
    	int i2 = count_if(v1.begin(), v1.end(), lowThree);
    	cout << i2 << endl;
    	
    
    	vector<int> ::iterator it22 = find(v1.begin(), v1.end(), 5);
    	cout << *it22 << endl;
    
    
    	vector<int> ::iterator it3 = find_if(v1.begin(), v1.end(), lowThree);
    	cout << *it3 << endl;
    
    	system("pause");
    }
    

      

  • 相关阅读:
    Ubuntu adb device
    ubuntu系统下创建软件桌面快捷方式
    Ubuntu 配置java环境变量
    Ubuntu 12.04 安装Chrome步骤
    java和php中static+final+synchronized 总结
    Sensor Hub 初探
    Ionic+PhoneGap+ Cordova
    Ionic初探 + 混合app的尝试
    ubuntu+dpkg+apt-get+aptitude 区别
    ubuntu下桌面系统及切换gdm+kdm+lightdm
  • 原文地址:https://www.cnblogs.com/xiaochige/p/6964662.html
Copyright © 2011-2022 走看看