算法函数练习

find:

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

int main()
{
	int a[10]={0,1,2,3,4,5,6,7,8,9};
	size_t a_size = sizeof(a)/sizeof(int);
	list<int> li(a, a+a_size);
	list<int>::iterator itor1, itor2;

	for(itor1 = li.begin(); itor1 != li.end(); ++itor1)//打印:0 1 2 3 4 5 6 7 8 9
		cout << *itor1 << "	";
	cout << endl;

	itor1 = find(li.begin(), li.end(), 5);
	itor2 = find(li.begin(), li.end(), 9);
	li.erase(itor1, itor2);//删除的区间为:[itor1, itor2)-----左闭右开

	for(itor1 = li.begin(); itor1 != li.end(); ++itor1)//打印:0 1 2 3 4 9
		cout << *itor1 << "	";
	cout << endl;
	return 0;
}

  count

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>  //count 在此定义
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     vector<int> vec;
10     for(vector<int>::size_type cnt1 = 0; cnt1 != 10; ++cnt1)
11     {
12         for(vector<int>::size_type cnt2 = 0; cnt2 != cnt1; ++cnt2)
13         {
14             vec.push_back(cnt2);
15         }
16     }
17     int val;
18     while(cin >> val)
19     {
20         cout << val << " 在vector中出现了 " << count(vec.begin(), vec.end(), val) << " 次。" << endl;
21     }
22     return 0;
23 }

