You need a c ++ algorithm commonly used collection, are here (b)

This paper relates to copying and replacement algorithm, arithmetic generation algorithm, the set of algorithms used

Need to traverse, search, sorting algorithms can read my previous article

1, and copy replacement algorithm

(1)copy

Element within the container specified range are copied to another vessel

Function prototype

copy(iterator beg,iterator end,iterator dest);

beg begin iterator

end end iterator

dest target starting iterator

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
 cout<<val<<" ";
}
void test01()
{
 vector<int>v1;
 for(int i=0;i<10;i++)
 {
  v1.push_back(i);
 }
 vector<int>v2;
 v2.resize(v1.size());
 copy(v1.begin(),v1.end(),v2.begin());
 for_each(v2.begin(),v2.end(),print);
 cout<<endl;
}
int main()
{
 test01();
}

(2)replace

The container specified in the scope of the old elements to modify new element

Function prototype

replace(iterator beg,iterator end,oldvalue,newvalue);

The old elements within the interval replaced with a new element

beg begin iterator

end end iterator

oldvalue old elements

newwalue new element

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
void test01()
{
 vector<int>v1;
 v1.push_back(1);
 v1.push_back(5);
 v1.push_back(3);
 v1.push_back(5);
 v1.push_back(5);
 cout<<"替换前:"<<endl;
 for_each(v1.begin(),v1.end(),MyPrint());
 cout<<endl;
 replace(v1.begin(),v1.end(),5,521);
 cout<<"替换后:"<<endl;
 for_each(v1.begin(),v1.end(),MyPrint());
}
int main()
{
 test01();
}

All the old elements are replaced with new elements

(3)replace_if

The elements within the interval satisfying the condition, replace the specified element

Function prototype

replace_if(iterator beg,iterator end,_pred,newvalue);

Alternatively conditional replacement element, to meet the conditions specified element

beg begin iterator

end end iterator

pred predicate

newvalue replaced with new elements

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
class Greater
{
public:
    bool operator()(int val)
 {
  return val>3;
 } 
};
void test01()
{
 vector<int>v1;
 v1.push_back(1);
 v1.push_back(5);
 v1.push_back(3);
 v1.push_back(5);
 v1.push_back(5);
 cout<<"替换前:"<<endl;
 for_each(v1.begin(),v1.end(),MyPrint());
 cout<<endl;
 replace_if(v1.begin(),v1.end(),Greater(),1000);
 cout<<"替换后:"<<endl;
 for_each(v1.begin(),v1.end(),MyPrint());
}
int main()
{
 test01();
}

(4)swap

Two interchangeable container element

Function prototype

swap(container c1,container c2);

Two interchangeable container element

c1 container 1, c2 container 2

2, the arithmetic generation algorithm

Arithmetic algorithm generation algorithm is small, the header file is included when using

#include <numeric>

(1)accumulate

The cumulative sum calculating section container element

Function prototype

accumulate(iterator beg,iterator end,value);

Calculating the cumulative sum container element

beg begin iterator

end end iterator

the accumulated value of the starting value

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
class Greater
{
public:
    bool operator()(int val)
 {
  return val>3;
 } 
};
void test01()
{
 vector<int>v;
 for(int i=0;i<100;i++)
 {
  v.push_back(i+1);
 }
 int num=accumulate(v.begin(),v.end(),0);
 cout<<"num="<<num<<endl;
}
int main()
{
 test01();
}

(2)fill

The vessel filled with the specified element

Function prototype

fill(iterator beg,  iterator end,  value);

beg begin iterator

end end iterator

Value of filling value

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
void test01()
{
 vector<int>v;
 v.resize(10);
 for_each(v.begin(),v.end(),MyPrint());
 fill(v.begin(),v.end(),100);
 cout<<endl;
 for_each(v.begin(),v.end(),MyPrint());
}
int main()
{
 test01();
}

3, a collection of commonly used algorithm

(1)set_intersection

The intersection of two sets of

Function prototype

set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);

Two sets must be ordered sequence

beg1 container 1 starts iterator

end1 container 1 ends iterator

beginning the container 2 beg2 iterator

end2 container 2 ends iterator

dest target vessel began iterator

Examples

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
void test01()
{
 vector<int>v1;
 vector<int>v2;
 for(int i=0;i<10;i++)
 {
  v1.push_back(i);
  v2.push_back(i+5);
 }
 vector<int>v3;
 v3.resize(min(v1.size(),v2.size()));
 vector<int>::iterator it=set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
 for_each(v3.begin(),it,MyPrint());
}
int main()
{
 test01();
}

set_intersection function returns an iterator, with an iterator accepts, as the end target iterator

(2)set_union

Sum of two sets of set and

Function prototype

set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);

Two sets must be ordered sequence

1 start beg1 container iterator
end1 end iterator container 1
beg2 container 2 starts iterators
end2 container 2 end iterator
dest target container starts iterator

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
void test01()
{
 vector<int>v1;
 vector<int>v2;
 for(int i=0;i<10;i++)
 {
  v1.push_back(i);
  v2.push_back(i+5);
 }
 vector<int>v3;
 v3.resize(v1.size()+v2.size());
 vector<int>::iterator it=set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
 for_each(v3.begin(),it,MyPrint());
}
int main()
{
 test01();
}

(3)set_difference

Differencing the two sets set

Function prototype

set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);

Two sets must be ordered sequence

beg1容器1开始迭代器
end1 容器1结束迭代器
beg2容器2开始迭代器
end2容器2结束迭代器
dest 目标容器开始迭代器

不过这里有一个前后顺序需要注意

差集:

v1: 0,1,2,3,4,5,6,7,8,9

v2: 5,6,7,8,9,10,11,12,13,14

v1对v2的差集为:0,1,2,3,4,

v2对v1的差集为:10,11,12,13,14

实例

#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
    bool operator()(int val)
 {
  cout<<val<<" ";
 } 
};
void test01()
{
 vector<int>v1;
 vector<int>v2;
 for(int i=0;i<10;i++)
 {
  v1.push_back(i);
  v2.push_back(i+5);
 }
 vector<int>v3;
 v3.resize(max(v1.size(),v2.size()));
 vector<int>::iterator it=set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
 cout<<"v1对v2的差集为:"<<endl;
 for_each(v3.begin(),it,MyPrint());
 vector<int>v4;
 v4.resize(max(v1.size(),v2.size()));
 vector<int>::iterator it2=set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),v4.begin());
 cout<<endl<<"v2对v1的差集为:"<<endl;
 for_each(v4.begin(),it2,MyPrint());
}
int main()
{
 test01();
}
发布了37 篇原创文章 · 获赞 3 · 访问量 1161

Guess you like

Origin blog.csdn.net/qq_45721778/article/details/105366140