标签:数组类 fun sar 最大 数据类型 产生 组合 读取数据 算术
例如
表示insertionSort这样一个函数模板的原型:
template <class Sortable> void insertionSort(Sortable a[], int n);
标准模板库(Standard Template Library,简称STL)提供了一些非常常用的数据结构和算法
transform算法的一种实现:
template <class InputIterator, class OutputIterator, class UnaryFunction> OutputIterator transform(InputIterator first, InputIterator last, OutputIterator result, UnaryFunction op) { for (;first != last; ++first, ++result) *result = op(*first); return result; }
输入流迭代器
istream_iterator<T>
输出流迭代器
ostream_iterator<T>
二者都属于适配器
//10_2.cpp #include <iterator> #include <iostream> #include <algorithm> using namespace std; //求平方的函数 double square(double x) { return x * x; } int main() { //从标准输入读入若干个实数,分别将它们的平方输出 transform(istream_iterator<double>(cin), istream_iterator<double>(), ostream_iterator<double>(cout, "\t"), square); cout << endl; return 0; }
程序涉及到输入迭代器、输出迭代器、随机访问迭代器这三个迭代器概念,并且以前两个概念为基础编写了一个通用算法。
//10_3.cpp #include <algorithm> #include <iterator> #include <vector> #include <iostream> using namespace std; //将来自输入迭代器的n个T类型的数值排序,将结果通过输出迭代器result输出 template <class T, class InputIterator, class OutputIterator> void mySort(InputIterator first, InputIterator last, OutputIterator result) { //通过输入迭代器将输入数据存入向量容器s中 vector<T> s; for (;first != last; ++first) s.push_back(*first); //对s进行排序,sort函数的参数必须是随机访问迭代器 sort(s.begin(), s.end()); copy(s.begin(), s.end(), result); //将s序列通过输出迭代器输出 } int main() { //将s数组的内容排序后输出 double a[5] = { 1.2, 2.4, 0.8, 3.3, 3.2 }; mySort<double>(a, a + 5, ostream_iterator<double>(cout, " ")); cout << endl; //从标准输入读入若干个整数,将排序后的结果输出 mySort<int>(istream_iterator<int>(cin), istream_iterator<int>(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; } /* 运行结果: 0.8 1.2 2.4 3.2 3.3 2 -4 5 8 -1 3 6 -5 -5 -4 -1 2 3 5 6 8 */
容器的基本功能与分类
容器的分类
容器的通用功能
对可逆容器的访问
随机访问容器
顺序容器
顺序容器的接口(不包含单向链表(forward_list)和数组(array))
例 顺序容器的基本操作
//10_4.cpp #include <iostream> #include <list> #include <deque> //输出指定的顺序容器的元素 template <class T> void printContainer(const char* msg, const T& s) { cout << msg << ": "; copy(s.begin(), s.end(), ostream_iterator<int>(cout, " ")); cout << endl; } int main() { //从标准输入读入10个整数,将它们分别从s的头部加入 deque<int> s; for (int i = 0; i < 10; i++) { int x; cin >> x; s.push_front(x); } printContainer("deque at first", s); //用s容器的内容的逆序构造列表容器l list<int> l(s.rbegin(), s.rend()); printContainer("list at first", l); //将列表容器l的每相邻两个元素顺序颠倒 list<int>::iterator iter = l.begin(); while (iter != l.end()) { int v = *iter; iter = l.erase(iter); l.insert(++iter, v); } printContainer("list at last", l); //用列表容器l的内容给s赋值,将s输出 s.assign(l.begin(), l.end()); printContainer("deque at last", s); return 0; } /* 运行结果如下: 0 9 8 6 4 3 2 1 5 4 deque at first: 4 5 1 2 3 4 6 8 9 0 list at first: 0 9 8 6 4 3 2 1 5 4 list at last: 9 0 6 8 3 4 1 2 4 5 deque at last: 9 0 6 8 3 4 1 2 4 5 /*
例 奇偶排序
先按照从大到小顺序输出奇数,再按照从小到大顺序输出偶数。
// 头部分省略
int main() {
istream_iterator<int> i1(cin), i2; //建立一对输入流迭代器
vector<int> s1(i1, i2); //通过输入流迭代器从标准输入流中输入数据
sort(s1.begin(), s1.end()); //将输入的整数排序
deque<int> s2;
//以下循环遍历s1
for (vector<int>::iterator iter = s1.begin(); iter != s1.end(); ++iter)
{
if (*iter % 2 == 0) //偶数放到s2尾部
s2.push_back(*iter);
else //奇数放到s2首部
s2.push_front(*iter);
}
//将s2的结果输出
copy(s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
接合(splice)操作
s1.splice(p, s2, q1, q2):将s2中[q1, q2)移动到s1中p所指向元素之前
// 头部分省略 int main() { string names1[] = { "Alice", "Helen", "Lucy", "Susan" }; string names2[] = { "Bob", "David", "Levin", "Mike" }; //用names1数组的内容构造列表s1 list<string> s1(names1, names1 + 4); //用names2数组的内容构造列表s2 list<string> s2(names2, names2 + 4); //将s1的第一个元素放到s2的最后 s2.splice(s2.end(), s1, s1.begin()); list<string>::iterator iter1 = s1.begin(); //iter1指向s1首 advance(iter1, 2); //iter1前进2个元素,它将指向s1第3个元素 list<string>::iterator iter2 = s2.begin(); //iter2指向s2首 ++iter2; //iter2前进1个元素,它将指向s2第2个元素 list<string>::iterator iter3 = iter2; //用iter2初始化iter3 advance(iter3, 2); //iter3前进2个元素,它将指向s2第4个元素 //将[iter2, iter3)范围内的结点接到s1中iter1指向的结点前 s1.splice(iter1, s2, iter2, iter3); //分别将s1和s2输出 copy(s1.begin(), s1.end(), ostream_iterator<string>(cout, " ")); cout << endl; copy(s2.begin(), s2.end(), ostream_iterator<string>(cout, " ")); cout << endl; return 0; }
顺序容器的插入迭代器
例:
list<int> s;
back_inserter iter(s);
*(iter++) = 5; //通过iter把5插入s末尾
顺序容器的适配器
栈模板
template <class T, class Sequence = deque<T> > class stack;
队列模板
template <class T, class FrontInsertionSequence = deque<T> > class queue;
//10_7.cpp, 省略头部分 int main() { stack<char> s; string str; cin >> str; //从键盘输入一个字符串 //将字符串的每个元素顺序压入栈中 for (string::iterator iter = str.begin(); iter != str.end(); ++iter) s.push(*iter); //将栈中的元素顺序弹出并输出 while (!s.empty()) { cout << s.top(); s.pop(); } cout << endl; return 0; } 运行结果如下: congratulations snoitalutargnoc
优先级队列也像栈和队列一样支持元素的压入和弹出,但元素弹出的顺序与元素的大小有关,每次弹出的总是容器中最“大”的一个元素。
template <class T, class Sequence = vector<T> > class priority_queue;
优先级队列的基础容器必须是支持随机访问的顺序容器。
一种细胞在诞生(即上次分裂)后会在500到2000秒内分裂为两个细胞,每个细胞又按照同样的规律继续分裂。
// 10.8.cpp, 头部分省略 const int SPLIT_TIME_MIN = 500; //细胞分裂最短时间 const int SPLIT_TIME_MAX = 2000; //细胞分裂最长时间 class Cell; priority_queue<Cell> cellQueue; class Cell { //细胞类 private: static int count; //细胞总数 int id; //当前细胞编号 int time; //细胞分裂时间 public: Cell(int birth) : id(count++) { //birth为细胞诞生时间 //初始化,确定细胞分裂时间 time = birth + (rand() % (SPLIT_TIME_MAX - SPLIT_TIME_MIN))+ SPLIT_TIME_MIN; } int getId() const { return id; } //得到细胞编号 int getSplitTime() const { return time; } //得到细胞分裂时间 bool operator < (const Cell& s) const //定义“<” { return time > s.time; } void split() { //细胞分裂 Cell child1(time), child2(time); //建立两个子细胞 cout << time << "s: Cell #" << id << " splits to #" << child1.getId() << " and #" << child2.getId() << endl; cellQueue.push(child1); //将第一个子细胞压入优先级队列 cellQueue.push(child2); //将第二个子细胞压入优先级队列 } }; int Cell::count = 0; int main() { srand(static_cast<unsigned>(time(0))); int t; //模拟时间长度 cout << "Simulation time: "; cin >> t; cellQueue.push(Cell(0)); //将第一个细胞压入优先级队列 while (cellQueue.top().getSplitTime() <= t) { cellQueue.top().split(); //模拟下一个细胞的分裂 cellQueue.pop(); //将刚刚分裂的细胞弹出 } return 0; } /* 运行结果如下: Simulation time: 5000 971s: Cell #0 splits to #1 and #2 1719s: Cell #1 splits to #3 and #4 1956s: Cell #2 splits to #5 and #6 2845s: Cell #6 splits to #7 and #8 3551s: Cell #3 splits to #9 and #10 3640s: Cell #4 splits to #11 and #12 3919s: Cell #5 splits to #13 and #14 4162s: Cell #10 splits to #15 and #16 4197s: Cell #8 splits to #17 and #18 4317s: Cell #7 splits to #19 and #20 4686s: Cell #13 splits to #21 and #22 4809s: Cell #12 splits to #23 and #24 4818s: Cell #17 splits to #25 and #26 */
集合用来存储一组无重复的元素。由于集合的元素本身是有序的,可以高效地查找指定元素,也可以方便地得到指定大小范围的元素在容器中所处的区间。
输入一串实数,将重复的去掉,取最大和最小者的中值,分别输出小于等于此中值和大于等于此中值的实数
//10_9.cpp #include <set> #include <iterator> #include <utility> #include <iostream> using namespace std; int main() { set<double> s; while (true) { double v; cin >> v; if (v == 0) break; //输入0表示结束 //尝试将v插入 pair<set<double>::iterator,bool> r=s.insert(v); if (!r.second) //如果v已存在,输出提示信息 cout << v << " is duplicated" << endl; } //得到第一个元素的迭代器 set<double>::iterator iter1=s.begin(); //得到末尾的迭代器 set<double>::iterator iter2=s.end(); //得到最小和最大元素的中值 double medium=(*iter1 + *(--iter2)) / 2; //输出小于或等于中值的元素 cout<< "<= medium: " copy(s.begin(), s.upper_bound(medium), ostream_iterator<double>(cout, " ")); cout << endl; //输出大于或等于中值的元素 cout << ">= medium: "; copy(s.lower_bound(medium), s.end(), ostream_iterator<double>(cout, " ")); cout << endl; return 0; } 运行结果如下: 1 2.5 5 3.5 5 7 9 2.5 0 5 is duplicated 2.5 is duplicated <= medium: 1 2.5 3.5 5 >= medium: 5 7 9
有五门课程,每门都有相应学分,从中选择三门,输出学分总和
//10_10.cpp #include <iostream> #include <map> #include <string> #include <utility> using namespace std; int main() { map<string, int> courses; //将课程信息插入courses映射中 courses.insert(make_pair("CSAPP", 3)); courses.insert(make_pair("C++", 2)); courses.insert(make_pair("CSARCH", 4)); courses.insert(make_pair("COMPILER", 4)); courses.insert(make_pair("OS", 5)); int n = 3; //剩下的可选次数 int sum = 0; //学分总和 while (n > 0) { string name; cin >> name; //输入课程名称 map<string, int>::iterator iter = courses.find(name);//查找课程 if (iter == courses.end()) { //判断是否找到 cout << name << " is not available" << endl; } else { sum += iter->second; //累加学分 courses.erase(iter); //将刚选过的课程从映射中删除 n--; } } cout << "Total credit: " << sum << endl; //输出总学分 return 0; } 运行结果如下: C++ COMPILER C++ C++ is not available CSAPP Total credit: 9
统计一句话中每个字母出现的次数
// 10_11.cpp #include <iostream> #include <map> #include <cctype> using namespace std; int main() { map<char, int> s; //用来存储字母出现次数的映射 char c; //存储输入字符 do { cin >> c; //输入下一个字符 if (isalpha(c)){ //判断是否是字母 c = tolower(c); //将字母转换为小写 s[c]++; //将该字母的出现频率加1 } } while (c != ‘.‘); //碰到“.”则结束输入 //输出每个字母出现次数 for (map<char, int>::iterator iter = s.begin(); iter != s.end(); ++iter) cout << iter->first << " " << iter->second << " "; cout << endl; return 0; }
上课时间查询
//10_12.cpp #include <iostream> #include <map> #include <utility> #include <string> using namespace std; int main() { multimap<string, string> courses; typedef multimap<string, string>::iterator CourseIter; //将课程上课时间插入courses映射中 courses.insert(make_pair("C++", "2-6")); courses.insert(make_pair("COMPILER", "3-1")); courses.insert(make_pair("COMPILER", "5-2")); courses.insert(make_pair("OS", "1-2")); courses.insert(make_pair("OS", "4-1")); courses.insert(make_pair("OS", "5-5")); //输入一个课程名,直到找到该课程为止,记下每周上课次数 string name; int count; do { cin >> name; count = courses.count(name); if (count == 0) cout << "Cannot find this course!" << endl; } while (count == 0); //输出每周上课次数和上课时间 cout << count << " lesson(s) per week: "; pair<CourseIter, CourseIter> range = courses.equal_range(name); for (CourseIter iter = range.first; iter != range.second; ++iter) cout << iter->second << " "; cout << endl; return 0; } 运行结果如下: JAVA Cannot find this course! OS 3 lesson(s) per week: 1-2 4-1 5-5
用到以下算法:
template<class InputIterator, class Type, class BinaryFunction>
Type accumulate(InputIterator first, InputIterator last, Type val, BinaryFunction binaryOp);
#include <iostream> #include <numeric> //包含数值算法头文件 using namespace std; //定义一个普通函数 int mult(int x, int y) { return x * y; }; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in a is " << accumulate(a, a + N, 1, mult) << endl; return 0; }
//10_14.cpp #include <iostream> #include <numeric> //包含数值算法头文件 using namespace std; class MultClass{ //定义MultClass类 public: //重载操作符operator() int operator() (int x, int y) const { return x * y; } }; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in a is " << accumulate(a, a + N, 1, MultClass()) //将类multclass传递给通用算法 << endl; return 0; }
//10_15.cpp #include <iostream> #include <numeric> //包含数值算法头文件 #include <functional> //包含标准函数对象头文件 using namespace std; int main() { int a[] = { 1, 2, 3, 4, 5 }; const int N = sizeof(a) / sizeof(int); cout << "The result by multipling all elements in A is “ << accumulate(a, a + N, 1, multiplies<int>()) << endl; //将标准函数对象传递给通用算法 return 0; }
// 10_16.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); cout << "before sorting:" << endl; copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t")); cout << endl; sort(a.begin(), a.end(), greater<int>()); cout << "after sorting:" << endl; copy(a.begin(),a.end(),ostream_iterator<int>(cout,"\t")); cout << endl; return 0; }
绑定适配器
例 函数适配器实例——找到数组中第一个大于40的元素
//10_17.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); vector<int>::iterator p = find_if(a.begin(), a.end(), bind2nd(greater<int>(), 40)); if (p == a.end()) cout << "no element greater than 40" << endl; else cout << "first element greater than 40 is: " << *p << endl; return 0; }
注:
find_if算法在STL中的原型声明为:
template<class InputIterator, class UnaryPredicate>
InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
它的功能是查找数组[first, last)区间中第一个pred(x)为真的元素。
组合适配器
例 ptr_fun、not1和not2产生函数适配器实例
// 10_18.cpp #include <functional> #include<iostream> #include<vector> #include<algorithm> using namespace std; bool g(int x, int y) { return x > y; } int main() { int intArr[] = { 30, 90, 10, 40, 70, 50, 20, 80 }; const int N = sizeof(intArr) / sizeof(int); vector<int> a(intArr, intArr + N); vector<int>::iterator p; p = find_if(a.begin(), a.end(), bind2nd(ptr_fun(g), 40)); if (p == a.end()) cout << "no element greater than 40" << endl; else cout << "first element greater than 40 is: " << *p << endl; p = find_if(a.begin(), a.end(), not1(bind2nd(greater<int>(), 15))); if (p == a.end()) cout << "no element is not greater than 15" << endl; else cout << "first element that is not greater than 15 is: " << *p << endl; p = find_if(a.begin(), a.end(), bind2nd(not2(greater<int>()), 15)); if (p == a.end()) cout << "no element is not greater than 15" << endl; else cout << "first element that is not greater than 15 is: " << *p << endl; return 0; }
例 成员函数适配器实例
//10_19.cpp #include <functional> #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Car { int id; Car(int id) { this->id = id; } void display() const { cout << "car " << id << endl; } }; int main() { vector<Car *> pcars; vector<Car> cars; for (int i = 0; i < 5; i++) pcars.push_back(new Car(i)); for (int i = 5; i < 10; i++) cars.push_back(Car(i)); cout << "elements in pcars: " << endl; for_each(pcars.begin(), pcars.end(), std::mem_fun(&Car::display)); cout << endl; cout << "elements in cars: " << endl; for_each(cars.begin(), cars.end(), std::mem_fun_ref(&Car::display)); cout << endl; for (size_t i = 0; i < pcars.size(); ++i) delete pcars[i]; return 0; }
例:
template<class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred);
查找[first, last)区间内pred(x)为真的首个元素
例:
template<class ForwardIterator, class T> InputIterator find_if(ForwardIterator first, ForwardIterator last, const T& x);
将[first, last)区间内的元素全部改写为x。
例:
template <class RandomAccessIterator , class UnaryPredicate> void sort(RandomAccessIterator first, RandomAccessIterator last, UnaryPredicate comp);
以函数对象comp为“<”,对 [first, last)区间内的数据进行排序
例:
template<class InputIterator, class OutputIterator, class BinaryFunction> OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryFunction op);
对[first, last)内的元素求部分“和”(所谓部分“和”,是一个长度与输入序列相同的序列,其第n项为输入序列前n个元素的“和”),以函数对象op为“+”运算符,结果通过result输出,返回的迭代器指向输出序列最后一个元素的下一个元素
标签:数组类 fun sar 最大 数据类型 产生 组合 读取数据 算术
原文地址:https://www.cnblogs.com/aidata/p/13041896.html