std::erase_if (std::flat_multimap)
來自 cppreference.com
< cpp | 容器 | flat_multimap
在標頭檔案 <flat_map> 中定義 |
||
template< class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Pred > |
(C++23 起) | |
從 c 中擦除所有滿足謂詞 pred 的元素。
如果表示式 bool(pred(std::pair<const Key&, const T&>(e))) 為 true,則謂詞 pred 滿足,其中 e
是 c 中的某個元素。
Key
和 T
必須是 MoveAssignable。否則,行為是未定義的。
目錄 |
[編輯] 引數
c | - | 要從中擦除元素的容器介面卡 |
pred | - | 如果元素應被擦除,則返回 true 的謂詞 |
[編輯] 返回值
被刪除元素的數量。
[編輯] 複雜度
謂詞 pred 被精確呼叫 c.size() 次。
異常
如果 erase_if
丟擲異常,則 c 保持有效但未指定(可能為空)狀態。
注意
該演算法是穩定的,即未被刪除的元素的順序保持不變。
[編輯] 示例
執行此程式碼
#include <iostream> #include <flat_map> void println(auto rem, auto const& container) { std::cout << rem << '{'; for (char sep[]{0, ' ', 0}; const auto& [key, value] : container) std::cout << sep << '{' << key << ", " << value << '}', *sep = ','; std::cout << "}\n"; } int main() { std::flat_multimap<int, char> data { {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}, {4, 'f'}, {5, 'g'}, {5, 'g'}, }; println("Original:\n", data); const auto count = std::erase_if(data, [](const auto& item) { auto const& [key, value] = item; return (key & 1) == 1; }); println("Erase items with odd keys:\n", data); std::cout << count << " items removed.\n"; }
輸出
Original: {{1, a}, {2, b}, {3, c}, {4, d}, {4, f}, {5, e}, {5, g}, {5, g}} Erase items with odd keys: {{2, b}, {4, d}, {4, f}} 5 items removed.
[編輯] 另請參閱
移除滿足特定標準的元素 (函式模板) | |
(C++20)(C++20) |
移除滿足特定標準的元素 (演算法函式物件) |