STL案例代码

来源:互联网 发布:qq for mac 10.8.5 编辑:程序博客网 时间:2024/05/18 02:54

//1.指针迭代器

#include <iostream>

#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;

 

system("pause");

return 0;

}

 

===========================================

//2.容器迭代器1

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

int main()

{

vector<int> intVector(100);

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;

 

system("pause");

return 0;

}

 

================================================

//3.输出迭代器

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

 

int main()

{

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);

 

vector<double>::iterator outputInterator = vdouble.begin();//定义容器迭代器,

copy(darray, darray + 10, outputInterator);

while (outputInterator != vdouble.end())

{

cout << *outputInterator << endl;

outputInterator++;

}

system("pause");

return 0;

}

 

提示:小伙伴们不要惊讶,这个1.0输出后变成了1,这在DEVVS2013下输出都是一样的。

=========================================================

//4.流迭代器

#include <iostream>

#include <algorithm>

#include <vector>

#include <stdlib.h>

#include <time.h>

#include <iterator>

using namespace std;

 

void Display(vector<int>& v, const char* s)

{

cout << endl << s << endl;

copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));//使用流迭代器,使用“\t”隔开

cout << endl;

}

 

int main()

{

srand(time(NULL));// 用时间初始化随机发生器

vector<int> collection(10);

for (int i = 0; i < 10; i++)

collection[i] = rand() % 1000;  // 产生的随机数用1000 模运算

Display(collection, "Before sorting");//排序前输出

sort(collection.begin(), collection.end());//对容器进行排序

Display(collection, "After sorting");//排序后输出

system("pause");

return 0;

}

 

===========================================================

//5.插入代器

#include <iostream>

#include <algorithm>

#include <list>

#include <iterator>

using namespace std;

 

void Display(list<int>& a, const char* s)

{

cout << s << endl;

copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));

cout << endl;

}

 

int main()

{

int iArray[5] = { 1, 2, 3, 4, 5 };

list<int> iList;

//使用copy函数把数组中值复制到STL容器,复制的时候利用前端插入,插入后,再在前面插入

// 若插入 1,再插入2,就变成了2,1

copy(iArray, iArray + 5, front_inserter(iList));

Display(iList, "Before find and copy ");

 

// 定义list容器迭代器,找到3的位置初始化迭代器,当前list容器的迭代器是(5,4,3,2,1)

list<int>::iterator p = find(iList.begin(), iList.end(), 3);

 

// 将数组的前两个元素插入到容器,插入位置为之前找到3的位置之前

copy(iArray, iArray + 2, inserter(iList, p));

Display(iList, "After find and copy");

 

system("pause");

return 0;

}

 

=======================================================

// 6. 函数和断言

#include <iostream>

#include <stdlib.h>

#include <stdio.h>

#include <time.h>

#include <vector>

#include <algorithm>

using namespace std;

 

#define VSIZE 10

vector<long> v(VSIZE);

 

void initialize(long &ri);

void show(const long &ri);

bool isMinus(const long &ri);

 

int main()

{

srand(time(NULL));// 初始化随机发生器种子

 

for_each(v.begin(), v.end(), initialize); //遍历容器,调用普通函数赋值操作

cout << "Vector of signal long itergers" << endl;

for_each(v.begin(), v.end(), show); // 编译容器,并显示

cout << endl;

 

int count = 0;

vector<long>::iterator p;

// 调用断言函数,如果容器中存在小于0的数,则返回第一个小于0 的迭代器

= find_if(v.begin(), v.end(), isMinus);

//cout << *p << endl;

// 统计小于0 的数的个数

while (!= v.end())

{

count++;

= find_if(+ 1, v.end(), isMinus);

}

cout << "Number of values:" << VSIZE << endl;

cout << "Negative valuses:" << count << endl;

 

system("pause");

return 0;

}

 

// 利用随机数获得值

void initialize(long &ri)

{

ri = (rand() - (RAND_MAX / 2));

}

 

// 显示

void show(const long &ri)

{

cout << ri << " ";

}

 

// 判断小于0的数,返回为真

bool isMinus(const long &ri)

{

return (ri < 0);

}

 

==========================================================

// 7. 函数对象

#include <iostream>

#include <numeric>

#include <vector>

#include <functional>

 

using namespace std;

 

#define MAX 10

vector<long> v(MAX);

 

int main()

{

//初始化容器容器中的元素

for (int i = 0; i < 10; i++)

v[i] = i + 1;

// accumulate函数对容器内元素求和,头文件numeric

long sum = accumulate(v.begin(), v.end(), 0);

cout << "Sum of values ==" << sum << endl;

 

//  函数模板acumulate利用multiplies仿函数(头文件functional)对容器内元素进行连续相乘操作

long product = accumulate(v.begin(), v.end(),1, multiplies<long>());

cout << "Product of values ==" << product << endl;

 

system("pause");

return 0;

}

 

