#include
|
Container Class
|
<deque>
|
deque
|
<list>
|
list
|
<map>
|
map, multimap
|
<queue>
|
queue, priority_queue
|
<set>
|
set, multiset
|
<stack>
|
stack
|
<vector>
|
vector, vector<bool>
|
#include <iostream.h>
#include <algorithm>
using namespace std;
#define SIZE 100
int iarray[SIZE];
int main()
{
iarray[20] = 50;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip == iarray + SIZE)
cout << "50 not found in array" << endl;
else
cout << *ip << " found in array" << endl;
return 0;
}
iarray[20] = 50;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip == iarray + SIZE) ...
cout << *ip << " found in array" << endl;
int* ip = find(iarray, iarray + SIZE, 50);
if (ip != NULL) ... // ??? incorrect, because (iarray+SIZE) is not equal to NULL.
#include <iostream.h>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> intVector(100);
void main()
{
intVector[20] = 50;
vector<int>::iterator intIter =
find(intVector.begin(), intVector.end(), 50);
if (intIter != intVector.end())
cout << "Vector contains value " << *intIter << endl;
else
cout << "Vector does not contain 50" << endl;
}
cout << "Vector contains value " << *intIter << endl;
vector<int>::iterator first;
first = intVector.begin();
*first = 123;
const vector<int>::iterator result;
result = find(intVector.begin(), intVector.end(), value);//查找某个值
if (result != intVector.end())
*result = 123; // ??? 我测试过,这样也可以,能通过,只是*++result = 123;就要报错了!!!
template <class InputIterator, class T>
InputIterator find(
InputIterator first, InputIterator last, const T& value) {
while (first != last && *first != value) ++first;
return first;
}
#include <iostream.h>
#include <algorithm> // Need copy()
#include <vector> // Need vector
using namespace std;
double darray[10] =
{1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9};
vector<double> vdouble(10);
int main()
{
vector<double>::iterator outputIterator = vdouble.begin();
copy(darray, darray + 10, outputIterator);
while (outputIterator != vdouble.end()) {
cout << *outputIterator << endl;
outputIterator++;
}
return 0;
}
template <class ForwardIterator, class T>
void replace (ForwardIterator first,
ForwardIterator last,
const T& old_value,
const T& new_value);
replace(vdouble.begin(), vdouble.end(), 1.5, 3.14159);//查找替换
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first,
BidirectionalIterator last);
reverse(vdouble.begin(), vdouble.end());//逆向排序
template <class RandomAccessIterator>
void random_shuffle (RandomAccessIterator first,
RandomAccessIterator last);
random_shuffle(vdouble.begin(), vdouble.end());//随意打乱顺序
int value;
cout << "Enter value: ";
cin >> value;
cout << "You entered " << value << endl;
#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()
#include <time.h> // Need time()
#include <algorithm> // Need sort(), copy()
#include <vector> // Need vector
using namespace std;
void Display(vector<int>& v, const char* s);
int main()
{
// Seed the random number generator
srandom( time(NULL) );
// Construct vector and fill with random integer values
vector<int> collection(10);
for (int i = 0; i < 10; i++)
collection[i] = random() % 10000;;
// Display, sort, and redisplay
Display(collection, "Before sorting");
sort(collection.begin(), collection.end());//排序
Display(collection, "After sorting");
return 0;
}
// Display label s and contents of integer vector v
void Display(vector<int>& v, const char* s)
{
cout << endl << s << endl;
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, "/t"));
cout << endl;
}
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, "/t")); //输出流迭代器 copy用法
$ g++ outstrm.cpp
$ ./a.out
Before sorting
677 722 686 238 964 397 251 118 11 312
After sorting
11 118 238 251 312 397 677 686 722 964
ostream_iterator<int>(cout, "/n")
list<double> dList;
vector<double> dVector;
copy(dVector.begin(), dVector.end(), front_inserter(dList)); //插入迭代器 copy用法
#include <iostream.h>
#include <algorithm>
#include <list>
using namespace std;
int iArray[5] = { 1, 2, 3, 4, 5 };
void Display(list<int>& v, const char* s);
int main()
{
list<int> iList;
// Copy iArray backwards into iList
copy(iArray, iArray + 5, front_inserter(iList)); //front_inserter迭代器的copy插入
Display(iList, "Before find and copy");
// Locate value 3 in iList
list<int>::iterator p =
find(iList.begin(), iList.end(), 3);
// Copy first two iArray values to iList ahead of p
copy(iArray, iArray + 2, inserter(iList, p)); //inserter迭代器的copy插入
Display(iList, "After find and copy");
return 0;
}
void Display(list<int>& a, const char* s)
{
cout << s << endl;
copy(a.begin(), a.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
}
$ g++ insert.cpp
$ ./a.out
Before find and copy
5 4 3 2 1
After find and copy
5 4 1 2 3 2 1
list<int> iList;
list<int>::iterator p =
find(iList.begin(), iList.end(), 2);
cout << "before: p == " << *p << endl;
advance(p, 2); // same as p = p + 2;
cout << "after : p == " << *p << endl;
int k = 0; //初始为零
distance(p, iList.end(), k);
cout << "k == " << k << endl;
使用 distance()函数来返回到达另一个迭代器所需要的步骤。
#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()
#include <time.h> // Need time()
#include <vector> // Need vector
#include <algorithm> // Need for_each()
#define VSIZE 24 // Size of vector
vector<long> v(VSIZE); // Vector object
// Function prototypes
void initialize(long &ri);
void show(const long &ri);
bool isMinus(const long &ri); // Predicate function
int main()
{
srandom( time(NULL) ); // Seed random generator
for_each(v.begin(), v.end(), initialize);//for_each 传递回调initialize
cout << "Vector of signed long integers" << endl;
for_each(v.begin(), v.end(), show);
cout << endl;
// Use predicate function to count negative values
//
int count = 0;
vector<long>::iterator p;
p = find_if(v.begin(), v.end(), isMinus);//find_if 传递回调函数-断言isMinus
while (p != v.end()) {
count++;
p = find_if(p + 1, v.end(), isMinus); // 循环调用
}
cout << "Number of values: " << VSIZE << endl;
cout << "Negative values : " << count << endl;
return 0;
}
// Set ri to a signed integer value
void initialize(long &ri)
{
ri = ( random() - (RAND_MAX / 2) );
// ri = random();
}
// Display value of ri
void show(const long &ri)
{
cout << ri << " ";
}
// Returns true if ri is less than 0
bool isMinus(const long &ri)
{
return (ri < 0);
}
TAnyClass object; // Construct object
object(); // Calls TAnyClass::operator()() function
for_each(v.begin(), v.end(), object);// Calls TAnyClass::operator()() function
#include <iostream.h>
#include <numeric> // Need accumulate()
#include <vector> // Need vector
#include <functional> // Need multiplies() (or times())
#define MAX 10
vector<long> v(MAX); // Vector object
int main()
{
// Fill vector using conventional loop
//
for (int i = 0; i < MAX; i++)
v[i] = i + 1;
// Accumulate the sum of contained values
//
long sum =
accumulate(v.begin(), v.end(), 0); //accumulate累加
cout << "Sum of values == " << sum << endl;
// Accumulate the product of contained values
//
long product =
accumulate(v.begin(), v.end(), 1, multiplies<long>());//注意这行 accumulate连乘
cout << "Product of values == " << product << endl;
return 0;
}
$ g++ accum.cpp
$ ./a.out
Sum of values == 55
Product of values == 3628800
另加一例:
#include<algorithm>
#include<numeric>
#include<vector>
#include<iostream>
using namespace std;
//成绩及格时返回true,否则返回false
bool __pass(int n){ return(n>=60);}//成绩及格时返回true,否则返回false
bool __no_pass(int n){ return(n<60);}
void main()
{
int n[]={10,20,30,40,50,60,70,80,90};
int total;
vector<int>v(n,n+sizeof(n)/sizeof(int));
vector<int>::iterator __i;
__i=min_element(v.begin(),v.end()); //min_element返回最小值的迭代器
cout<<"最小值:"<<*__i<<"."<<endl;
__i=max_element(v.begin(),v.end()); //max_element返回最大值的迭代器
cout<<"最大值:"<<*__i<<"."<<endl;
cout<<"通过:"<<count_if(v.begin(),v.end(),__pass)<<endl; //count_if统计符合回调函数的元素个数
cout<<"没通过:"<<count_if(v.begin(),v.end(),__no_pass)<<endl;
total=accumulate(v.begin(),v.end(),0); //accumulate累加指定迭代器区间内所有元素
cout<<"平均"<<(total/(int)(v.size()))<<endl;
}
#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()
#include <time.h> // Need time()
#include <algorithm> // Need random_shuffle()
#include <vector> // Need vector
#include <functional> // Need ptr_fun()
using namespace std;
// Data to randomize
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> v(iarray, iarray + 10);
// Function prototypes
void Display(vector<int>& vr, const char *s);
unsigned int RandInt(const unsigned int n);
int main()
{
srandom( time(NULL) ); // Seed random generator
Display(v, "Before shuffle:");
pointer_to_unary_function<unsigned int, unsigned int>
ptr_RandInt = ptr_fun(RandInt); // Pointer to RandInt()//注意这行
random_shuffle(v.begin(), v.end(), ptr_RandInt); //打乱顺序random_shuffle 回调为函数对象
Display(v, "After shuffle:");
return 0;
}
// Display contents of vector vr
void Display(vector<int>& vr, const char *s)
{
cout << endl << s << endl;
copy(vr.begin(), vr.end(), ostream_iterator<int>(cout, " ")); //copy回调输出流迭代器(仅用于记忆)
cout << endl;
}
// Return next random value in sequence modulo n
unsigned int RandInt(const unsigned int n)
{
return random() % n;
}
$ g++ randfunc.cpp
$ ./a.out
Before shuffle:
1 2 3 4 5 6 7 8 9 10
After shuffle:
6 7 2 8 3 5 10 1 9 4
pointer_to_unary_function<unsigned int, unsigned int> //声明一个函数对象 ??能是单目函数模板吗??
ptr_RandInt = ptr_fun(RandInt); //辅助函数ptr_fun 将全局函数转换成函数对象
random_shuffle(v.begin(), v.end(), ptr_RandInt);
在本例子中,发生器只是简单的调用rand()函数。
#include <iostream.h>
#include <algorithm> // Need random_shuffle()
#include <vector> // Need vector
#include <functional> // Need unary_function
using namespace std;
// Data to randomize
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<int> v(iarray, iarray + 10);
// Function prototype
void Display(vector<int>& vr, const char *s);
// The FiboRand template function-object class
template <class Arg>
class FiboRand : public unary_function<Arg, Arg> {
int i, j;
Arg sequence[18];
public:
FiboRand();
Arg operator()(const Arg& arg);
};
void main()
{
FiboRand<int> fibogen; // Construct generator object
cout << "Fibonacci random number generator" << endl;
cout << "using random_shuffle and a function object" << endl;
Display(v, "Before shuffle:");
random_shuffle(v.begin(), v.end(), fibogen);
Display(v, "After shuffle:");
}
// Display contents of vector vr
void Display(vector<int>& vr, const char *s)
{
cout << endl << s << endl;
copy(vr.begin(), vr.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
}
// FiboRand class constructor
template<class Arg>
FiboRand<Arg>::FiboRand()
{
sequence[17] = 1;
sequence[16] = 2;
for (int n = 15; n > 0; n—)
sequence[n] = sequence[n + 1] + sequence[n + 2];
i = 17;
j = 5;
}
// FiboRand class function operator
template<class Arg>
Arg FiboRand<Arg>::operator()(const Arg& arg)
{
Arg k = sequence[i] + sequence[j];
sequence[i] = k;
i--;
j--;
if (i == 0) i = 17;
if (j == 0) j = 17;
return k % arg;
}
$ g++ fiborand.cpp
$ ./a.out
Fibonacci random number generator
using random_shuffle and a function object
Before shuffle:
1 2 3 4 5 6 7 8 9 10
After shuffle:
6 8 5 4 3 7 10 1 9
template <class Arg>
class FiboRand : public unary_function<Arg, Arg> {...
#include <iostream.h>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;
// Data
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<int> aList(iarray, iarray + 10);
int main()
{
int k = 0;
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k); //bind1st将参数8作为函数greater的第一个参数,那么迭代值为第二个,即8>element 或 element<8;
//bind1st、bind2nd实现回调函数的参数传递
cout << "Number elements < 8 == " << k << endl;
return 0;
}
bind1st(greater<int>(), 8)
8 > q
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k);
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k);
start = find_if(aList.begin(), aList.end(),
not1(bind1st(greater<int>(), 6)));
另外还有例子:
int a[] = {1, 2, 100, 200};
std::vector< int> arr(a, a + 4);
// 移除所有小于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::bind2nd( std::less< int>(), 100)), arr.end());
这里的比较表达式相当于arr.value < 100
如果用bind1st则表达的意思就恰恰相反
// 移除所有大于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::bind1st( std::less< int>(), 100)), arr.end());
这里的表达式相当于100 < arr.value
当然为了实现删除大于100的元素你同样可以使用bind2nd
// 移除所有大于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::bind2nd( std::greater< int>(), 100)), arr.end());
前面说道=的比较,比如说x <= k怎么实现呢,std又提供了一个好东西not1,我们可以说 !(x > k) 和 x <= k是等价的,那么我们看看下面的表达式:
// 移除所有小于等于100的元素
arr.erase( std::remove_if( arr.begin(), arr.end(),
std::not1(std::bind2nd( std::greater< int>(), 100))), arr.end());
说明:not1是否定返回值是单目的函数,std中还有not2它是否定返回值是双目的函数