简体   繁体   中英

Counting similar ints in a vector

Is there a stl function for vector that will allow me to find the number of elements in a vector? For example if I was looking for the count of ints 4 in this vector:

vector<ACard> PairFinder;

for (int i = 0; i < 5; i++)
{
Pairfinder[i] = rand()%5+1
}

You want std::count :

#include <algorithm>

int n = std::count(PairFinder.begin(), PairFinder.end(), 4);

If you cannot easily represent the value type you're looking for, you can use the variant std::count_if to specify a custom predicate.

是的,很容易,使用algorithm标头中的std::count

int fours = std::count(PairFinder.begin(), PairFinder.end(), 4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM