容器包括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 c;
ContainerType c(num);
ContainerType c(num, val);
ContainerType c(inIterBegin, inIterEnd);
//復制構造函數
ContainerType c(otherLikeContainer);
ContainerType 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()
reserve(num)
capacity()
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)
at(index) //會檢查下標范圍
operator[](index)
push_front(val)
pop_front()
包括stack,queue,priority_queue
c1 = c2
empty()
size()
push(val)
pop()
front()
back()
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()
其實不存在特有函數,只是這些函數的接口略有不同
ContainerType c;
ContainerType c(inIterBegin, inIterEnd);
ContainerType c(otherLikeContainer);
ContainerType c;
ContainerType c(inIterBegin, inIterEnd);
ContainerType c(otherLikeContainer);
operator[someKey]
//返回值為pair
insert(val)
//返回iterator
insert(val)
參見The STL Associative Containers and their Member Functions
包括string,bitset等類容器
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);
s[i]
s.at(i) //邊界檢查
s.begin()
s.end()
s.rbegin()
s.rend()
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)
s.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
//
//查找char
s.find(char_value)
s.find(char_value, size_type_index)
s.rfind(char_value)
s.rfind(char_value, size_type_index)
//
//查找string
s.find(string_value)
s.find(string_value, size_type_index) //從index處開始查找
//從後向前查找
s.rfind(string_value)
s.rfind(string_value, size_type_index)
//查找cstring
s.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_value
s.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-other
s.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結果不包含delimiter
getline(inStream, string_variable, char_delimiter_value)