代码验证

#include<iostream>
using namespace std;
template <class T>
T sum(T* array, int size = 0)
{
	T total = 0;
	for (int i = 0; i < size; i++)
		total += array[i];
	return total;
}

template <class T1, class T2>
T2 sum(T1* array1, T2* array2, int size = 0)
{
	T2 total = 0;
	for (int i = 0; i < size; i++)
		total += array1[i] + array2[i];
	return total;
}

char* sum(char* s1, char* s2)
{
	char* str = new char[strlen(s1) + strlen(s2)];
	strcpy(str, s1);
	return strcat(str, s2);
}
void main()
{
	int iArr[] = { 1,2,3,4,5 };
	double dArr[] = { 1.1,2.2,3.3,4.4,5.5 };
	char* p1 = "Hello,";
	char* p2 = "World";
	int iTotal = sum(iArr, 5);
	double dTotal = sum(dArr, 5);
	double idTotal = sum(iArr, dArr, 5);
	p1 = sum(p1, p2);
	cout << iTotal << endl;
	cout << dTotal << endl;
	cout << idTotal << endl;
	cout << p1 << endl;
}


#include<iostream>
using namespace std;
template <class T,int n>
class mysequence {
	T memblock[N];
public:
	void setmember(int x, T value)
	{
		memblock[x] = value;

	}
	T getmember(int x) {
		return memblock[x];
	}
};

int main()
{
	mysequence<int, 5>myints;
	mysequence<double, 5>myfloats;
	myints.setmember(0, 100);
	myfloats.setmember(3, 3.1416);
	cout << myints.getmember(0) << endl;
	cout << myfloats.getmember(3) << endl;
	return 0;
}

课本习题
//9-1
#include<iostream>
using namespace std;
template<class T>
class arry
{
	int  n;
	T score;
public:
	arry(){}
	arry(int m):n(m){}
	T sum()
	{
		T ans = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> score;
			ans += score;
		}
	return ans;
	}
	
};
int main()
{
	int n;
	cout << "请输入人数:" << endl;
	cin >> n;
	arry<float> s(n);
	cout << "输入依次输入每个人的成绩:";
	cout << "该课程的平均成绩为:" << s.sum() / n << endl;
}


//9-5
不会
//9-6
不会


//9-10
#include<iostream>
using namespace std;
void sort(int a[],int l,int r)
{
	if (l >= r)return;
	int i = l - 1, j = r + 1, x = a[l + r >> 1];
	while (i < j)
	{
		do i++; while (a[i] < x);
		do j--; while (a[j] > x);
		if (i < j)swap(a[i], a[j]);
	}
	sort(a, l, j);
	sort(a, j + 1, r);
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 0, 19);
	for (int i = 0; i < 20; i++)
	{
		cout << data1[i] << " ";
	}
	return 0;
}

//10-3
#include <iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int>s;
	for (int i = 0; i < 5; i++)
	{
		s.push_back(i);
		cout<<"第"<<i << "次添加后的容量: " << s.capacity() << endl;
	}
	return 0;
}


//9-12
#include<iostream>
using namespace std;
void sort(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (a[i] > a[j])
			{
				swap(a[i], a[j]);
			}
		}
	}
		
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 20);
	for (int i = 0; i < 20; i++)
	{
		cout << data1[i] << " ";
	}
	return 0;
}

