STL_ Common algorithms

Time:2021-4-6

STL_ Common algorithms

1、 Common search algorithms

adjacent_find()

adjacent_find(iterator beg, iterator end, _callback);

In the range of iterator pair identification elements, find a pair of adjacent duplicate elements, and return the iterator pointing to the first element of the pair.

vector vecInt;
	vecInt.push_back(1);
	vecInt.push_back(2);
    vecInt.push_back(2);
    vecInt.push_back(4);
    vecInt.push_back(5);
	vecInt.push_back(5);
	vector::iterator it = adjacent_find(vecInt.begin(), vecInt.end()); 
    //*it = 2

binary_search()

bool binary_search(iterator beg, iterator end, value);

Find value in the ordered sequence, and return true if found. Note: in unordered sequences, do not use.

set setInt;
	setInt.insert(3);
    setInt.insert(1);
	setInt.insert(7);
	setInt.insert(5);
	setInt.insert(9);
	bool bFind = binary_search(setInt.begin(), setInt.end(), 5);//nFind=true

count()

count(iterator beg, iterator end, value);

Use the equal operator to compare the elements within the scope of the flag with the input value and return the equal number.

vector vecInt;
vecInt.push_back(1);
vecInt.push_back(2);
vecInt.push_back(2);
vecInt.push_back(4);
vecInt.push_back(2);
vecInt.push_back(5);
int iCount = count(vecInt.begin(),vecInt.end(),2);  //iCount==3

count_if()

count_ If (first iterator, non iterator, search value (result of value to be compared)) (condition count)

//Suppose vector vecinta, vecinta contains 1,3,5,7,9 elements

//Define the comparison function first
bool GreaterThree(int iNum){
     if(iNum>=3){
     	return true;
     }
     else{
	    return false;
     }
} 

int iCount = count_if(vecIntA.begin(), vecIntA.end(), GreaterThree);
//In this case, Icount = = 4

find()

find(iterator beg, iterator end, value)

Find: use the equal operator of the underlying element to compare the elements within the specified range with the input value. When matched, ends the search and returns the iterator for that element.

vector vecInt;
vecInt.push_back(1);
vecInt.push_back(3);
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);

vector::iterator it = find(vecInt.begin(), vecInt.end(), 5);        //*it == 5

find_if()

find_ if(iterator beg, iterator end, _ Callback); (conditional lookup)

find_ If: use the input function to perform find instead of the equal operator. Returns the iterator of the found element.

//Let's say that vector vecinta contains 1,3,5,3,9 elements 
vector::it = find_if(vecInt.begin(),vecInt.end(),GreaterThree);
//In this case, * it = = 3, * (it + 1) = = 5, * (it + 2) = = 3, * (it + 3) = = 9

2、 Common sorting algorithms

merge()

The following is the sorting and general algorithm: provide element sorting strategy

Merge: merge two ordered sequences and store them in another sequence.

//For example, vecinta, vecintb and vecintc are containers declared with vector. Vecinta already contains 1,3,5,7,9 elements, and vecintb already contains 2,4,6,8 elements
vecIntC.resize (9) ; // expand capacity
merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(),vecIntC.begin());
//At this time, vecintc stores nine elements in order: 1, 2, 3, 4, 5, 6, 7, 8, 9

sort()

Sort: rearranges the elements in the specified range in the default ascending order. To change the collation, you can enter a comparison function.

//Students
Class CStudent:
{
public:
    CStudent(int iID, string strName)
    {
		m_iID=iID; 
		m_strName=strName; 
	}
public:      
    int m_iID;
    string m_strName;
}