find_first_of

 1 /**
 2   * find_first_of查找算法:这个算法带有两对迭代器参数来标记两段元素范围,在第一段范围内
 3   * 查找与第二段范围中 任意 元素匹配的元素,然后返回一个迭代器,指向第一个匹配的元素,
 4   * 如果找不到匹配元素,则返回第一个范围的end迭代器。
 5   */
 6 
 7 /**
 8   *示例:假设list1 和 list2 是两个存放名字的 list 对象,可使用find_first_of 统计
 9   *有多少个名字同时出现在这两个列表当中
10   *list1 和  list2类型不必精确匹配;list1可以是list对象,list2可以是vector对象、deque对象,只要
11   *可以进行 == 操作符进行比较即可。如果list1 是 list<string> 对象,则 list2 可以是vector<char *>对象,
12   *因为string标准库为string标准库为string对象与char*对象定义了相等(==)操作符。
13   */
14 #include <iostream>
15 #include <algorithm>
16 #include <list>
17 
18 using namespace std;
19 
20 int main()
21 {
22     list<string> list1;
23     list<string> list2;
24     
25     list1.push_back("Jack");
26     list1.push_back("Ben");
27     list1.push_back("Bob");
28     list1.push_back("Marry");
29     
30     list2.push_back("Jack");
31     list2.push_back("Lucy");
32     list2.push_back("Andy");
33     list2.push_back("Bob");
34     
35     size_t cnt = 0;
36     list<string>::iterator it = list1.begin();
37     while((it = find_first_of(it, list1.end(), list2.begin(), list2.end())) != list1.end())
38     {
39         ++cnt;
40         ++it;
41     }
42     cout << "list1 和 list2 中公有的名字一共有 " << cnt << " 个." << endl;
43     system("pause");
44     return 0;
45 }

 erase

  1 #include <iostream>
  2 #include <list>
  3 #include <vector>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 int main()
  9 {
 10     int a[10]={0,1,2,3,4,5,6,7,8,9};
 11     size_t a_size = sizeof(a)/sizeof(int);
 12 
 13     list<int> li(a, a+a_size);
 14     cout << "初始list中元素为:" << endl;
 15     for(list<int>::iterator itor = li.begin(); itor != li.end(); ++itor)
 16     {
 17         cout << *itor << "	";
 18     }
 19     cout << endl;
 20 
 21     vector<int> ve(a, a+a_size);
 22     cout << "初始vector中的元素为" << endl;
 23     for(vector<int>::iterator itor = ve.begin(); itor != ve.end(); ++itor)
 24     {
 25         cout << *itor << "	";
 26     }
 27     cout << endl;
 28 
 29     for(list<int>::iterator itor = li.begin(); itor != li.end(); ++itor)
 30     {
 31         if((*itor) & 1)
 32         {
 33             //li.erase(itor);注意这样写是错误的,不能达到预期效果,引文每次erase之后的迭代器会失效,所以要重新赋值
 34             itor = li.erase(itor);  //erase返回指向删除迭代器itor所指元素之后的下一个元素的迭代器,
 35                                     //如果itor已经指向超出末端的下一位置的迭代器,则返回的迭代器也指向容器超出末端的下一位置
 36         }
 37     }
 38     for(vector<int>::iterator itor = ve.begin(); itor != ve.end(); ++itor)
 39     {
 40         if(!((*itor) & 1))
 41         {
 42             itor = ve.erase(itor);
 43         }
 44     }
 45 
 46 
 47     cout << "删除奇数后list中元素为:" << endl;
 48     for(list<int>::iterator itor = li.begin(); itor != li.end(); ++itor)
 49     {
 50         cout << *itor << "	";
 51     }
 52     cout << endl;
 53     cout << "删除偶数后vector中元素为:" << endl;
 54     for(vector<int>::iterator itor = ve.begin(); itor != ve.end(); ++itor)
 55     {
 56         cout << *itor << "	";
 57     }
 58     cout << endl;
 59     system("pause");
 60     return 0;
 61 }
 62 
 63 /*
 64 #include <iostream>
 65 #include <list>
 66 #include <vector>
 67 using namespace std;
 68 
 69 int main(int argc, char* argv[])
 70 {
 71     int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
 72     vector<int> iVec( ia, ia+11 );
 73     list<int> iLst( ia, ia+11 );
 74 
 75     cout << "  Before erase, the elements of iVec are:" << endl;
 76     for ( vector<int>::iterator it = iVec.begin(); it != iVec.end(); ++it )
 77         cout << *it << " ";
 78     cout << "
  Before erase, the elements of iLst are:" << endl;
 79     for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
 80         cout << *it << " ";
 81 
 82     // 2 erase
 83     for ( vector<int>::iterator iter = iVec.begin(); iter != iVec.end(); ++iter )
 84     {
 85         if ( *iter % 2 == 0 )
 86         {
 87             iter = iVec.erase( iter );
 88         }
 89     }
 90     for ( list<int>::iterator Lit = iLst.begin(); Lit != iLst.end(); ++Lit )
 91     {
 92         if ( *Lit % 2 == 1 )
 93         {
 94             Lit = iLst.erase( Lit );
 95             --Lit;
 96         }
 97     }
 98 
 99 
100     // show
101     cout << "
  After erase, the elements of iVec are:" << endl;
102     for ( vector<int>::iterator it = iVec.begin(); it != iVec.end(); ++it )
103         cout << *it << " ";
104     cout << "
  After erase, the elements of iLst are:" << endl;
105     for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
106         cout << *it << " ";
107     cout << endl;
108 
109     system("pause");
110     return 0;
111 }
112 */

 accumulate

 1 /**
 2   *   accumulate 算法在numeric头文件中定义,accumulate有带有三个形参,头两个形参指定要累加的元素范围,第三个形参
 3   *   则是累加的初值。accumulate 将它的一个内部变量设置为指定的初值,然后在此初值上累加输入范围内所有元素的值。
 4   *   accumulate 算法返回累加的结果,其返回类型就是第三个实参的类型
 5   *   accumulate 对要累加的元素类型一无所知,必须根据第三个参数来确定,所以第三个实参不能少
 6   */
 7 
 8 /*
 9 示例一:用accumulate统计vector<int>容器对象中的元素之和
10 #include <iostream>
11 #include <vector>
12 #include <numeric>  //accumulate 在此定义
13 
14 using namespace std;
15 
16 int main()
17 {
18     vector<int> vec;
19     for(vector<int>::size_type cnt = 0; cnt != 101; ++cnt)
20         vec.push_back(cnt);
21     cout << "0到100的和为:" << accumulate(vec.begin(), vec.end(), 0) << endl;
22     system("pause");
23     return 0;
24 }
25 */
26 //示例二:用 accumulate 把string类型的vector容器中的元素连接起来:
27 #include <iostream>
28 #include <vector>
29 #include <numeric>
30 
31 using namespace std;
32 
33 int main()
34 {
35     vector<string> vec;
36     string str;
37     while(cin >> str)
38         vec.push_back(str);
39     cout << "连接后的字符串为" << accumulate(vec.begin(), vec.end(), string("")) << endl;
40     /**
41       *注意程序显示的创建了一个string对象,用作该函数调用的第三个实参。传递一个字符串字面值将会导致编译时错误。
42       *因为此时累加和的类型将是 const char*类型,而string的加法操作符所使用的操作数则分别是string和const char*类型,
43       *加法的结果将产生一个string对象,而不是const char*指针
44     */
45     system("pause");
46     return 0;
47 }

 写容器元素的算法

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <iterator>
 4 #include <list>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     list<int> li;
