This series of articles is the learning notes of dark horse programmer C++ tutorial. The links of the previous series of articles are as follows
C++ core programming: memory model of P1- > program
C++ core programming: P2- > reference
C++ core programming: P3- > function improvement
C++ core programming: P4- > classes and objects - encapsulation
C++ core programming: P5- > classes and objects - initialization and cleaning of objects
C++ core programming: P6- > classes and objects - c++ object model and this pointer
C++ core programming: P7- > classes and objects - friends
C++ core programming: P8- > classes and objects - operator overloading
C++ core programming: P9- > classes and objects - inheritance
C++ core programming: P10- > classes and objects - polymorphism
C++ core programming: P11- > file operation
C++ core programming: P12- > template ---- function template
C++ core programming: P13- > template ---- class template
C++ core programming: P14- > STL----STL novice
C++ core programming: P15- > STL---- common containers (Part 1)
C++ core programming: P16- > STL---- common containers (Part 2)
C++ core programming: P17- > STL---- function object
C++ core programming: P18- > STL---- common algorithms (Part 1)
1, Common copy and replace algorithms
Learning objectives: master common copy and replacement algorithms
Algorithm Introduction:
Copy / / copy the elements of the specified range in the container to another container
replace / / modify the old element of the specified range in the container to a new element
replace_if / / replace the elements that meet the conditions in the specified range in the container with new elements
Swap / / swap the elements of the two containers
1.1 copy
Function Description: copy the elements in the specified range in the container to another container
Function prototype: copy(iterator beg, iterator end, iterator dest);
Find the element by value, find the iterator that returns the specified position, and the iterator that returns the end cannot be found
beg: start iterator
End: end iterator
dest: target start iterator
Case: test the function of copy
#include <algorithm> #include <vector> #include <iostream> using namespace std; class myPrints { public: void operator()(int val) { cout << val << " "; } }; void test01() { vector<int> v1; for (int i = 0; i < 10; i++) { v1.push_back(i + 1); } vector<int> v2; v2.resize(v1.size()); copy(v1.begin(), v1.end(), v2.begin()); for_each(v2.begin(), v2.end(), myPrints()); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
1.2 replace
Function Description: modify the old element of the specified range in the container to a new element
Function prototype: replace(iterator beg, iterator end, oldvalue, newvalue);
Replace the old element in the interval with a new element
beg start iterator
End end iterator
oldvalue old element
newvalue new element
Case: test the function of copy
#include <algorithm> #include <vector> #include <iostream> using namespace std; class myPrint { public: void operator()(int val) { cout << val << " "; } }; void test01() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(50); v.push_back(20); v.push_back(40); v.push_back(70); for_each(v.begin(), v.end(), myPrint()); cout << endl; replace(v.begin(), v.end(), 20, 2000); for_each(v.begin(), v.end(), myPrint()); } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
1.3 replace_if
Function Description: replace the elements that meet the conditions in the interval with the specified elements
Function prototype: replace_if(iterator beg, iterator end, _pred, newvalue);
Replace the elements according to the conditions, and replace the elements that meet the conditions with the specified elements
beg: start iterator
End: end iterator
_ pred: predicate
newvalue: replaced new element
Case: Test replace_ Functions of if
#include <algorithm> #include <vector> #include <iostream> using namespace std; class myPrint { public: void operator()(int val) { cout << val << " "; } }; class Greater30 { public: bool operator()(int val) { return val >= 30; } }; void test01() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(50); v.push_back(20); v.push_back(40); v.push_back(70); //Replace those greater than or equal to 30 with 3000 for_each(v.begin(), v.end(), myPrint()); cout << endl; replace_if(v.begin(), v.end(), Greater30(), 3000); for_each(v.begin(), v.end(), myPrint()); } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
1.4 swap
Function Description: exchange elements of two containers
Function prototype: swap(container c1, container c2);
Swap elements of two containers
c1: container 1
c2: container 2
Case: test the function of swap
#include <algorithm> #include <vector> #include <iostream> using namespace std; class myPrint { public: void 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 + 100); } cout << "Before exchange: " << endl; for_each(v1.begin(), v1.end(), myPrint()); cout << endl; for_each(v2.begin(), v2.end(), myPrint()); cout << endl; cout << "After exchange: " << endl; swap(v1, v2); for_each(v1.begin(), v1.end(), myPrint()); cout << endl; for_each(v2.begin(), v2.end(), myPrint()); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
2, Common arithmetic generation algorithm
Learning objectives: master common arithmetic generation algorithms
Note: the arithmetic generation algorithm is a small algorithm, and the header file included when using is \include <numeric>
Algorithm Introduction:
accumulate / / calculate the cumulative sum of container elements
fill / / add elements to the container
2.1 accumulate
Function Description: calculate the cumulative sum of interval content elements
Function prototype: accumulate(iterator beg, iterator end, value);
Calculate the cumulative sum of container elements
beg: start iterator
End: end iterator
Value: starting value (starting cumulative value, adding all subsequent values here)
Case: test the function of accumulate
#include <numeric> #include <vector> #include <iostream> using namespace std; class myPrint { public: void operator()(int val) { cout << val << " "; } }; void test01() { vector<int> v; for (int i = 0; i <= 100; i++) { v.push_back(i); } int a = accumulate(v.begin(), v.end(), 0); int b = accumulate(v.begin(), v.end(), 1000); cout << "a = " << a << ", b = " << b << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
2.2 fill
Function Description: fill the container with the specified elements
Function prototype: fill(iterator beg, iterator end, value);
Fill the container with elements
beg: start iterator
End: end iterator
Value: filled value
Case: test the function of fill
#include <numeric> #include <vector> #include <iostream> #include <algorithm> using namespace std; class myPrint { public: void operator()(int val) { cout << val << " "; } }; void test01() { vector<int> v; v.resize(10); //fill fill(v.begin(), v.end(), 100); for_each(v.begin(), v.end(), myPrint()); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
3, Common set algorithm
Learning objectives: master common set algorithms
Algorithm Introduction:
set_intersection / / find the intersection of two containers
set_union / / find the union of two containers
set_difference / / find the difference between two containers
3.1 set_intersection
Function Description: find the intersection of two containers
Function prototype: set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
Find the intersection of two sets. Note that the two sets must be ordered sequences
beg1: container 1 start iterator
end1: container 1 end iterator
beg2: container 2 start iterator
end2: container 2 end iterator
dest: target container start iterator
Case: Test Set_ Function of intersection
#include <numeric> #include <vector> #include <iostream> #include <algorithm> using namespace std; void myPrint(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); } for_each(v1.begin(), v1.end(), myPrint); cout << endl; for_each(v2.begin(), v2.end(), myPrint); cout << endl; vector<int> v3; //The container size is the smallest of the two containers v3.resize(min(v1.size(), v2.size())); vector<int>::iterator End = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); for_each(v3.begin(), End, myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
3.2 set_intersection
Function Description: find the union of two sets
Function prototype: set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
Find the union of two sets. Note: the two sets must be an ordered sequence.
beg1: container 1 start iterator
end1: container 1 end iterator
beg2: container 2 start iterator
end2: container 2 end iterator
dest: target container start iterator
Case: Test Set_ Functions of union.
#include <numeric> #include <vector> #include <iostream> #include <algorithm> using namespace std; void myPrint(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); } for_each(v1.begin(), v1.end(), myPrint); cout << endl; for_each(v2.begin(), v2.end(), myPrint); cout << endl; vector<int> v3; //The new container size is the sum of the previous two container sizes v3.resize(v1.size() + v2.size()); vector<int>::iterator End = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); for_each(v3.begin(), End, myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.
3.3 set_difference
Function Description: find the difference set of two sets
Function prototype: set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
Find the difference set of two sets. Note: the two sets must be an ordered sequence.
beg1: container 1 start iterator
end1: container 1 end iterator
beg2: container 2 start iterator
end2: container 2 end iterator
dest: target container start iterator
The definition of difference set is as follows:
Case: Test Set_ Function of difference
#include <numeric> #include <vector> #include <iostream> #include <algorithm> using namespace std; void myPrint(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); } for_each(v1.begin(), v1.end(), myPrint); cout << endl; for_each(v2.begin(), v2.end(), myPrint); cout << endl; vector<int> v3; v3.resize(max(v1.size(),v2.size())); //Difference set of v1 and v2 vector<int>::iterator End = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); for_each(v3.begin(), End, myPrint); cout << endl; //Difference set of v2 and v1 End = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin()); for_each(v3.begin(), End, myPrint); cout << endl; } int main() { test01(); system("pause"); return 0; }
Run, the effect is as follows.