STL_Algorithm5-math: random_shuffle, count, count_if, min_element, max_element, accumulate, for_each, transform
2010-12-25 12:09
645 查看
// Math.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
using std::cout;
using std::endl;
bool greater9( int );
void outputSquare( int );
int calculateCube( int );
int main()
{
const int SIZE = 10;
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector< int > v( a1, a1 + SIZE );
std::ostream_iterator< int > outputIt( cout, " " );
cout<<"Vector v before random_shuffle: ";
std::copy( v.begin(), v.end(), outputIt );
std::random_shuffle( v.begin(), v.end() );
cout<< "/nVector v after random_shuffle: ";
std::copy( v.begin(), v.end(), outputIt );
int a2[] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
std::vector< int > v2( a2, a2 + SIZE );
cout<<"/n/nVector v2 contains: ";
std::copy( v2.begin(), v2.end(), outputIt );
int result = std::count( v2.begin(), v2.end(), 8 );
cout<<"/nNumber of elements matching 8: "<<result;
result = std::count_if( v2.begin(), v2.end(), greater9 );
cout<<"/nNumber of elements greater than 9: "<<result;
cout<<"/n/nMinimum element in Vector v2 is: "
<<*(std::min_element( v2.begin(), v2.end() ) );
cout<<"/n/nMaxmum element in Vector v2 is: "
<<*(std::max_element( v2.begin(), v2.end() ) );
cout<<"/n/nThe total of the elements in Vector v is: "
<< std::accumulate( v.begin(), v.end(), 0 );
cout<< "/n/nThe square of every integer in Vector v is:/n";
std::for_each( v.begin(), v.end(), outputSquare );
std::vector< int > cubes( SIZE );
std::transform( v.begin(), v.end(), cubes.begin(), calculateCube );
cout<< "/n/nThe cube of every integer in Vector v is:/n";
std::copy( cubes.begin(), cubes.end(), outputIt );
cout<<endl;
return 0;
}
bool greater9( int value )
{
return value > 9;
}
void outputSquare( int value )
{
cout<<value * value<< ' ';
}
int calculateCube( int value )
{
return value*value*value;
}
//
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
using std::cout;
using std::endl;
bool greater9( int );
void outputSquare( int );
int calculateCube( int );
int main()
{
const int SIZE = 10;
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector< int > v( a1, a1 + SIZE );
std::ostream_iterator< int > outputIt( cout, " " );
cout<<"Vector v before random_shuffle: ";
std::copy( v.begin(), v.end(), outputIt );
std::random_shuffle( v.begin(), v.end() );
cout<< "/nVector v after random_shuffle: ";
std::copy( v.begin(), v.end(), outputIt );
int a2[] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 };
std::vector< int > v2( a2, a2 + SIZE );
cout<<"/n/nVector v2 contains: ";
std::copy( v2.begin(), v2.end(), outputIt );
int result = std::count( v2.begin(), v2.end(), 8 );
cout<<"/nNumber of elements matching 8: "<<result;
result = std::count_if( v2.begin(), v2.end(), greater9 );
cout<<"/nNumber of elements greater than 9: "<<result;
cout<<"/n/nMinimum element in Vector v2 is: "
<<*(std::min_element( v2.begin(), v2.end() ) );
cout<<"/n/nMaxmum element in Vector v2 is: "
<<*(std::max_element( v2.begin(), v2.end() ) );
cout<<"/n/nThe total of the elements in Vector v is: "
<< std::accumulate( v.begin(), v.end(), 0 );
cout<< "/n/nThe square of every integer in Vector v is:/n";
std::for_each( v.begin(), v.end(), outputSquare );
std::vector< int > cubes( SIZE );
std::transform( v.begin(), v.end(), cubes.begin(), calculateCube );
cout<< "/n/nThe cube of every integer in Vector v is:/n";
std::copy( cubes.begin(), cubes.end(), outputIt );
cout<<endl;
return 0;
}
bool greater9( int value )
{
return value > 9;
}
void outputSquare( int value )
{
cout<<value * value<< ' ';
}
int calculateCube( int value )
{
return value*value*value;
}
相关文章推荐
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL 之count,count_if,max,max_element,min,min_element和random_shuffle
- STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL fill()填充算法以及for_each()算法 取容器中的最大最小值min_element,max_element
- 记录 C++ STL 中 一些好用的函数--持续更新 (for_each,transform,count_if,find_if)
- 从零开始学C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- C++之STL(五):非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL 实践(for_each() getline sort random_shuffle的使用)
- STL之for_each, find_if,adjacent_find,count_if的使用
- 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
- STL常用遍历算法for_each和transform的比较
- STL 之for_each,transform
- STL算法(19)——for_each()和transform()的比较
- STL中的两个算法for_each与transform
- C++ STL之max_element()/min_element()
- STL常用遍历算法for_each和transform的比较
- STL之for_each, cout, cout_if, find, find_if
- Effectiv STL 37 Use accumulate or for_each to summarize ranges