12     list<int>::iterator itor;
13     for(list<int>::size_type cnt = 0; cnt != 10; ++cnt)
14         li.push_back(cnt);
15     for(itor = li.begin(); itor != li.end(); ++itor)
16         cout << *itor << "  ";
17     cout << endl;
18     
19     // back_inserter是插入迭代器,是可以给基础容器添加元素的迭代器
20     fill_n(back_inserter(li), 10, 0);  //在原来li序列的后边插入10个0
21     
22     for(itor = li.begin(); itor != li.end(); ++itor)
23         cout << *itor << "  ";
24     cout << endl;
25     
26     fill(li.begin(), li.end(), 0);     //把li列表中的值全赋值成 0
27     for(itor = li.begin(); itor != li.end(); ++itor)
28         cout << *itor << "  ";
29     cout << endl;
30     
31     vector<int> ivec;
32     // copy elements from li into ivec
33     copy(li.begin(), li.end(), back_inserter(ivec));
34     //该做法效率比较差,相当于vector<int> ivec(li.begin(), li.end());
35     
36     //replace any elements with value of 0 by 42
37     replace(li.begin(), li.end(), 0, 42);//把li中所有0变成42
38     
39     //create empty vector to hold the replacement
40     vector<int> vec;//li中的元素不变,把li中的元素复制到vec中,并且把所有为0的元素改成42
41     replace_copy(li.begin(), li.end(), back_inserter(vec), 0, 42);
42     
43     for(itor = li.begin(); itor != li.end(); ++itor)
44         cout << *itor << "  ";
45     cout << endl;
46     
47     for(vector<int>::iterator itor1 = ivec.begin(); itor1 != ivec.end(); ++itor1)
48         cout << *itor1 << "  ";
49     cout << endl;
50     for(vector<int>::iterator itor1 = vec.begin(); itor1 != vec.end(); ++itor1)
51         cout << *itor1 << "  ";
52     cout << endl;
53     
54     
55     system("pause");
56     return 0;
57 }

 对容器元素重新排序的算法

 1 /**
 2   * 功能:对一个存储了string类型单词的vector容器,计算该容器中有多少个由六位或以上字母组成的单词
 3   *每个单词只统计一次,不考虑他出现的次数,要求以长度从小到大输出,长度相同的按照字典序从小到大输出
 4   * 步骤:1、字典排序;  2、去掉所有重复的单词;  3、按照单词长度排序  4、统计长度等于后超过6个字符的单词个数
 5   */
 6 #include <iostream>
 7 #include <vector>
 8 #include <algorithm>
 9 
10 using namespace std;
11 
12 bool isShorter(const string &s1, const string &s2)  //从小到大排序
13 {
14     return s1.size() < s2.size();
15 }
16 
17 bool GT6(const string s)
18 {
19     return s.size() >= 6;
20 }
21 
22 string make_plural(size_t ctr, const string &word, const string &ending)
23 {
24     return (ctr == 1) ? word : word + ending;
25 }
26 int main()
27 {
28     vector<string> words;
29     
30     string next_word;
31     while(cin >> next_word)
32     {
33         words.push_back(next_word);
34     }
35     //sort(words.begin(), words.end());  //字典序排列
36     vector<string>::iterator unique_end = unique(words.begin(), words.end());//
37     //unique算法,删除  相邻   的   重复  元素(并非真的删除,words的大小没有变化)然后重新排列输入范围内的元素
38     //并且返回一个迭代器,表示无重复的值范围的结束;unique函数只是将无重复的元素复制到序列的前端,
39     //返回的迭代器指向超出无重复元素范围的下一个位置.
40     
41     words.erase(unique_end, words.end());
42     stable_sort(words.begin(), words.end(), isShorter);//长度按照从大到小的稳定排序,所以长度相同的仍然按照的是字典序排列
43     vector<string>::size_type cnt = count_if(words.begin(), words.end(), GT6);
44 
45     cout << cnt << " " << make_plural(cnt, "word", "s") << " 6 characters or longer" << endl;
46     for (vector<string>::iterator it = words.begin();  it != words.end(); ++it)
47         cout << *it << "  ";
48     cout << endl;
49 
50     system("pause");
51     return 0;
52 }

 unique_copy

 1 //unique_copy用于指定复制不重复的元素到目标序列
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <list>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11 
12     int ia[] = { 1, 2, 3, 4, 100, 100, 5 };
13     list<int> iLst( ia, ia+7 );
14     // testing out iLst
15     cout << "
	The contents of iLst:
	";
16     for ( list<int>::iterator it = iLst.begin(); it != iLst.end(); ++it )
17         cout << *it << "  ";
18     cout << endl;
19 
20     vector<int> iVec;
21     // copy iLst's member to iVec;
22     cout << "
  Using unique_copy, copy iLst's member to iVec: " << endl;
23     unique_copy( iLst.begin(), iLst.end(), back_inserter( iVec) );
24 
25     cout << "
	The contents of iVec:
	";
26     for ( vector<int>::iterator it = iVec.begin(); it != iVec.end(); ++it )
27         cout << *it << "  ";
28     cout << endl;
29     system("pause");
30     return 0;
31 }
原文地址:https://www.cnblogs.com/dongsheng/p/3316590.html