-
pair和tiplemake_pairmake_tuple
-
容器
- 顺序容器
listforward_listvectordeque
- 关联容器
mapmultimapsetmultiset
- 专用容器
queuepriority_queue
- 迭代器
nextadvanceprevbegincbegincrbegin
- 顺序容器
-
算法
- 标准库在头文件
<algorithm>中包含大量的通用函数。 - 元素迭代
fill函数将使用一个值填充一个容器generate函数时类似的,但不是单个值,它可以是一个函数、一个函数对象或者lambda表达式。for_each函数将变量访问迭代器指定的所有元素,间接引用迭代器,并将元素传递给函数。transform
- 获取信息
count函数用于对区间中指定数量的数字项进行统计。count_ifall_ofany_ofnone_of
- 容器比较
equalmismachis_permutation
- 修改元素
reverse翻转copycopy_backwardreverse_copymovemove_backwardremove_copyremove_copy_iferasereplacereplace_ifreplace_copyreplace_copy_ifrorateuniqueunique_copyiter_swapswap_ranges
- 查找元素
min_elementmax_elementminmax_elementadjacent_findfindfind_iffind_if_notdifferentfind_first_ofsearchfind_end
- 元素排序
sortstable_sortpartial_sort_copyis_sortedis_sorted_untilnth_elementpartial_sortpartitionshuffleheaplower_boundupper_boundmergeincludesset_unionfinal
- 标准库在头文件
-
数值库
- 使用
<ratio>的编译器运算。 - 使用
<complex>的复数运算。
- 使用
-
标准库应用
#include <iostream> #include <fstream> #include <vector> #include <string> #include <list> using namespace std; using vec_str = vector<string>; using list_str = list<string>; using vec_list = vector<list_str>; void usage() { cout << "usage: csv_parser file" << endl; } list_str parse_line(const string& line) { list_str data; string::const_iterator it = line.begin(); string item; bool bQuote = false; bool bDQuote = false; do { switch (*it) { case '\'': if (bDQuote) { item.push_back(*it); } else { if ((it + 1) != line.end() && *(it + 1) == '\'') { item.push_back(*it); ++it; } else { bQuote = !bQuote; if (bQuote) item.clear(); } } break; case '"': if (bDQuote) { item.push_back(*it); } else { if ((it + 1) != line.end() && *(it + 1) == '"') { item.push_back(*it); ++it; } else { bQuote = !bQuote; if (bQuote) item.clear(); } } break; case ',': if (bQuote || bDQuote) item.push_back(*it); else data.push_back(move(item)); break; default: item.push_back(*it); } ++it; } while (it != line.end()); //由于item变量即将被销毁,所以对move的调用可以确保其内容被移动到列表中,而不是赋值。 //如果没有这个调用,当将元素放入列表时,字符的拷贝构造函数将被调用。 data.push_back(move(item)); return data; } int main(int argc, const char* argv[]) { if (argc <= 1) { usage(); return 1; } ifstream stm; stm.open(argv[1], ios_base::in); if (!stm.is_open()) { usage(); cout << "cannot open" << argv[1] << endl; return 1; } vec_str lines; for (string line; getline(stm, line);) { if (line.empty()) continue; lines.push_back(move(line)); } stm.close(); vec_list parsed; for (string& line : lines) { parsed.push_back(parse_line(line)); } int count = 0; for (list_str row : parsed) { cout << ++count << "> "; for (string field : row) { cout << field << " "; } cout << endl; } return 0; }