From http://cppreference.com/
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
/* count_if */
void count_if() {
int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 };
int start = 0;
int end = 9;
int target_value = 3;
int num_items = std::count_if( nums+start,
nums+end,
std::bind2nd(std::equal_to<int>(), target_value) );
std::cout << " nums[] contains " << num_items << " items matching " << target_value << std::endl;
}
/* accumulate */
void accumulate() {
int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 };
int start = 0;
int end = 9;
int target_value = 3;
int num_items = std::accumulate( nums+start,
nums+end,
target_value);
std::cout << " accumulate result: " << num_items << std::endl;
}
/* adjacent_difference */
void adjacent_difference() {
int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13, 0 };
int start = 3;
int end = 4;
int result = 0;
std::adjacent_difference( nums+start,
nums+end,
&result);
std::cout << " adjacent_difference: result: " << result << std::endl;
}
/* adjacent find */
void adjacent_find() {
std::vector<int> v1;
for( int i = 0; i < 10; i++ ) {
v1.push_back(i);
// add a duplicate 7 into v1
if( i == 7 ) {
v1.push_back (i);
}
}
std::vector<int>::iterator result;
result = std::adjacent_find( v1.begin(), v1.end() );
if( result == v1.end() ) {
std::cout << " Did not find adjacent elements in v1" << std::endl;
} else {
std::cout << " Found matching adjacent elements starting at " << *result << std::endl;
}
}
/* binary_search
Note: list has to be in order beforehand for it to work
*/
void binary_search()
{
int nums[] = { -242, -1, 0, 5, 8, 9, 11 };
int start = 0;
int end = 7;
for( int i = 0; i < 10; i++ ) {
if( std::binary_search( nums+start, nums+end, i ) ) {
std::cout << " nums[] contains " << i << std::endl;
} else {
std::cout << " nums[] DOES NOT contain " << i << std::endl;
}
}
}
/* copy */
void copy()
{
std::vector<int> from_vector;
for( int i = 0; i < 10; i++ ) {
from_vector.push_back( i );
}
std::vector<int> to_vector(10);
std::copy( from_vector.begin(), from_vector.end(), to_vector.begin() );
std::cout << "to_vector contains: ";
for( unsigned int i = 0; i < to_vector.size(); i++ ) {
std::cout << to_vector[i] << " ";
}
std::cout << std::endl;
}
/* copy_backward */
void copy_backward()
{
std::vector<int> from_vector;
for( int i = 0; i < 10; i++ ) {
from_vector.push_back( i );
}
std::vector<int> to_vector(15);
std::copy_backward( from_vector.begin(), from_vector.end(), to_vector.end() );
std::cout << "to_vector contains: ";
for( unsigned int i = 0; i < to_vector.size(); i++ ) {
std::cout << to_vector[i] << " ";
}
std::cout << std::endl;
}
int main ()
{
count_if();
accumulate();
adjacent_difference();
adjacent_find();
binary_search();
copy();
copy_backward();
return 0;
}
All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...
Subscribe to:
Post Comments (Atom)
tim's shared items
Blog Archive
-
▼
2007
(118)
-
▼
November
(46)
- Universal Digital Library
- c++ qsort, threaded qsort and shell sort
- Only the Paranoid Survive
- Using pictures as a captcha
- BZFlag - Multiplayer 3D Tank Game
- Bugzilla Addons
- Infrarecorder free CD/DVD burning
- Alfresco & Jaspersoft - content management and bus...
- Open-source software rated: Ten alternatives you need
- C# programming
- Hash functions
- Userfriendly comic strip
- Password MD5 cracking
- SOAP financial news
- E8 (mathematics)
- Google AJAX Feed + Firefox extensions => Piggy Ban...
- PyQT4 Python GUI programming
- Yahoo developer series - movies
- Map reduce open source style
- Linux changes to stop wearing down the harddisk
- Miro - watch free internet video channels and play...
- A Practical Introduction to GNU Privacy Guard in W...
- Exetel SMS API
- Money Bookers Automatic Payments
- Find current version on Nokia phone
- Google Gphone - Android - An Open Handset Alliance...
- Sports Arbitrage Betting program
- PostgreSQL Git repository
- Store encrypted passwords in PostgreSQL
- Visual studio build script
- CD DVD burning and other cool applications
- Javascript Plotting
- Tango icon library
- Drop IO
- Google mapplets
- Add any page to iGoogle
- 2007 Restaurant Winners SMH
- Cool CSS for a webpage
- Regular expressions with C on Linux
- c++ count_if example
- C++ algorithm examples
- The Curse of Xanadu
- Ten Tips for a (Slightly) Less Awful Resume
- Execution in the Kingdom of Nouns
- Comparison of different SQL implementations
- Anyterm - A Terminal Anywhere
-
▼
November
(46)
No comments:
Post a Comment