//Student number comparison function
bool Compare(const CStudent &stuA,const CStudent &stuB)
{
	return (stuA.m_iID vecStu;
    vecStu.push_ Back (cstudent (2, "second child");
	vecStu.push_ Back (cstudent (1, "boss");
	vecStu.push_ Back (cstudent (3, "old three");
	vecStu.push_ Back (cstudent (4, "old four");
	sort(vecStu.begin(),vecStu.end(),Compare);
	//At this time, vecstu container contains "oldest object", "2nd object", "3rd object", "4th object" in order
}

random_shuffle()

random_ Shuffle: randomly adjusts the order of elements within the specified range.

Srand (time (0)); // set random seed
vector vecInt;
vecInt.push_back(1);
vecInt.push_back(3);
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);
string str("itcastitcast ");
random_ shuffle( vecInt.begin (),  vecInt.end ()); // random sorting, such as: 9,7,1,5,3
random_ shuffle( str.begin (),  str.end ()); // random sorting, such as "itstcastic"

reverse()

Reverse: reverses the elements in the specified range

vector vecInt;
vecInt.push_back(1);
vecInt.push_back(3);
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);
reverse(vecInt.begin(), vecInt.end());       //{9,7,5,3,1}

3、 Common copy and replacement algorithms

copy()

The copy algorithm copies the specified range of elements in a container to another container

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
vector vecIntB;
vecIntB.resize (5) ; // expand space
copy(vecIntA.begin(), vecIntA.end(), vecIntB.begin());  //vecIntB: {1,3,5,7,9}

replace()

Replace (beg, end, oldvalue, newvalue): replaces all elements equal to oldvalue in the specified range with newvalue.

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(3);
vecIntA.push_back(9);
replace(vecIntA.begin(), vecIntA.end(), 3, 8);     //{1,8,5,8,9}

replace_if()

/*
	replace_ The if algorithm replaces the elements that meet the conditions in the specified range in the container with new elements
	@Param bag container start iterator
	@Param end container end iterator
	@Param callback function callback or predicate (returns a function object of type bool)
	@New elements of param oldvalue
*/
replace_if(iterator beg, iterator end, _callback, newvalue)

replace_ If: replace all elements whose operation result is true in the specified range with new values.

//Replace elements greater than or equal to 3 with 8
vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(3);
vecIntA.push_back(9);
replace_ if( vecIntA.begin (),  vecIntA.end (), greaterthree, 8); // the definition of greaterthree is above.

swap()

Swap: exchange elements of two containers

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vector vecIntB;
vecIntB.push_back(2);
vecIntB.push_back(4);
Swap (vecinta, vecintb); // exchange

4、 Common arithmetic and generation algorithms

accumulate()

Accumulate: sums the elements in the specified range, and then adds an initial value specified by Val to the result.

include

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
int iSum = accumulate(vecIntA.begin(), vecIntA.end(), 100);     //iSum==125

fill()

Fill: assign the input value to all elements within the scope of the flag.

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
fill(vecIntA.begin(), vecIntA.end(), 8);       //8, 8, 8, 8, 8

5、 Common set algorithms

set_union(),set_intersection(),set_difference()

set_ Union: construct an ordered sequence containing the union of two ordered sequences.

set_ Intersection: construct an ordered sequence, which contains the intersection of two ordered sequences.

set_ Difference: construct an ordered sequence that preserves elements that exist in the first ordered sequence but not in the second ordered sequence.

vector vecIntA;
vecIntA.push_back(1);
vecIntA.push_back(3);
vecIntA.push_back(5);
vecIntA.push_back(7);
vecIntA.push_back(9);
 
vector vecIntB;
vecIntB.push_back(1);
vecIntB.push_back(3);
vecIntB.push_back(5);
vecIntB.push_back(6);
vecIntB.push_back(8);

vector vecIntC;
vecIntC.resize(10);

//Union
set_union(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());       
//vecIntC : {1,3,5,6,7,8,9,0,0,0}

//Intersection
fill(vecIntC.begin(),vecIntC.end(),0);
set_intersection(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());   
//vecIntC: {1,3,5,0,0,0,0,0,0,0}

//Difference set
fill(vecIntC.begin(),vecIntC.end(),0);
set_difference(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());     
//vecIntC: {7,9,0,0,0,0,0,0,0,0}

6、 Common traversal algorithms

for_each()

/*
    Traversal algorithm traverses container elements
	@Param beg start iterator
	@Param end end end iterator
	@param _ Callback function callback or function object
	@Return function object
*/
for_each(iterator beg, iterator end, _callback);

for_ Each: use the specified function to iteratively access all elements in the specified range. The function must not modify elements in the sequence.

void show(const int &iItem){
    cout << iItem;
}

main()
{
    int iArray[] = {0,1,2,3,4};
    vector vecInt(iArray,iArray+sizeof(iArray)/sizeof(iArray[0]));
	for_each(vecInt.begin(), vecInt.end(), show);
//Results 0 1 2 3 4 was printed
}

transform()

/*
	The transform algorithm transports the specified container interval elements to another container
	Note: transform does not allocate memory to the target container, so we need to allocate memory in advance
	@Param beg1 source container start iterator
	@Param end1 source container end iterator
	@Param beg2 target container start iterator
	@param _ Cakkback callback function or function object
	@Return returns the target container iterator
*/
transform(iterator beg1, iterator end1, iterator beg2, _callbakc)

Transform: and for_ Each is similar to traversing all elements, but the elements of the container can be modified

int increase (int i){ 
    return i+1;  
} 
main()
{
	vector vecIntA;
    vecIntA.push_back(1);
    vecIntA.push_back(3);
	vecIntA.push_back(5);
    vecIntA.push_back(7);
    vecIntA.push_back(9);

    transform(vecIntA.begin(),vecIntA.end(),vecIntA.begin(),increase);   
    //vecIntA : {2,4,6,8,10}
}