algorithm之不变序列操作

时间:2023-03-09 17:30:20
algorithm之不变序列操作

概述:不变序列算法,参见http://www.cplusplus.com/reference/algorithm/

/*
std::for_each
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function fn); Apply function to range
Applies function fn to each of the elements in the range [first,last).
[对[first, last)中的所有元素调用函数fn]
The behavior of this template function is equivalent to:
[该模板函数的行为与以下操作等价:]
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) {
fn (*first);
++first;
}
return fn; // or, since C++11: return move(fn);
}
*/ #include <iostream>
#include <vector>
#include <algorithm> class myclass
{
public:
void operator() (int i)
{
std::cout<<' '<<i;
}
}; void myfunctoin (int i)
{
std::cout<<' '<<i;
} int main()
{
std::vector<int> myvec;
std::vector<int>::iterator it; for(int i=; i<; i++)
myvec.push_back(i+); std::cout<<"myvec contains:";
for_each(myvec.begin(), myvec.end(), myfunctoin);
std::cout<<'\n'; //or
myclass obj;
std::cout<<"myvec contains:";
for_each(myvec.begin(), myvec.end(), obj);
std::cout<<'\n'; system("pause");
return ;
}
/*
std::find
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val); Find value in range
Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.
[返回[first, last)中第一个指向val的迭代器,如果没有找到则返回last]
The function uses operator== to compare the individual elements to val.
[该函数使用operator==来比较元素元素是否为val]
The behavior of this function template is equivalent to:
[该模板函数的行为与以下操作等价:]
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while(first != last){
if(*first == val) return first;
++first;
}
return last;
}
*/ #include <iostream>
#include <vector>
#include <algorithm> int main()
{
// using std::find with array and pointer.
int myints[] = {, , , };
int *p; p = std::find(myints, myints+, );
if(p != myints+)
std::cout<<"Element found in myints: "<<*p<<'\n';
else
std::cout<<"Element not found in myints\n"; // using std::find with vector and iterator.
std::vector<int> myvector (myints, myints+);
std::vector<int>::iterator it; it = std::find(myvector.begin(), myvector.end(), );
if(it != myvector.end())
std::cout<<"Element found in myints: "<<*it<<'\n';
else
std::cout<<"Element not found in myints\n"; system("pause");
return ;
}
/*
std::find_if
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); Find element in range
Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns last.
[返回[first, last)中第一个使得pred返回真的迭代器,如果没有找到,则返回last]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:]
template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while(first != last){
if(pred(*first)) return first;
++first;
}
return last;
}
*/ #include <iostream>
#include <algorithm>
#include <vector> bool IsOdd (int i)
{
return ((i%) == );
} int main()
{
std::vector<int> myvector; myvector.push_back();
myvector.push_back();
myvector.push_back();
myvector.push_back(); std::vector<int>::iterator it = std::find_if(myvector.begin(), myvector.end(), IsOdd); if(it != myvector.end())
std::cout<<"The first odd value in myvector is "<<*it<<'\n';
else
std::cout<<"There is no odd value in myvector."<<'\n'; system("pause");
return ;
}
/*
//std::adjacent_find
template <class FowardIterator>
FowardIterator adjacent_find (FowardIterator first, FowardIterator last); template <class FowardIterator, class BinaryPredicate>
FowardIterator adjacent_find (FowardIterator first, FowardIterator last, BinaryPredicate pred); Find equal adjacent elements in range
Searches the range [first,last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.
[在[fisrt, last)中寻找第一次匹配成功的两个连续元素,并返回指向其中第一个元素的迭代器,如果没有匹配成功,则返回last]
Two elements match if they compare equal using operator== (or using pred, in version (2)).
[默认使用operator==来判断两个元素是否匹配成功,或者使用pred来判断]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:]
template <class FowardIterator>
FowardIterator adjacent_find (FowardIterator first, FowardIterator last)
{
if(first != last)
{
FowardIterator next = first;
++next;
while(next != last){
if(*first == *next) return first;
++first; ++next;
}
return last;
}
}
*/ #include <iostream>
#include <algorithm>
#include <vector> bool myfunctoin(int i, int j)
{
return (i == j);
} int main()
{
int myints[] = {, , , , , , , , };
std::vector<int> myvector(myints, myints+);
std::vector<int>::iterator it; it = std::adjacent_find(myvector.begin(), myvector.end()); if(it != myvector.end())
std::cout<<"The first pair of repeated elements are "<<*it<<'\n'; it = std::adjacent_find(++it, myvector.end(), myfunctoin); if(it != myvector.end())
std::cout<<"The second pair of repeated elements are "<<*it<<'\n'; system("pause");
return ;
}
/*
std::find_end
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); Find last subsequence in range
Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
[在序列[first1, last2)中搜索出最后一个与序列[first2, last2)相匹配的子序列,并返回指向搜索到的子序列的第一个元素的迭代器,如果没有搜索到则返回last1]
The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2).
[比较两个序列中的元素是通过operator==来进行的(或者使用pred)]
This function returns the last of such occurrences. For an algorithm that returns the first instead, see search.
[该函数按返回的是最后一个匹配的子序列,如果想要得到第一个匹配的子序列,请看search算法]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:]
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return last1; // specified in C++11 ForwardIterator1 ret = last1; while(first1 != last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while(*it1 == *it2){ // or: while (pred(*it1,*it2)) for version (2)
++it1; ++it2;
if(it2 == last2) {ret = first1; break;}
if(it1 == last1) return ret;
}
++first1;
}
return ret;
}
*/ #include <iostream>
#include <algorithm>
#include <vector> bool myfunctoin(int i, int j){
return (i==j);
} int main()
{
int myints[] = {, , , , , , , , , };
std::vector<int> myvector(myints, myints+); int needle1[] = {, , }; // using default comparison.
std::vector<int>::iterator it;
it = std::find_end(myvector.begin(), myvector.end(), needle1, needle1+); if(it != myvector.end())
std::cout<<"needle1 last found at position "<<(it - myvector.begin())<<'\n'; int needle2[] = {, , }; // using predicate comparison.
it = std::find_end(myvector.begin(), myvector.end(), needle2, needle2+, myfunctoin); if(it != myvector.end())
std::cout<<"needle2 last found at position "<<(it - myvector.begin())<<'\n'; system("pause");
return ;
}
/*
std::find_first_of
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); Find element from set in range
Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.
[返回指向[first1, last1)中第一个与[first2, last2)中任一元素相匹配的元素的迭代器,如果没有找到则返回last1]]
The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (or pred, in version (2)), until a pair matches.
[元素的比较是通过operator==来进行的(或者通过pred)]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:] template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
{
while(first1 != last1){
for(ForwardIterator2 it2 = first2; it2 != last2; ++it2){
if(*it2 == *first1) // or: if (pred(*it,*first)) for version (2)
return first1;
}
++first1;
}
return last1;
}
*/ #include <iostream> // std::cout
#include <algorithm> // std::find_first_of
#include <vector> // std::vector
#include <cctype> // std::tolower bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
} int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+);
std::vector<char>::iterator it; int needle[] = {'A','B','C'}; // using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+); if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n'; // using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+, comp_case_insensitive); if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n'; system("pause");
return ;
}
/*
std::equal
template <class InputItertor1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2); template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred); Test whether the elements in two ranges are equal
[比较两个范围中的元素是否相等]
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
[将[first1, last1)中的元素与以first2为起点的相同范围内的元素进行比较,如果两个范围中的元素全部相等则返回真]
The elements are compared using operator== (or pred, in version (2)).
[元素间通过operator==进行比较(或者使用pred)]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:]
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator2 last1, InputIterator2 first2)
{
while(first1 != last1){
if(!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2
return false;
++first1; ++first2;
}
return true;
}
*/ #include <iostream>
#include <algorithm>
#include <vector> bool mypredicate(int i, int j)
{
return (i == j);
} int main()
{
int myints[] = {, , , , };
std::vector<int> myvector(myints, myints+); if(std::equal(myvector.begin(), myvector.end(), myints))
std::cout<<"The contents of both sequences are equal.\n";
else
std::cout<<"The contents of both sequences differ.\n"; myvector[] = ; if(std::equal(myvector.begin(), myvector.end(), myints, mypredicate))
std::cout<<"The contents of both sequences are equal.\n";
else
std::cout<<"The contents of both sequences differ.\n"; system("pause");
return ;
}
/*
std::mismatch
template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator1 first2); template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator1 first2, BinaryPredicate pred); Return first position where two ranges differ
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns the first element of both sequences that does not match.
[比较[first1, last1)与[fist2, last2)之间的元素,并返回两者第一个不匹配的元素]
The elements are compared using operator== (or pred, in version (2)).
[元素间的比较通过operator==来进行(或者通过pred)]
The function returns a pair of iterators to the first element in each range that does not match.
[该函数的返回值类型是pair,其中的两个参数是指向两个区间中不匹配元素的迭代器]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:] template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2
{ ++first1; ++first2; }
return std::make_pair(first1,first2);
}
*/ #include <iostream>
#include <algorithm>
#include <vector>
#include <utility> //std::pair bool mypredicate(int i, int j){
return (i==j);
} int main()
{
std::vector<int> myvector;
for(int i=; i<; i++) myvector.push_back(i*); int myints[] = {, , , , }; std::pair<std::vector<int>::iterator, int*> mypair; // using default comparison
mypair = std::mismatch(myvector.begin(), myvector.end(), myints);
std::cout<<"First mismatch elements: "<<*mypair.first<<" and "<<*mypair.second<<'\n'; // using predicate comparison
mypair = std::mismatch(++mypair.first, myvector.end(), ++mypair.second, mypredicate);
std::cout<<"Second mismatch elements: "<<*mypair.first<<" and "<<*mypair.second<<'\n'; system("pause");
return ;
}
/*
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2, BinaryPredicate pred); Search range for subsequence
Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
[在[first1, last1)中搜索第一个与序列[first2, last2)相匹配的子序列,并返回指向搜索到的子序列的第一个元素的迭代器,如果没有搜索到则返回last1]
The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2).
[元素的比较是通过operator==来进行的(或者通过pred)]
This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end.
[该函数返回第一个与给定序列匹配的子序列,如果想要返回最后一个,请参看find_end.]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:] template <class ForwardIterator1, class ForwardIterator1>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2)
{
if(first2 == last2) return first1; // specified in C++11 while(first1 != last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while(*it1 == *it2){ // or pred(*it1, *it2) for verson 2
++it1; ++it2;
if(it2 == last2) return first1;
if(it1 == last1) return last1;
}
++first1;
}
return last1;
}
*/ #include <iostream>
#include <algorithm>
#include <vector> bool mypredicate(int i, int j){
return (i==j);
} int main()
{
std::vector<int> myvector;
for(int i=; i<; i++) myvector.push_back(i*);
std::vector<int>::iterator it; // using default comparison.
int needle1[] = {, , , };
it = std::search(myvector.begin(), myvector.end(), needle1, needle1+); if(it != myvector.end())
std::cout<<"needle1 found at position "<<it-myvector.begin()<<'\n';
else
std::cout<<"needle1 not found.\n"; // using predicate comparison.
int needle2[] = {, , };
it = std::search(myvector.begin(), myvector.end(), needle2, needle2+, mypredicate); if(it != myvector.end())
std::cout<<"needle2 found at position "<<it-myvector.begin()<<'\n';
else
std::cout<<"needle2 not found.\n"; system("pause");
return ;
}
/*
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val); template <class ForwardIterator, class Size, class T, class BinaryPredicate>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred); Search range for elements
Searches the range [first,last) for a sequence of count elements, each comparing equal to val (or for which pred returns true).
[在[first, last)中搜索是否连续出现count次val]
The function returns an iterator to the first of such elements, or last if no such sequence is found.
[该函数返回指向第一个等于val的元素的迭代器,如果没有找到则返回last]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:] template<class ForwardIterator, class Size, class T>
ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val)
{
ForwardIterator it, limit;
Size i; limit=first; std::advance(limit,std::distance(first,last)-count); //注:私认为应该是std::advance(limit, std::distance(first, last)-count+1) while (first!=limit)
{
it = first; i=0;
while (*it==val) // or: while (pred(*it,val)) for the pred version
{ ++it; if (++i==count) return first; }
++first;
}
return last;
}
*/ #include <iostream> // std::cout
#include <algorithm> // std::search_n
#include <vector> // std::vector bool mypredicate (int i, int j) {
return (i==j);
} int main () {
int myints[]={,,,,,,,};
std::vector<int> myvector (myints,myints+); std::vector<int>::iterator it; // using default comparison:
it = std::search_n (myvector.begin(), myvector.end(), , ); if (it!=myvector.end())
std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
else
std::cout << "match not found\n"; // using predicate comparison:
it = std::search_n (myvector.begin(), myvector.end(), , , mypredicate); if (it!=myvector.end())
std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
else
std::cout << "match not found\n"; system("pause");
return ;
}
/*
std::count
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); Count appearances of value in range
Returns the number of elements in the range [first,last) that compare equal to val.
[返回[first, last)中与val相等的元素的个数]
The function uses operator== to compare the individual elements to val.
[该函数使用operator==来判断元素是否等于val]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:]
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while(first != last){
if(*first == val) ++ret;
++first;
}
return ret;
}
*/ #include <iostream>
#include <algorithm>
#include <vector> int main()
{
int myints[] = {, , , , , , , };
int mycount = std::count(myints, myints+, );
std::cout<<"10 appears "<<mycount<<" times.\n"; std::vector<int> myvector(myints, myints+);
mycount = std::count(myvector.begin(), myvector.end(), );
std::cout<<"20 appears "<<mycount<<" times.\n"; system("pause");
return ;
}
/*
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred); Return number of elements in range satisfying condition
[返回区间中满足条件的元素个数]
Returns the number of elements in the range [first,last) for which pred is true.
[返回[first, last)中使得pred返回真的元素个数]
The behavior of this function template is equivalent to:
[该函数模板的行为等价于以下操作:] template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while(first != last)
{
if(pred(*first)) ++ret;
first++;
}
return ret;
}
*/ #include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <utility> class stuRecord
{
public:
class stuInfo
{
public:
void setName(std::string m_name) {name = m_name;}
void setAge(int m_age) {age = m_age;}
void setAddr(std::string m_addr) {addr = m_addr;} std::string getName() const {return name;}
int getAge() const {return age;}
std::string getAddr() const {return addr;} private:
std::string name;
int age;
std::string addr;
}; private:
int id;
stuInfo m_stuInfo; public:
stuRecord(int m_id, std::string m_name, int m_age, std::string m_addr)
{
id = m_id;
m_stuInfo.setName(m_name);
m_stuInfo.setAge(m_age);
m_stuInfo.setAddr(m_addr);
} int getId() const {return id;}
stuInfo getStuInfo() const {return m_stuInfo;}
}; typedef stuRecord::stuInfo stuRI; bool setRange(std::pair<int, stuRI> stu)
{
return ((stu.second.getAge() > ) && (stu.second.getAge() < ));
} int main()
{
stuRecord stu1(, "张三", , "上海");
stuRecord stu2 = stuRecord(, "李四", , "上海");
stuRecord stu3 = stuRecord(, "王五", , "深圳");
stuRecord stu4 = stuRecord(, "赵六", , "长沙");
stuRecord stu5 = stuRecord(, "孙七", , "广东"); std::map<int, stuRI> mymap;
mymap.insert(std::make_pair(stu1.getId(), stu1.getStuInfo()));
mymap.insert(std::make_pair(stu2.getId(), stu2.getStuInfo()));
mymap.insert(std::make_pair(stu3.getId(), stu3.getStuInfo()));
mymap.insert(std::make_pair(stu4.getId(), stu4.getStuInfo()));
mymap.insert(std::make_pair(stu5.getId(), stu5.getStuInfo())); int mycount = std::count_if(mymap.begin(), mymap.end(), setRange);
std::cout<<"The number of students whose age between 20 and 30 is "<<mycount<<'\n'; system("pause");
return ;
}