Replace erase-remove idiom with std::erase and std::erase_if (3)

Bug: webrtc:438403372
Change-Id: I08982640d626afd3dfb8bfd8fea363f8e2efa837
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/404584
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Commit-Queue: Björn Terelius <terelius@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45355}
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index bb89b36..4904e5b 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -1391,15 +1391,13 @@
 void P2PTransportChannel::RemoveRemoteCandidate(
     const Candidate& cand_to_remove) {
   RTC_DCHECK_RUN_ON(network_thread_);
-  auto iter =
-      std::remove_if(remote_candidates_.begin(), remote_candidates_.end(),
-                     [cand_to_remove](const Candidate& candidate) {
-                       return cand_to_remove.MatchesForRemoval(candidate);
-                     });
-  if (iter != remote_candidates_.end()) {
+  size_t num_erased = std::erase_if(
+      remote_candidates_, [cand_to_remove](const Candidate& candidate) {
+        return cand_to_remove.MatchesForRemoval(candidate);
+      });
+  if (num_erased > 0) {
     RTC_LOG(LS_VERBOSE) << "Removed remote candidate "
                         << cand_to_remove.ToSensitiveString();
-    remote_candidates_.erase(iter, remote_candidates_.end());
   }
 }
 
diff --git a/p2p/base/stun_dictionary.cc b/p2p/base/stun_dictionary.cc
index dce69ca..548cf40 100644
--- a/p2p/base/stun_dictionary.cc
+++ b/p2p/base/stun_dictionary.cc
@@ -278,10 +278,8 @@
   }
 
   // remove any pending updates.
-  pending_.erase(
-      std::remove_if(pending_.begin(), pending_.end(),
-                     [key](const auto& p) { return p.second->type() == key; }),
-      pending_.end());
+  std::erase_if(pending_,
+                [key](const auto& p) { return p.second->type() == key; });
 
   // Create tombstone.
   auto tombstone = std::make_unique<StunByteStringAttribute>(key);
@@ -304,10 +302,8 @@
   }
   int key = attr->type();
   // remove any pending updates.
-  pending_.erase(
-      std::remove_if(pending_.begin(), pending_.end(),
-                     [key](const auto& p) { return p.second->type() == key; }),
-      pending_.end());
+  std::erase_if(pending_,
+                [key](const auto& p) { return p.second->type() == key; });
 
   // remove any existing key.
   tombstones_.erase(key);
diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc
index 6bdb30e..7ea5dd7 100644
--- a/p2p/client/basic_port_allocator.cc
+++ b/p2p/client/basic_port_allocator.cc
@@ -420,15 +420,11 @@
     }
   }
 
-  networks.erase(
-      std::remove_if(networks.begin(), networks.end(),
-                     [networks_with_connection](const Network* network) {
-                       // If a network does not have any connection, it is
-                       // considered failed.
-                       return networks_with_connection.find(network->name()) !=
-                              networks_with_connection.end();
-                     }),
-      networks.end());
+  std::erase_if(networks, [networks_with_connection](const Network* network) {
+    // If a network does not have any connection, it is
+    // considered failed.
+    return networks_with_connection.contains(network->name());
+  });
   return networks;
 }
 
diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc
index f6ac9f8..2d543ee 100644
--- a/p2p/client/basic_port_allocator_unittest.cc
+++ b/p2p/client/basic_port_allocator_unittest.cc
@@ -449,17 +449,12 @@
 
   void OnCandidatesRemoved(PortAllocatorSession* session,
                            const std::vector<Candidate>& removed_candidates) {
-    auto new_end = std::remove_if(
-        candidates_.begin(), candidates_.end(),
-        [removed_candidates](Candidate& candidate) {
-          for (const Candidate& removed_candidate : removed_candidates) {
-            if (candidate.MatchesForRemoval(removed_candidate)) {
-              return true;
-            }
-          }
-          return false;
-        });
-    candidates_.erase(new_end, candidates_.end());
+    std::erase_if(candidates_, [removed_candidates](Candidate& candidate) {
+      return absl::c_any_of(
+          removed_candidates, [&candidate](const Candidate& removed_candidate) {
+            return candidate.MatchesForRemoval(removed_candidate);
+          });
+    });
   }
 
   bool HasRelayAddress(const ProtocolAddress& proto_addr) {
diff --git a/p2p/test/fake_ice_transport.h b/p2p/test/fake_ice_transport.h
index 28e8be2..c236c9a 100644
--- a/p2p/test/fake_ice_transport.h
+++ b/p2p/test/fake_ice_transport.h
@@ -11,7 +11,6 @@
 #ifndef P2P_TEST_FAKE_ICE_TRANSPORT_H_
 #define P2P_TEST_FAKE_ICE_TRANSPORT_H_
 
-#include <algorithm>
 #include <cstddef>
 #include <cstdint>
 #include <map>
@@ -290,15 +289,12 @@
   }
   void RemoveRemoteCandidate(const Candidate& candidate) override {
     RTC_DCHECK_RUN_ON(network_thread_);
-    auto it = std::remove_if(
-        remote_candidates_.begin(), remote_candidates_.end(),
+    size_t num_erased = std::erase_if(
+        remote_candidates_,
         [&](const Candidate& c) { return candidate.MatchesForRemoval(c); });
-    if (it == remote_candidates_.end()) {
+    if (num_erased == 0) {
       RTC_LOG(LS_INFO) << "Trying to remove a candidate which doesn't exist.";
-      return;
     }
-
-    remote_candidates_.erase(it);
   }
 
   void RemoveAllRemoteCandidates() override {