Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / C++中的容器

容器与容器适配器

容器包括vector, deque, list, map, multimap, set, multiset。容器适配器包括基于deque的stack和queue,基于vector的priority_queue。string也实现了stl的接口。因为编写C++程序时经常需要查找容器的函数接口,故作此总结。C++新引入的容器与函数未引入。主要参考自:STL Containers and Container Adaptors

序列容器

包括vector,deque,list

共有函数

  • 构造函数ContainerType<T> c; ContainerType<T> c(num);ContainerType<T> c(num, val); ContainerType<T> c(inIterBegin, inIterEnd); //复制构造函数ContainerType<T> c(otherLikeContainer); ContainerType<T> c = otherLikeContainer;
  • 赋值构造函数c1 = c2
  • 比较运算c1 == c2 c1 != c2 c1 < c2 //按元素逐个比较c1 <= c2 c1 > c2 c1 >= c2
  • 容量empty() size() max_size() resize(num, val = default)
  • 迭代器和引用begin()end()rbegin() rend()front() back()
  • 插入值push_back(val) insert(iter, val) insert(iter, num, val)insert(iter, inIterBegin, inIterEnd)
  • 赋值(换掉容器内所有元素)assign(inIterBegin, inIterEnd) assign(num, val)
  • 删除元素pop_back() erase(iter) erase(iterBegin, iterEnd) clear()
  • 其他swap(otherLikeContainer)get_allocator()

特有函数

  • vector特有reserve(num)capacity()
  • list特有merge(otherList) //按照大小顺序合并,二者必须是有序的merge(otherList, binPred) remove(val)remove_if(unPred) reverse() sort() sort(binPred) splice(iter, otherList) //将otherList中所有元素移动到iter处splice(iter, otherList, otherIterBegin, otherIterEnd) unique() unique(binPred)
  • vector和deque特有at(index) //会检查下标范围operator[](index)
  • deque和list特有push_front(val)pop_front()

容器适配器

包括stack,queue,priority_queue

共有函数

c1 = c2 empty()size() push(val) pop()

特有函数

  • queue特有函数front() back()
  • stack和priority_queue 特有函数top() == != < <= > >=

序列容器与容器适配器函数表格

参见The STL Sequential Containers and Container Adaptors,
and their Member Functions

关联容器

包括map, multimap, set, multiset

共有函数

  • 赋值c1 = c2
  • 比较== != < <= > >=
  • 容量empty() const size() const max_size()
  • 迭代器begin() end() rbegin() rend()
  • 插入值insert(p, val) insert(start, end)
  • 删除erase(someKey) erase(iter) erase(start, end) clear()
  • 查找count(someKey) find(someKey)//返回迭代器lower_bound(someKey) //大于等于someKey的迭代器upper_bound(someKey) //大于someKey的迭代器equal_range(someKey)
  • 其他swap(otherLikeContainer) get_allocator() //key和val比较的函数对象key_comp() value_comp()

特有函数

其实不存在特有函数,只是这些函数的接口略有不同
  • map/multimap特有构造函数ContainerType<keyType, ValueType> c; ContainerType<KeyType, ValueType> c(inIterBegin, inIterEnd); ContainerType<KeyType, ValueType> c(otherLikeContainer);
  • set/multiset特有构造函数ContainerType<T> c; ContainerType<T> c(inIterBegin, inIterEnd); ContainerType<T> c(otherLikeContainer);
  • map特有成员函数operator[someKey]
  • map/set特有成员函数//返回值为pair<iterator, true/false(是否已经含有此值)>insert(val)
  • multimap/multiset特有成员函数//返回iteratorinsert(val)

关联容器函数表格

参见The STL Associative Containers and their Member Functions

其他

包括string,bitset等类容器

