C++ STL Algorithms

Finding algorithm: Determining whether a value is contained in a container

  1. count(first,last,value);
    Returns the number of elements equal to value in the [first, last] range.
    Code example:
#include <iostream>     // std::cout
#include <algorithm>    // std::count
#include <vector>       // std::vector
using namespace std;
int main() {
	int myints[] = { 10,20,30,30,20,10,10,20 };   // 8 elements
	int mycount = count(myints, myints + 8, 10);
	cout << "10 appears " << mycount << " times."<<endl;

	vector<int> myvector(myints, myints + 8);
	mycount = count(myvector.begin(), myvector.end(), 20);
	std::cout << "20 appears " << mycount << " times."<<endl;

	system("pause");
	return 0;
}

Output:

  1. ①count_if(first,last,value,IsOdd);
    Returns the number of elements within [first, last] range that make IsOdd true
    ②count_if(first,last,value,IsOdd);
    Returns the number of elements within [first, last] range that make IsOdd true and equal to value
    Example: Statistics 1-10 is the number of multiples of 3:
#include <iostream>     // std::cout
#include <algorithm>    // std::count_if
#include <vector>       // std::vector
using namespace std;
bool IsOdd(int i) { 
	return ((i % 2) == 1); 
}
int main() {
	vector<int> myvector;
	for (int i = 1; i < 10; i++) 
		myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9
	int mycount = count_if(myvector.begin(), myvector.end(), IsOdd);// 1 3 5 7 9
	cout << "myvector contains " << mycount << " odd values."<<endl;

	system("pause");
	return 0;
}

Output:

  1. binary_search(first,last,value);
    [first,last) If value is found in the range, the Boolean value is true, otherwise false is returned.
    binary_search() implements a binary search algorithm.
    Elements in a sequence must be arranged in ascending order (from small to large).
    Code example:
#include <iostream>     // std::cout
#include <algorithm>    // std::binary_search, std::sort
#include <vector>       // std::vector
using namespace std;
bool myfunction(int i, int j) { 
	return (i < j); 
}
int main() {
	int myints[] = { 1,2,3,4,5,4,3,2,1 };
	vector<int> v(myints, myints + 9); // 1 2 3 4 5 4 3 2 1

	sort(v.begin(), v.end());

	cout << "looking for a 3... ";
	if (binary_search(v.begin(), v.end(), 3))
		cout << "found!"<<endl; 
	else 
		cout << "not found."<<endl;

	sort(v.begin(), v.end(), myfunction);

	cout << "looking for a 6... ";
	if (binary_search(v.begin(), v.end(), 6))
		cout << "found!"<<endl; 
	else 
		cout << "not found."<<endl;

	system("pause");
	return 0;
}

Output:

  1. ①adjacent_find (first,last);
    When two consecutive elements are found to be equal on the iterator interval [first, last], the iterator position of the first element in the pair of elements is returned, and the last element is returned if it is not found.
    ②adjacent_find (first,last,myfunction);
    Find a pair of continuous identical elements on the iterator interval [first, last] that satisfy the myfunction condition, and return last if it is not found.
    Code example:
#include <iostream>     // std::cout
#include <algorithm>    // std::adjacent_find
#include <vector>       // std::vector
using namespace std;
bool myfunction(int i, int j) {
	return (i == j);
}
int main() {
	int myints[] = { 5,20,5,30,30,20,10,10,20,20 };
	vector<int> myvector(myints, myints + 10); 
	vector<int>::iterator it; 

	it = adjacent_find(myvector.begin(), myvector.end());

	if (it != myvector.end())
		cout << "the first pair of repeated elements are: " << *it << '\n';

	it = adjacent_find(++it, myvector.end(), myfunction);

	if (it != myvector.end())
		cout << "the second pair of repeated elements are: " << *it << '\n';

	it = adjacent_find(++it, myvector.end(), myfunction);

	if (it != myvector.end())
		cout << "the third pair of repeated elements are: " << *it << '\n';

	system("pause");
	return 0;
}

Output:
5. ①equal_range (first,last,value);

Posted by luanne on Sun, 13 Oct 2019 09:03:04 -0700