【C++】学习笔记:21天学通C++ 第23-29章重点选摘
STL算法分两大类:非变序算法和变序算法
非变序算法
计数算法 count() count_if()
搜索算法 search() search_n() find() find_if() find_end() find_first_of() adjacent_find()
比较算法 equal() mismatch() lexicographical_compare()
变序算法
初始化算法 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()
可用于有序容器的算法
binary_search() lower_bound() upper_bound()
查找
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;
计数
size_t numZeros = count(numsInVec.cbegin(),numsInVec.cend(),0);
size_t numZeros = count_if(numsInVec.cbegin(),numsInVec.cend(),IsEven<int>);
搜索字符串
auto range = search(numsInVec.cbegin(),numsInVec.cend((),numsInList.cbegin(),numsInList.cend());
auto partialRange = search_n(numInVec.cbegin(),numsInVec.cend(),n,value);
填充
fill(numsInVec.begin(),numsInVec.end(),value);
fill_n(numsInVec.begin(),n,value);
生成
generate(numsInVec.begin().numsInVec.end(),rand);
generate_n(numdInList.begin(),n,rand);
逐个处理
for_each(start_of_range,end_of_range,unaryFunctionObject);
转换
transform(str.cbegin(),str.cend(),strLowerCaseCopy.begin(),::tolower);
transform(numsInVec.cbegin(),numsInVec.cend(),numsInVec2.cbegin(),sumInDeque.begin(),plus<int>());
复制
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());
移除
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(numsInVec.begin(),numsInVec.end(),value_to_be_replaced,value);
replace(numsInVec.begin(),numsInVec.end(),[](int element){return ((element%2)==0);},value);
排序
sort(numsInVec.begin(),numsInVec.end()); // ascending order
sort(numsInVec.begin(),numsInVec.end(),[](int lhs,int rhs){return (lhs>rhs);}); // descending order
去重
auto newEnd = unique(numsInVec.begin(),numsInVec.end());
numsInVec.erase(newEnd,numsInVec.end());
二分查找
bool elementFound = binary_search(numsInVec.begin),numsInVec.end(),value);
if(elementFound)
cout << “Element found in the vector!” << endl;
stable_sort()将确保排序后元素的相对顺序保持不变
范围分区
partion(numsInVec.begin(),numsInVec.end(),IsEven);
stable_partion(numsInVec.begin(),numsInVec.end(),IsEven);
在有序集合中插入元素
auto minInsertPos = lower_bound(names.begin(),names.end(),str);
auto maxInsertPos = upper_bound(names.begin(),names.end(),str);
使用remove() remove_if() unique()务必使用erase()调整容器大小
stack 成员函数push() pop() empty() size() top()
queue成员函数 push() pop() front() back() empty() size()
priority_queue成员函数 push() pop() top() empty() size()
bitset 的成员方法 set() set(N,val=1) reset() reset(N) flip() size() count()
智能指针 unique_pointer 其复制构造函数和赋值运算符被声明为私有
编写使用多个线程的应用程序时,可考虑使用std::shared_ptr和std::weak_ptr
异常处理
try
{
// code made exception safe
}
catch(...) // catch(const std::exception& exp)
{
cout << “Exception in SomeFunc(), quitting” << endl;
}
自定义异常类
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();
}
}
大多数操作系统都提供信号量(semaphore)和互斥量(mutex)来同步线程。