STL sequential containers
std::vector std::deque
std::list
std::forward_list
STL associative containers
std::set std::unordered_set
std::map std::unordered_map
std::multiset std::unordered_multiset
std::multimap std::unordered_multimap
STL container adapter
std::stack std::queue std::priority_queue
STL string class
std::string std::wstring
for(auto charLocator = stlString.cbegin();charLocator !=stlString.end();++charLocator){}
std::string lookup
while(subStr!=string::npos)
{
// Make find() search forward from the next character onwards
size_t searchOffset = subStrPos + 1;
subStrPos = sampleStr.find(“ ”,searchOffset);
}
std::string reverse
reverse(sampleStr.begin(),sampleStr.end());
case conversion
transform(inStr.begin().inStr.end(),inStr.begin(),::toupper);
transform(inStr.begin().inStr.end(),inStr.begin(),::tolower);
oprator "" s for C++14 std::string
Use pointer syntax to access elements in vector
vector<int>::const_iterator element = integers.cbegin();
auto element = integers.cbegin();
std::vector size vec.size(); capacity vec.capacity(); allocation capacity vec.reserve(num);
std::deque is similar to vector, but supports push_front() and pop_front()
std::list inversion list.reverse() Sort list.sort()
std::forward_list When inserting elements, only push_front() can be used, and insert() can also be used to insert
STL set and multiset specify sorting criteria
// used as a template parameter in set / multiset instantiation
template <typename T>
struct SortDescending
{
bool operator()(const T& lhs, const T& rhs) const
{
return (lhs>rhs);
}
}
set <int, SortDescending<int>> setInts;
multisets <int, SortDescending<int>> msetInts;
Tips: 对于其对象储存在set或multiset等容器中的类,别忘了在其中实现运算符<和==,分别用于排序和查找。
STL std::map and std::multimap instantiation
#include <map>
using namespace std;
...
map<keyType, valueType, Predicate = std::less<keyType>> mapObj;
multimap <keyType, valueType, Predicate = std::less<keyType>> mmapObj;
insert element
insert(make_pair(key, value));
insert(pair<keyType, valueType>(key, value));
Find elements in multimap
auto pairFound = mmapIntToStr.find(key);
// check if find() succeeded;
if(pairFound != mmapIntToStr.end())
{
// Find the number of pairs that have the same supplied key
size_t numPairInMap = mmapIntToStr.count(key);
for(size_t count = 0 ;count < numPairInMap;++count)
{
cout << “Key:” << pairFound->first ;
cout << “, Value [” << counter << “] = ”;
cout << pairFound->second << endl;
++ pairFound;
}
}
else
cout << “Element not found in the multimap”;
基于散列表的STL键值对容器std::unordered_map, 其平均插入和删除时间固定,查找元素的时间也是固定的。
散列函数hash_function();load_factor()指出了unordered_map桶的填满程度。因插入元素导致load_factor()超过max_load_factor()时,unordered_map将重新组织以增加桶数,并重建散列表。
Function object to store state
template<typename elementType>
struct DisplayElementKeepCount
{
int count;
DisplayElementKeepCount():count(0) {}
void operator()(const elementType& elelment)
{
++count;
cout << element << ‘ ’;
}
};
General syntax for lambda expressions
[stateVar1, stateVar2](Type1& var1, Type2& var2){// lambda code here;}
To modify a state variable in a lambda expression
[stateVar1, stateVar2](Type& param) mutable {// lambda code here;}
If the modification is still valid externally
[&stateVar1, &stateVar2](Type& param) {// lambda code here;}
To explicitly specify the return type
[stateVar1, stateVar2](Type var1, Type var2) -> ReturnType
{return (value or expression);}
In the case of compound statements
[stateVar1, stateVar2](Type var1, Type var2) -> ReturnType
{
Statement 1;
Statement 2;
return (value or expression);
}
To pass all local variables
[=](Type& Param, ...){... expression;}
A lambda expression instance that accepts a state variable via a capture list
[divisor](int dividend){return (dividend%divisor) == 0;}