//9-10
#include<iostream>
using namespace std;
void print(int a[], int n);
void sort(int a[],int n)
{
	for (int i = 0; i < n; i++)
	{
		print(a, 20);
		for (int j = 0; j < n-i; j++)
		{
			if (a[j] < a[j+1])
			{
				swap(a[j], a[j+1]);
			}
		}
	}
		
}
void print(int a[],int n)
{
	for (int i = 0; i < 20; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	sort(data1, 20);
	cout << "最终结果:" << endl;
	print(data1, 20);
	return 0;
}


//9-17
#include<iostream>
using namespace std;
void print(int a[], int n);
int sort(int a[],int l,int r,int x)
{
	
	while (l < r)
	{
		int i = 0, j = r;
		int mid =l + r >> 1;
		if (a[mid] >= x) r = mid;
		else l = mid + 1;
	}
	return r;
}
void print(int a[],int n)
{
	for (int i = 0; i < 20; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}
int main()
{
	int data1[] = { 1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20 };
	cout<<sort(data1,0,19,5);
	//cout << "最终结果:" << endl;
	//print(data1, 20);
	return 0;
}

//10-4
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
using namespace std;
template<class T>
void jp(T colect, int n, int m)
{
	if (n < 1 || m < 1) {
		cout << "错误的问题假设" << endl;
		return;
	}
	for (int i = 1; i <= n; i++)
	{
		colect.push_back(i);
	}
	class T::iterator iter=colect.begin(), del;
		int counter=1;
		while (colect.size() > 1)
		{
			while (counter % m == 0 && colect.size() > 1) {
				counter = 1;
					if (typeid(colect) != typeid(list<int>))
						iter = colect.erase(iter);
					else {
						del = iter;
						iter++;
						colect.erase(del);
					}
				if (iter == colect.end())
					iter = colect.begin();
			}
			counter++;
			iter++;
			if (iter == colect.end())
				iter = colect.begin();
		}
	//finish = colock();
	cout << "最后剩余人的编号为" << *iter << endl;
	cout << "使用容器" << typeid(colect).name() << endl;
}
int main()
{
	vector<int>v;
	jp(v, 100000, 5);
	
	return 0;
}



//10-5
template<class T>
void exchange(list<T>& l1, class list<T>::iterator p1, list<T>& l2, class list<T>::iterator p2)
{
	list<T>temp;
	temp.splice(temp.begin(), l1, p1, l1.end());
	l1.splice(l1.end(), l2, p2, l2.end());
	l2.splice(l2.end(), temp, temp.begin(), temp.end());
}

//10-7
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
#include<queue>
#include<stack>
using namespace std;

int main()
{
	int a[] = { 5,4,1,6 };
	stack<int>s;
	queue<int>q;
	priority_queue<int>q1;
	cout << "stack" << endl;
	for (int i = 0; i < 4; i++) {
		s.push(a[i]);
	}
	if (!s.empty()) {
		cout << "第一次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第二次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第三次pop,取出:" << s.top() << endl;
		s.pop();
	}
	if (!s.empty()) {
		cout << "第四次pop,取出:" << s.top() << endl;
		s.pop();
	}


	cout << "queue" << endl;
	for (int i = 0; i < 4; i++) {
		q.push(a[i]);
	}
	if (!q.empty()) {
		cout << "第一次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第二次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第三次queue,取出:" << q.front() << endl;
		q.pop();
	}
	if (!q.empty()) {
		cout << "第四次queue,取出:" << q.front() << endl;
		q.pop();
	}


	cout << "priority_queue" << endl;
	for (int i = 0; i < 4; i++) {
		q1.push(a[i]);
	}
	
	if (!q1.empty()) {
		cout << "第一次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第二次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第三次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	if (!q1.empty()) {
		cout << "第四次 aurityqueue,取出:" << q1.top() << endl;
		q1.pop();
	}
	
	return 0;
}


//10-8
#include <iostream>
#include<vector>
#include<list>
#include<typeinfo>
#include<ctime>
#include<queue>
#include<stack>
#include<set>
using namespace std;

int main()
{
	string str;
	multiset<string>strset;
	while (1)
	{
		cout << "请输入字符串" << endl;
		cin >> str;
		if (str == "QUIT")
		{
			break;
		}
		int counter = strset.count(str);
		if (counter > 0) {
			cout << str << "出现了" << counter << "次" << endl;
		}
		else {
			cout << str << "在集合中没有出现过" << endl;
		}
		strset.insert(str);
	}
	return 0;
}
posted on 2023-04-28 21:26  许七安gyg  阅读(13)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });