llvm.org GIT mirror llvm / 6a2af3e
[stl-extras] Provide an adaptor of std::count for ranges. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288619 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Gottesman 2 years ago
2 changed file(s) with 26 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
638638 std::end(Range);
639639 }
640640
641 /// Wrapper function around std::count to count the number of times an element
642 /// \p Element occurs in the given range \p Range.
643 template
644 auto count(R &&Range, const E &Element) -> typename std::iterator_traits<
645 decltype(std::begin(Range))>::difference_type {
646 return std::count(std::begin(Range), std::end(Range), Element);
647 }
648
641649 /// Wrapper function around std::count_if to count the number of times an
642650 /// element satisfying a given predicate occurs in a range.
643651 template
235235 EXPECT_EQ("Tes", std::get<1>(Values));
236236 EXPECT_EQ('Y', std::get<2>(Values));
237237 }
238 }
238
239 TEST(STLExtrasTest, CountAdaptor) {
240 std::vector v;
241
242 v.push_back(1);
243 v.push_back(2);
244 v.push_back(1);
245 v.push_back(4);
246 v.push_back(3);
247 v.push_back(2);
248 v.push_back(1);
249
250 EXPECT_EQ(3, count(v, 1));
251 EXPECT_EQ(2, count(v, 2));
252 EXPECT_EQ(1, count(v, 3));
253 EXPECT_EQ(1, count(v, 4));
254 }
255 }