C++语言程序设计 (郑莉)第十章 泛型程序设计与C++标准模板库













cpp 复制代码
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
	const int N = 5;
	vector<int> s(N);
	for (int i = 0; i < N; i++) {
		cin >> s[i];
	}
	transform(s.begin(), s.end(), ostream_iterator<int>(cout, " "), negate<int>());
	cout << endl;
	return 0;
}


cpp 复制代码
#include <iostream>
#include <iterator>
#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;
}




cpp 复制代码
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;

template <class T,class InputIterator,class OutputIterator>
void mySort(InputIterator first, InputIterator last, OutputIterator result) {
	vector<T> s;
	for (; first != last; ++first) {
		s.push_back(*first);
	}
	sort(s.begin(), s.end());
	copy(s.begin(), s.end(), result);
}

int main() {
	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;
}












cpp 复制代码
#include <iostream>
#include <list>
#include <deque>
#include <iterator>
using namespace std;
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() {
	deque <int> s;
	for (int i = 0; i < 10; i++) {
		int x;
		cin >> x;
		s.push_front(x);
	}
	printContainer("deque at first", s);
	list <int> l(s.rbegin(), s.rend());
	printContainer("list at first", 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);
	s.assign(l.begin(), l.end());
	printContainer("deque at last", s);
	return 0;
}



cpp 复制代码
#include <iostream>
#include <list>
#include <deque>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	istream_iterator<int> i1(cin), i2;
	vector<int> s1(i1, i2);
	sort(s1.begin(), s1.end());
	deque<int> s2;
	for (vector<int>::iterator iter = s1.begin(); iter != s1.end(); ++iter) {
		if (*iter % 2 == 0) {
			s2.push_back(*iter);
		}
		else {
			s2.push_front(*iter);
		}
	}
	copy(s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <list>
#include <deque>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	string name1[] = { "Alice","Helen","Lucy","Susan" };
	string name2[] = { "Bob","David","Levin","Mike" };
	list <string> s1(name1, name1 + 4);
	list <string> s2(name2, name2 + 4);
	s2.splice(s2.end(), s1, s1.begin());
	list <string>::iterator iter1 = s1.begin();
	advance(iter1, 2);
	list <string>::iterator iter2 = s2.begin();
	++iter2;
	list <string>::iterator iter3 = iter2;
	advance(iter3, 2);
	s1.splice(iter1, s2, iter2, iter3);
	copy(s1.begin(), s1.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
	copy(s2.begin(), s2.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
	return 0;
}









cpp 复制代码
#include <iostream>
#include <list>
#include <deque>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <stack>
using namespace std;
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;
}


cpp 复制代码
#include <iostream>
#include <list>
#include <deque>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <stack>
#include <queue>
using namespace std;
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++) {
		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()const {
		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;
}








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;
		}
		pair<set<double>::iterator, bool> r = s.insert(v);
		if (!r.second) {
			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, " "));
	return 0;
}



cpp 复制代码
#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;

int main() {
	map<string, int> 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 availeble" << endl;
		}
		else {
			sum += iter->second;
			courses.erase(iter);
			n--;
		}
	}
	cout << "Total credit:" << sum << endl;
	return 0;
}
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]++;
		}
	} while (c != '.');
	for (map<char, int>::iterator iter = s.begin(); iter != s.end(); ++iter) {
		cout << iter->first << " " << iter->second << " ";
	}
	cout << endl;
	return 0;
}


cpp 复制代码
#include <iostream>
#include <map>
#include <utility>
#include <string>
using namespace std;
int main() {
	multimap<string, string> courses;
	typedef multimap<string, string>::iterator Courselter;
	courses.insert(make_pair("C++", "2-6"));
	courses.insert(make_pair("COMPILER", "3-1"));
	courses.insert(make_pair("COMPLIER", "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<Courselter, Courselter> range = courses.equal_range(name);
	for (Courselter iter = range.first; iter != range.second; ++iter) {
		cout << iter->second << " ";
	}
	cout << endl;
	return 0;
}





cpp 复制代码
#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;
}
cpp 复制代码
#include <iostream>
#include <numeric>
using namespace std;
class MultClass {
public:
	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()) << endl;
	return 0;
}


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;
}
cpp 复制代码
#include <iostream>
#include <functional>
#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;
}


cpp 复制代码
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using namespace placeholders;
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);
	auto p = find_if(a.begin(), a.end(), bind(greater<>(), _1, 40));
	if (p == a.end()) {
		cout << "no element grater than 40" << endl;
	}
	else {
		cout << "first elment grater than 40 is " << *p << endl;
	}
	return 0;
}









相关推荐
努力的小陈^O^2 小时前
问题:Spring循环依赖问题排查与解决
java·开发语言·前端
FreeBuf_2 小时前
利用零宽度字符的隐形JavaScript混淆工具InvisibleJS浮出水面
开发语言·javascript·ecmascript
lsx2024062 小时前
Go 语言指针
开发语言
wearegogog1233 小时前
基于MATLAB的IEEE 9节点系统潮流计算
开发语言·matlab
分布式存储与RustFS3 小时前
RustFS在AI场景下的实测:从GPU到存储的完整加速方案
开发语言·人工智能·rust·对象存储·企业存储·rustfs·minio国产化替代
揽昕3 小时前
判断对象是否含有某个属性
开发语言·前端·javascript
phltxy3 小时前
解锁JavaScript WebAPI:从基础到实战,打造交互式网页
开发语言·javascript
资生算法程序员_畅想家_剑魔4 小时前
Java常见技术分享-分布式篇-分布式系统基础理论
java·开发语言·分布式
FL16238631294 小时前
C# winform部署yolo26-obb旋转框检测的onnx模型演示源码+模型+说明
开发语言·c#