===================================================

// 8.发生器函数对象

#include <iostream>

#include <stdlib.h>

#include <time.h>

#include <algorithm>

#include <functional>

#include <vector>

#include<iterator>

using namespace std;

 

void Display(vector<int>& vr, const char* s);

unsigned int RandInt(const unsigned int n);

 

int main()

{

int iarray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

vector<int> v(iarray, iarray + 10);

 

srand(time(NULL));

Display(v, "Before shuffle:");

 

//Converts a unary function pointer into an adaptable unary function.

pointer_to_unary_function<unsigned int, unsigned int> ptr_RandInt =

ptr_fun(RandInt);

//对一个元素序列进行重新排序(随机的),包含在头文件 algorithm.h中

random_shuffle(v.begin(), v.end(), ptr_RandInt);

Display(v,"After shuffle:");

system("pause");

return 0;

}

 

/// 显示

void Display(vector<int>& vr, const char* s)

{

cout << endl << s << endl;

copy(vr.begin(), vr.end(), ostream_iterator<int>(cout, " "));

cout << endl;

}

 

//返回随机值

unsigned int RandInt(const unsigned int n)

{

return rand() % n;

}

 

=============================================

//9. 发生器函数对象

#include <iostream>

#include <algorithm>

#include <vector>

#include <iterator>

#include <functional>

using namespace std;

 

/*

FiboRand模板函数对象类,继承自unary_function<Arg,Arg>

该类定义了两个成员函数,一个是构造函数,另一是operator()()函数,

该操作符允许random_shuffle()算法像一个函数一样“调用"一个FiboRand对象。

*/

template <typename Arg>

class FiboRand :public unary_function<Arg, Arg>

{

int i, j;

Arg sequence[18];

public:

FiboRand();

Arg operator()(const Arg& arg);

};

 

 

template<typename Arg>

FiboRand<Arg>::FiboRand()

{

sequence[17] = 1;

sequence[16] = 2;

for (int n = 15; n >= 0; n--)

sequence[n] = sequence[+ 1] + sequence[+ 2];

i = 17;

j = 5;

/*

for (i = 0; i < 18; ++i)

cout << sequence[i] << " ";

cout << endl;

*/

}

 

template<typename 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;

}

 

void Display(vector<int>& vr, const char* s)

{

cout << endl << s << endl;

copy(vr.begin(), vr.end(), ostream_iterator<int>(cout, " "));

cout << endl;

}

 

 

int main()

{

int iarray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

vector<int> v(iarray, iarray + 10);

 

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:");

 

system("pause");

return 0;

}

 

===============================================

//10.绑定器函数对象

 

#include <iostream>

#include <algorithm>

#include <functional>

#include <list>

using namespace std;

 

/*

bind1st() 创建一个函数对象,函数对象将值V作为第一个参数A

bind2nd() 创建一个函数对象,函数对象将值V作为第二个参数B

*/

 

int main()

{

int iarray[10] = { 1, 2, 11, 4, 34, 6, 7, 8, 9, 10 };

list<int> aList(iarray, iarray + 10);

 

int k = 0;

/*

 

typename iterator_traits<_InIt>::difference_type

count_if(_InIt _First, _InIt _Last, _Pr _Pred)

{// count elements satisfying _Pred

_DEBUG_RANGE(_First, _Last);

_DEBUG_POINTER(_Pred);

return (_Count_if(_Unchecked(_First), _Unchecked(_Last), _Pred));

}

*/

// count_if() 计算满足特定条件的元素的数目

= count_if(aList.begin(), aList.end(), bind1st(greater<int>(), 8));

cout << "Number elements < 8 ==" << k << endl;

system("pause");

return 0;

}

 

==============================================

// 11.混合迭代器函数

#include <iostream>

#include <list>

#include <iterator>

using namespace std;

 

/*

advance()   按指定的数目增减迭代器

distance()  返回到达一个迭代器所需(递增)操作的数目

*/

 

void Display(list<int>& v, const char* s)

{

cout << endl;

cout << s << endl;

copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));

cout << endl;

}

 

int main()

{

int iArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

list<int> iList;

list<int>::iterator p = iList.begin(); //定义迭代器并指向容器开始

copy(iArray, iArray + 10, inserter(iList, p));//从p开始插入

//copy(iArray, iArray + 10, front_inserter(iList));//前端插入

Display(iList, "Before");

= find(iList.begin(), iList.end(), 2);

cout << "before:p==" << *<< endl;

advance(p, 2);//p+=2

cout << "after:p==" << *<< endl;

 

int k = 0;

= iList.begin();

= distance(p, iList.end());//等价于iList.end()-p,

cout << "k==" << k << endl;

system("pause");

return 0;

}

 


调试平台: VS2013

参考自《三十分钟掌握STL》


1 0
原创粉丝点击