用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ std::count 的使用

2012-11-27 作者: 程序猿style举报

[c++]代码库

//  std::count 的使用
#include <algorithm>
#include <vector>
#include <iostream>

// A unary predicate for the *_if functions
template<typename elementType>
bool IsEven(const elementType& number) {
	// return true if the number is even
	return ((number % 2) == 0);
}

int main() {
	using namespace std;

	// A sample container - vector of integers
	vector<int> vecIntegers;

	// Inserting sample values
	for (int nNum = -9; nNum < 10; ++nNum)
		vecIntegers.push_back(nNum);

	// Display all elements in the collection
	cout << "Elements in our sample collection are: " << endl;
	vector<int>::const_iterator iElementLocator;
	for (iElementLocator = vecIntegers.begin()
	; iElementLocator != vecIntegers.end(); ++iElementLocator)
		cout << *iElementLocator << ' ';

	cout << endl << endl;

	// Determine the total number of elements
	cout << "The collection contains '";
	cout << vecIntegers.size() << "' elements" << endl;

	// Use the count_if algorithm with the unary predicate IsEven:
	size_t nNumEvenElements = count_if(vecIntegers.begin(), vecIntegers.end(),
			IsEven<int>);

	cout << "Number of even elements: " << nNumEvenElements << endl;
	cout << "Number of odd elements: ";
	cout << vecIntegers.size() - nNumEvenElements << endl;

	// Use count to determine the number of '0's in the vector
	size_t nNumZeroes = count(vecIntegers.begin(), vecIntegers.end(), 0);
	cout << "Number of instances of '0': " << nNumZeroes << endl << endl;

	cout << "Searching for an element of value 3 using find: " << endl;

	// Find a sample integer '3' in the vector using the 'find' algorithm
	vector<int>::iterator iElementFound;
	iElementFound = find(vecIntegers.begin() // Start of range
			, vecIntegers.end() // End of range
			, 3); // Element to find

	// Check if find succeeded
	if (iElementFound != vecIntegers.end())
		cout << "Result: Element found!" << endl << endl;
	else
		cout << "Result: Element was not found in the collection." << endl;

	cout << "Finding the first even number using find_if: " << endl;

	// Find the first even number in the collection
	vector<int>::iterator iEvenNumber;
	iEvenNumber = find_if(vecIntegers.begin() // Start of range
			, vecIntegers.end() // End of range
			, IsEven<int>); // Unary Predicate

	if (iEvenNumber != vecIntegers.end()) {
		cout << "Number '" << *iEvenNumber << "' found at position [";
		cout << distance(vecIntegers.begin(), iEvenNumber);
		cout << "]" << endl;
	}

	return 0;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...