[C++] Study Notes: 21 Days to Learn C++ Chapter 23-29 Highlights
STL algorithms are divided into two categories: non-variable order algorithms and variable order algorithms
non-variable algorithm
计数算法 count() count_if()
搜索算法 search() search_n() find() find_if() find_end() find_first_of() adjacent_find()
比较算法 equal() mismatch() lexicographical_compare()
Variation Algorithm
初始化算法 fill() fill_n() generate() fenerate_n()
修改算法for_each() transform()
复制算法 copy() copy_backward()
删除算法 remove() remove_if() remove_copy() remove_copy_if() unique() unique_copy()
替换算法 replace() replace_if()
排序算法 sort() stable_sort() partial_sort() partial_sort_copy()
Algorithms Available for Sorted Containers
binary_search() lower_bound() upper_bound()
look up
auto element = find(numsInVec.cbegin(),numsInVec.cend(),numToFind);
auto element = find_if(numsInVec.cbegin(),numsInVec.cend(),[](int element){return (element%2)==0;});
if (element != numInVec.end())
cout << “Result:Value found!” << endl;
count
size_t numZeros = count(numsInVec.cbegin(),numsInVec.cend(),0);
size_t numZeros = count_if(numsInVec.cbegin(),numsInVec.cend(),IsEven<int>);
search string
auto range = search(numsInVec.cbegin(),numsInVec.cend((),numsInList.cbegin(),numsInList.cend());
auto partialRange = search_n(numInVec.cbegin(),numsInVec.cend(),n,value);
filling
fill(numsInVec.begin(),numsInVec.end(),value);
fill_n(numsInVec.begin(),n,value);
generate
generate(numsInVec.begin().numsInVec.end(),rand);
generate_n(numdInList.begin(),n,rand);
Process one by one
for_each(start_of_range,end_of_range,unaryFunctionObject);
convert
transform(str.cbegin(),str.cend(),strLowerCaseCopy.begin(),::tolower);
transform(numsInVec.cbegin(),numsInVec.cend(),numsInVec2.cbegin(),sumInDeque.begin(),plus<int>());
copy
auto lastElement = copy(numsInList.cbegin().numsInList.cend(),numsInVec.begin());
copy_if (numsInList.cbegin().numsInList.cend(), lastElement,[](int elemet){return ((element%2)==1);});
copy_backward(numsInList.cbegin().numsInList.cend(),numsInVec.begin());
remove
auto newEnd = remove(numsInVec.begin(),numsInVec.end(),0);
//auto newEnd = remove_if(numsInVec.begin(),numsInVec.end(),[](int num){return ((num%2)==1);});
numsInVec.erase(newEnd,nemsInVec.end());
replace
replace(numsInVec.begin(),numsInVec.end(),value_to_be_replaced,value);
replace(numsInVec.begin(),numsInVec.end(),[](int element){return ((element%2)==0);},value);
to sort
sort(numsInVec.begin(),numsInVec.end()); // ascending order
sort(numsInVec.begin(),numsInVec.end(),[](int lhs,int rhs){return (lhs>rhs);}); // descending order
Deduplication
auto newEnd = unique(numsInVec.begin(),numsInVec.end());
numsInVec.erase(newEnd,numsInVec.end());
binary search
bool elementFound = binary_search(numsInVec.begin),numsInVec.end(),value);
if(elementFound)
cout << “Element found in the vector!” << endl;
stable_sort() will ensure that the relative order of the sorted elements remains the same
range partition
partion(numsInVec.begin(),numsInVec.end(),IsEven);
stable_partion(numsInVec.begin(),numsInVec.end(),IsEven);
inserts elements into a sorted set
auto minInsertPos = lower_bound(names.begin(),names.end(),str);
auto maxInsertPos = upper_bound(names.begin(),names.end(),str);
Use remove() remove_if() unique() Be sure to use erase() to resize the container
stack 成员函数push() pop() empty() size() top()
queue成员函数 push() pop() front() back() empty() size()
priority_queue成员函数 push() pop() top() empty() size()
Member method of bitset set() set(N,val=1) reset() reset(N) flip() size() count()
Smart pointer unique_pointer whose copy constructor and assignment operator are declared private
When writing applications that use multiple threads, consider using std::shared_ptr and std::weak_ptr
exception handling
try
{
// code made exception safe
}
catch(...) // catch(const std::exception& exp)
{
cout << “Exception in SomeFunc(), quitting” << endl;
}
custom exception class
class CustomException: public std::exception
{
string reason;
public:
// constructor, needs reason
CustomException(const char* why):reason(why){}
// redefining virtual function to return ‘reason’
virtual const char* what() const throw()
{
return reason.c_str();
}
}
Most operating systems provide semaphores and mutexes to synchronize threads.