名稱空間
變體
操作

std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>::erase

來自 cppreference.com
 
 
 
 
iterator erase( iterator position );
(1) (C++23 起)
iterator erase( const_iterator pos );
(2) (C++23 起)
iterator erase( const_iterator first, const_iterator last );
(3) (C++23 起)
size_type erase( const Key& key );
(4) (C++23 起)
template< class K >
size_type erase( K&& x );
(5) (C++23 起)

從容器中移除指定元素。保留其餘等效元素的順序。

1,2) 移除 pos 處的元素。
3) 移除範圍 [firstlast) 中的元素,該範圍必須是 *this 中的有效範圍。
4) 移除所有鍵等價於 key 的元素。
5) 移除所有鍵與值 x 比較等價的元素。此過載僅在限定 ID Compare::is_transparent 有效並表示一個型別,且 iteratorconst_iterator 都不能隱式轉換為 K 時才參與過載決議。它允許在不構造 Key 例項的情況下呼叫此函式。

迭代器 pos 必須有效且可解引用。因此,end() 迭代器(有效但不可解引用)不能用作 pos 的值。

目錄

[編輯] 引數

pos - 指向要移除元素的迭代器
first, last - 定義要移除元素範圍的迭代器對
key - 要移除元素的鍵值
x - 任何型別的值,可與表示要移除元素的鍵進行透明比較

[編輯] 返回值

1-3) 最後一個被移除元素之後的迭代器。
4) 移除元素的數量。
5) 移除元素的數量。

[編輯] 異常

1-3) 不丟擲任何異常。
4,5) Compare 物件丟擲的任何異常。

[編輯] 複雜度

取決於底層容器。通常是線性時間。

[編輯] 示例

#include <flat_map>
#include <iostream>
 
int main()
{
    std::flat_multimap<int, std::string> c =
    {
        {1, "one"}, {2, "two"}, {3, "three"},
        {4, "four"}, {5, "five"}, {6, "six"}
    };
 
    // erase all odd numbers from c
    for (auto it = c.begin(); it != c.end();)
    {
        if (it->first % 2 != 0)
            it = c.erase(it);
        else
            ++it;
    }
 
    for (auto& p : c)
        std::cout << p.second << ' ';
    std::cout << '\n';
}

輸出

two four six

[編輯] 參閱

清除內容
(public member function) [編輯]