Chromium Code Reviews| Index: base/stl_util.h |
| diff --git a/base/stl_util.h b/base/stl_util.h |
| index 89a53b070ef515cf81d584098e7aace8ed2ba201..cdd91b646e558111bedd24f33a33907773089e43 100644 |
| --- a/base/stl_util.h |
| +++ b/base/stl_util.h |
| @@ -90,6 +90,16 @@ void STLDeleteContainerPairSecondPointers(ForwardIterator begin, |
| } |
| } |
| +// Counts the number of instances of val in a container. |
| +template <typename Container, typename T> |
| +typename std::iterator_traits< |
| + typename Container::const_iterator>::difference_type |
| +STLCount(const Container* const container, const T& val) { |
|
Lei Zhang
2015/03/26 21:48:45
Can |container| be a const ref instead?
robliao
2015/03/26 21:51:48
My initial iteration started out like that, but I
miu
2015/03/26 22:24:43
I gotta agree with thestig@ here. "const Containe
robliao
2015/03/26 23:15:50
Changed and Updated.
|
| + const typename Container::const_iterator begin = container->begin(); |
| + const typename Container::const_iterator end = container->end(); |
| + return std::count(begin, end, val); |
| +} |
| + |
| // To treat a possibly-empty vector as an array, use these functions. |
| // If you know the array will never be empty, you can use &*v.begin() |
| // directly, but that is undefined behaviour if |v| is empty. |
| @@ -195,6 +205,14 @@ bool ContainsKey(const Collection& collection, const Key& key) { |
| return collection.find(key) != collection.end(); |
| } |
| +// Test to see if a collection like a vector contains a particular value. |
| +// Returns true if the value is in the collection. |
| +template <typename Collection, typename Value> |
| +bool ContainsValue(const Collection& collection, const Value& value) { |
| + return std::find(collection.begin(), collection.end(), value) != |
| + collection.end(); |
| +} |
| + |
| namespace base { |
| // Returns true if the container is sorted. |