《C++ primer plus》附录G:STL方法与函数(二) 学习笔记

来源:互联网 发布:老男孩linux培训 编辑:程序博客网 时间:2024/05/16 03:44

STL函数(一)十二个非修改序列操作

1.for_each()

对序列中的每个元素执行某操作

template< class InputIt, class UnaryFunction >UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f );

std::vector<int> nums{3, 4, 2, 8, 15, 267};auto print = [](const int& n) { std::cout << " " << n; }; std::cout << "before:";std::for_each(nums.begin(), nums.end(), print);//输出结果为:3 4 2 8 15 267

2.find()

在序列中找出某个值的第一次出现的位置,如果找不到该元素,则返回last

template< class InputIt, class T >InputIt find( InputIt first, InputIt last, const T& value );

3.find_if()

返回在序列中第一个元素使得p(*i)为true的迭代器,如果找不到该元素,则返回last

template< class InputIt, class UnaryPredicate >InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );

4.find_end()

返回一个迭代器,该迭代器指向[first1,last1)中最后一个与[first2,last2)区间匹配的元素,第一个版本使用==进行比较,如果找不到,则返回last1

template< class ForwardIt1, class ForwardIt2 >ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );


#include <algorithm>#include <iostream>#include <vector> int main(){    std::vector<int> v{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};    std::vector<int>::iterator result;     std::vector<int> t1{1, 2, 3};     result = std::find_end(v.begin(), v.end(), t1.begin(), t1.end());    if (result == v.end()) {        std::cout << "subsequence not found\n";    } else {        std::cout << "last subsequence is at: "                  << std::distance(v.begin(), result) << "\n";    }     std::vector<int> t2{4, 5, 6};    result = std::find_end(v.begin(), v.end(), t2.begin(), t2.end());    if (result == v.end()) {        std::cout << "subsequence not found\n";    } else {        std::cout << "last subsequence is at: "                   << std::distance(v.begin(), result) << "\n";    }}

5.find_first_of()

返回一个迭代器,该迭代器指向[first1,last1)中第一个与[first2,last2)区间任何一个元素相匹配的元素,第一个版本使用==进行比较,如果找不到,则返回last1

template< class InputIt, class ForwardIt >InputIt find_first_of( InputIt first, InputIt last,ForwardIt s_first, ForwardIt s_last );template< class InputIt, class ForwardIt, class BinaryPredicate >InputIt find_first_of( InputIt first, InputIt last,ForwardIt s_first, ForwardIt s_last, BinaryPredicate p );

6.adjacent_find()

返回一个迭代器,该迭代器指向[first1,last1)中第一个与后面元素匹配的值,第一个版本使用==进行比较,如果找不到,则返回last

template< class ForwardIt >ForwardIt adjacent_find( ForwardIt first, ForwardIt last );template< class ForwardIt, class BinaryPredicate>ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );

#include <algorithm>#include <iostream>#include <vector> int main(){    std::vector<int> v1{0, 1, 2, 3, 40, 40, 41, 41, 5};     auto i1 = std::adjacent_find(v1.begin(), v1.end());     if (i1 == v1.end()) {        std::cout << "no matching adjacent elements\n";    } else {        std::cout << "the first adjacent pair of equal elements at: "                  << std::distance(v1.begin(), i1) << '\n';    }     auto i2 = std::adjacent_find(v1.begin(), v1.end(), std::greater<int>());    if (i2 == v1.end()) {        std::cout << "The entire vector is sorted in ascending order\n";    } else {        std::cout << "The last element in the non-decreasing subsequence is at: "                  << std::distance(v1.begin(), i2) << '\n';    }}/*输出结果:The first adjacent pair of equal elements at: 4The last element in the non-decreasing subsequence is at: 7 */

7.count()

统计某个值出现的次数

template< class InputIt, class T >typename iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value );

8.count_if()

统计匹配pred的元素的次数

template< class InputIt, class UnaryPredicate >typename iterator_traits<InputIt>::difference_type count_if( InputIt first, InputIt last, UnaryPredicate p );

9.mismatch()

返回一个pair对象,mismatch()在[first1,last1)中查找第一个与first2开始不匹配的元素,pair指向不匹配的两个元素,第一个版本使用==进行比较

template< class InputIt1, class InputIt2 >std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1, InputIt2 first2 );template< class InputIt1, class InputIt2, class BinaryPredicate >std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2,BinaryPredicate p );

#include <iostream>#include <string>#include <algorithm> std::string mirror_ends(const std::string& in){    return std::string(in.begin(),                       std::mismatch(in.begin(), in.end(), in.rbegin()).first);} int main(){    std::cout << mirror_ends("abXYZba") << '\n'              << mirror_ends("abca") << '\n'              << mirror_ends("aba") << '\n';}/*输出结果:abaaba */

10.equal()

如果[first1,last1)区间中的每个元素都与一first2开始的序列中的相应元素相匹配,则返回true

template< class InputIt1, class InputIt2 >bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 );template< class InputIt1, class InputIt2, class BinaryPredicate >bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p );

#include <algorithm>#include <iostream>#include <string> bool is_palindrome(const std::string& s){    return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());} void test(const std::string& s){    std::cout << "\"" << s << "\" "        << (is_palindrome(s) ? "is" : "is not")        << " a palindrome\n";} int main(){    test("radar");    test("hello");}/*输出结果为:"radar" is a palindrome"hello" is not a palindrome */

11.search()

该函数在[first1,last1)中搜索第一个与[first2,last2)区间相匹配的序列,第一个版本使用==进行比较,如果找不到,则返回last1

template< class ForwardIt1, class ForwardIt2 >ForwardIt1 search( ForwardIt1 first, ForwardIt1 last, ForwardIt2 s_first, ForwardIt2 s_last );template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >ForwardIt1 search( ForwardIt1 first, ForwardIt1 last,ForwardIt2 s_first, ForwardIt2 s_last, BinaryPredicate p );

12.search_n()

该函数在[first1,last1)中搜索第一个与count个value相匹配的序列,第一个版本使用==进行比较,如果找不到,则返回last1

template< class ForwardIt, class Size, class T >ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value );template< class ForwardIt, class Size, class T, class BinaryPredicate >ForwardIt search_n( ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPredicate p );

















0 0