C++ PLUS

// 16.string类和标准模板库.cpp : 定义控制台应用程序的入口点。
//

#include “stdafx.h”
#include
#include
#include
using namespace std;

//第三题
/*int reduce(long ar[], int n)
{
list a;
int i = 0;
while (i < n)
{
a.push_back(ar[i]);
i++;
}
a.sort();
a.unique();
list::iterator p = a.begin();
for (i = 0; i < a.size (); i++, p++)
ar[i] = *p;
cout << a.size()<<endl;
for (i = 0; i < a.size(); i++)
cout << ar[i] << " " << endl;
return a.size();
}

int main()
{
long a[10] = { 5, 45, 64, 54, 45, 54, 87, 54, 5, 64 };
reduce(a, 10);
return 0;
}*/

//第5题:

/*
class es
{
private:
int num;
string ss;
public:
es(int a = 0, string b = “NULL”) :num(a), ss(b){ cout << "creating "<< endl; }
void set(int a, string b){ num = a; ss = b; }
void show(){ cout << num << " " << ss << endl; }
int operator >(es & a);
int operator ==(es & a);
int operator <(es & a); //重载以调用函数模板sort()和unique()
};

int es::operator >(es & a)
{
if (num > a.num)
return 1;
else if (num == a.num&&ss > a.ss)
return 1;
else return 0;
}

int es::operator ==(es & a)
{
if (num == a.num&&ss == a.ss)
return 1;
else return 0;
}

int es::operator <(es & a)
{
if (!(*this>a) && !(*this == a))
return 1;
else return 0;
}

template
void show(T ar[], int n)
{
int i = 0;
for (; i < n; i++)
ar[i].show();
}

template
int reduce(T ar[], int n)
{
list a;
int i = 0;
for (i = 0; i < n; i++)
a.push_back(ar[i]);
a.sort();
a.unique();
list::iterator p = a.begin();
for (i = 0; i < a.size(); p++,i++)
ar[i] = *p;
show(ar, a.size());
return a.size();
}

int main()
{
es a[7];
a[0].set(1, “aaa”);
a[1].set(9, “grg”);
a[2].set(1, “aaa”);
a[3].set(4, “rerg”);
a[4].set(6, “er”);
a[5].set(4, “rerg”);
a[6].set(6, “aer”);
reduce(a,7);
cin.get();
return 0;
}

*/

// random_shuffle(a.begin(), a.end()); 把数字打乱,保证不重复

/*
while (1)
{
scanf_s("%s", b,30);
if (strcmp(b, “quit”) == 0)
break;
cout << b << endl;
pat.push_back(b);
}
*/

//第九题
/*
#include<time.h>
#include

#include

int SIZE = 10000000;
int main()
{
vector v0;
int i = 0;
srand(time(0));
for (; i < SIZE; i++)
v0.push_back(rand() % 10000 + 1);
vector v1;
list v2;
int n;
i = 0;
for (; i < SIZE; i++)
{
n = rand() % 10000 + 1;
v1.push_back(n);
v2.push_back(n);
}

clock_t s1 = clock();
sort(v1.begin(),v1.end());
clock_t s2 = clock();
cout << (double)(s2 - s1)/CLOCKS_PER_SEC << endl;
 s1 = clock();
v2.sort();
 s2 = clock();
cout << (double)(s2 - s1) / CLOCKS_PER_SEC << endl;

s1 = clock();
list<int>::iterator p = v2.begin();
for (i = 0; i < SIZE; i++,p++)
{
	v0[i] = *p;
}
sort(v0.begin(), v0.end());
p = v2.begin();
for (i = 0; i < SIZE; i++)
{
	v2.push_back(v0[i]);
}
s2 = clock();
cout << (double)(s2 - s1) / CLOCKS_PER_SEC << endl;
return 0;

}
*/

//第十题

/*#include
#include
#include
#include<time.h>
struct re
{
string title;
int rate;
int price;
};
extern int n;
int cmp0(shared_ptr a, shared_ptr b)
{
if (n1)
if (a->title > b->title)
return 1;
else
return 0;
else if (n
2)
if (a->rate >b->rate)
return 1;
else
return 0;
else if (n==3)
if (a->price >b->price)
return 1;
else
return 0;
else cout << “ERROR” << endl;
exit(0);
}

int n;
void show(shared_ptr a)
{
if (n == 1)
{
cout << a->title << " " << a->rate << " " << a->price << endl;
}
if (n == 2)
{
cout << a->rate << " " << a->title << " " << a->price << endl;
}
if (n == 3)
{
cout << a->price << " " << a->rate << " " << a->title << endl;
}
}

int SIZE = 9;
int main()
{
cout << “enter 1 or 2 or 3:” << endl << “1 rank title,2 rank rate,3 rank price” << endl;
int j;
cin >> n;
cout << “enter 1 or 2:” << “1 up,2 down:” << endl;
cin >> j;
vector<shared_ptr> a;
int i = 0;
vector<shared_ptr>::iterator pp=a.begin();
for (; i < SIZE; i++)
{

     shared_ptr<re> p(new re);                                                                             //注意用法
	cin >> p->title;
	cin >> p->rate;                                                                                   //a[i]->price违法???
	//cin >> p->price;
	a.push_back(p);
	//cout << (*(a.begin())).rate << endl;
	//for_each(a.begin(), a.end(), show);
}
cout << endl << endl;
for_each(a.begin(), a.end(), show);
sort(a.begin(), a.end(), cmp0);                                                        //真为小
cout << endl << endl;
if (j==1)
	for_each(a.begin(), a.end(), show);
else if (j==2)
	for_each(a.rbegin(), a.rend(), show);
else cout << "ERROR" << endl;
return 0;

}

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值