diff --git a/shell/browser/lib/bluetooth_chooser.cc b/shell/browser/lib/bluetooth_chooser.cc index 7d84cff45e2c..2bd9310679f6 100644 --- a/shell/browser/lib/bluetooth_chooser.cc +++ b/shell/browser/lib/bluetooth_chooser.cc @@ -110,13 +110,10 @@ void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id, // an event return; } - bool changed = false; - auto entry = device_map_.find(device_id); - if (entry == device_map_.end()) { - device_map_[device_id] = device_name; - changed = true; - } else if (should_update_name) { - entry->second = device_name; + + auto [iter, changed] = device_map_.try_emplace(device_id, device_name); + if (!changed && should_update_name) { + iter->second = device_name; changed = true; } @@ -139,11 +136,8 @@ std::vector BluetoothChooser::GetDeviceList() { std::vector vec; vec.reserve(device_map_.size()); - for (const auto& it : device_map_) { - DeviceInfo info = {it.first, it.second}; - vec.push_back(info); - } - + for (const auto& [device_id, device_name] : device_map_) + vec.emplace_back(device_id, device_name); return vec; }