string

  • 构造函数string s;string s(c_string_value);string s(char_array, size_type_count);string s(string_value);string s(string_value, size_type_index);string s(string_value, size_type_index, size_type_count);string s(size_type_count, char_value);string s(input_iterator_start, input_iterator_end);
  • 取chars[i]s.at(i) //边界检查
  • 迭代器s.begin()s.end()s.rbegin()s.rend()
  • append与赋值operator+=s.append(string_value)s.append(c_string_value)s.append(size_type_count, char_value)s.append(c_string_value, size_type_count)s.append(c_string_value, size_type_index, size_type_count)s.append(first_input_iterator, last_input_iterator)//operator=s.assign(string_value)s.assign(c_string_value)s.assign(size_type_count, char_value)s.assign(c_string_value, size_type_count)s.assign(c_string_value, size_type_index, size_type_count)s.assign(start_input_iterator, end_input_iterator)
  • 转换为c-strings.copy(char_array, size_type_count, size_type_index)s.c_str() //返回以结束的char数组地址,数组归s所有,不要更改s.data() //返回不以结束的char数组地址,数组归s所有,不要更改
  • 子串s.substr(size_type_index)s.substr(size_type_index, size_type_count)
  • 容量及调整容量s.empty()s.capacity()s.length()s.size()s.max_size()s.reserve(size_type_value)s.resize(size_type_value, char_value)s.resize(size_type_value)
  • 删除s.clear()s.erase() //删除所有字符s.erase(size_type_index)s.erase(size_type_index, size_type_count)s.erase(iterator_position)s.erase(first_iterator, last_iterator)
  • 查找//所有的find均返回下标值,若找不到,返回string::npos////查找chars.find(char_value) s.find(char_value, size_type_index) s.rfind(char_value) s.rfind(char_value, size_type_index)////查找strings.find(string_value) s.find(string_value, size_type_index) //从index处开始查找//从后向前查找s.rfind(string_value) s.rfind(string_value, size_type_index)//查找cstrings.find(c_string_value, size_type_index, size_type_count) s.rfind(c_string_value, size_type_index, size_type_count)//s.find_first_of(char_value) s.find_first_of(char_value, size_type_index) s.find_first_not_of(char_value) s.find_first_not_of(char_value, size_type_index)////查找在/不在string中的char,返回下标s.find_first_of(string_value) s.find_first_of(string_value, size_type_index) s.find_first_not_of(string_value) s.find_first_not_of(string_value, size_type_index)//s.find_first_of(c_string_value, size_type_index, size_type_count) s.find_first_not_of(string_value, size_type_index, size_type_count)//s.find_last_of(char_value) s.find_last_of(char_value, size_type_index) s.find_last_not_of(char_value) s.find_last_not_of(char_value, size_type_index)//s.find_last_of(string_value) s.find_last_of(string_value, size_type_index) s.find_last_not_of(string_value) s.find_last_not_of(string_value, size_type_index)//s.find_last_of(c_string_value, size_type_index, size_type_count) s.find_last_not_of(string_value, size_type_index, size_type_count)
  • 插入值s.insert(size_type_index, string_variable) s.insert(size_type_index, c_string_value)s.insert(size_type_index1, string_variable,size_type_index2, size_type_count)s.insert(size_type_index, c_string_value, size_type_count)s.insert(size_type_index, size_type_count, char_value)//c++中函数形参总是count在val之前s.insert(iterator_position, size_type_count, char_value)s.insert(iterator_position, char_value)s.insert(iterator_position, input_iterator_first, input_iterator_last)//s.push_back(char_value)
  • 字符/字符串替换s.replace(size_type_index, size_type_count, string_value)s.replace(iterator_first, iterator_last, string_values.replace(size_type_index1, size_type_count1, string_value, size_type_index2, size_type_count2)s.replace(size_type_index, size_type_count, c_string_value)s.replace(iterator_first, iterator_last, c_string_value)s.replace(size_type_index, size_type_count1, c_string_value, size_type_count2)s.replace(iterator_first, iterator_last, c_string_value, size_type_count)s.replace(size_type_index, size_type_count1, size_type_count2, char_value)s.replace(iterator_first, iterator_last, size_type_count, char_value)s.replace(iterator_first, iterator_last, input_iterator_start, input_iterator_end)
  • 比较//==, !=, <, > <=, >=已经重载////compare返回值为int:s-others.compare(string_value)s.compare(size_type_index, size_type_count, string_value)s.compare(size_type_index1, size_type_count1, string_value, size_type_index2, size_type_count2)//s.compare(c_string_value)s.compare(size_type_index, size_type_count, c_string_value)s.compare(size_type_index, size_type_count1, c_string_value, size_type_count2)
  • 其他函数s.swap(string_variable)////以下三个非成员函数swap(string_variable1, string_variable2)//getline(inStream, string_variable)// string结果不包含delimitergetline(inStream, string_variable, char_delimiter_value)
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/115945.htm