diff --git a/cpp/src/phonenumberutil.cc b/cpp/src/phonenumberutil.cc index dfd9ad31a..d9f0f30bb 100644 --- a/cpp/src/phonenumberutil.cc +++ b/cpp/src/phonenumberutil.cc @@ -716,12 +716,9 @@ PhoneNumberUtil::PhoneNumberUtil() } PhoneNumberUtil::~PhoneNumberUtil() { - for (vector::const_iterator it = - country_calling_code_to_region_code_map_->begin(); - it != country_calling_code_to_region_code_map_->end(); - ++it) { - delete it->second; - } + STLDeleteContainerPairSecondPointers( + country_calling_code_to_region_code_map_->begin(), + country_calling_code_to_region_code_map_->end()); } // Public wrapper function to get a PhoneNumberUtil instance with the default diff --git a/cpp/src/stl_util.h b/cpp/src/stl_util.h index ead643def..29e86b39c 100644 --- a/cpp/src/stl_util.h +++ b/cpp/src/stl_util.h @@ -26,6 +26,16 @@ struct OrderByFirst { } }; +// Deletes the second attribute (pointer type expected) of the pairs contained +// in the provided range. +template +void STLDeleteContainerPairSecondPointers(const ForwardIterator& begin, + const ForwardIterator& end) { + for (ForwardIterator it = begin; it != end; ++it) { + delete it->second; + } +} + } // namespace phonenumbers } // namespace i18n