C++ PLUS

1 篇文章 0 订阅
1 篇文章 0 订阅

// 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;

}

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一些常见的数据结构,以及它们在 C++ 中的实现: 1. 数组 C++ 中的数组是一组相同类型的元素的集合,可以通过下标来访问数组中的元素。下面是一个创建和初始化数组的示例: ```cpp int arr[5]; // 声明一个包含5个整数的数组 arr[0] = 1; // 设置第一个元素为1 arr[1] = 2; // 设置第二个元素为2 arr[2] = 3; // 设置第三个元素为3 arr[3] = 4; // 设置第四个元素为4 arr[4] = 5; // 设置第五个元素为5 ``` 2. 链表 链表是一种动态数据结构,它由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。下面是一个简单的链表实现: ```cpp struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 创建一个链表,并添加一些元素 ListNode* head = new ListNode(1); ListNode* second = new ListNode(2); ListNode* third = new ListNode(3); head->next = second; second->next = third; ``` 3. 栈 栈是一种后进先出(LIFO)的数据结构,它支持两个基本操作:push(将元素压入栈顶)和pop(将栈顶元素弹出)。下面是一个简单的栈实现: ```cpp class Stack { public: Stack() {} void push(int x) { st.push(x); } void pop() { st.pop(); } int top() { return st.top(); } bool empty() { return st.empty(); } private: std::stack<int> st; }; // 创建一个栈,并添加一些元素 Stack s; s.push(1); s.push(2); s.push(3); s.pop(); // 弹出栈顶元素 ``` 4. 队列 队列是一种先进先出(FIFO)的数据结构,它支持两个基本操作:enqueue(将元素插入队尾)和dequeue(将队头元素弹出)。下面是一个简单的队列实现: ```cpp class Queue { public: Queue() {} void enqueue(int x) { q.push(x); } void dequeue() { q.pop(); } int front() { return q.front(); } bool empty() { return q.empty(); } private: std::queue<int> q; }; // 创建一个队列,并添加一些元素 Queue q; q.enqueue(1); q.enqueue(2); q.enqueue(3); q.dequeue(); // 弹出队头元素 ``` 5. 堆 堆是一种完全二叉树,它满足堆属性:对于每个节点 x,其父节点的值小于或等于 x 的值(最大堆)或大于或等于 x 的值(最小堆)。在 C++ 中,可以使用 priority_queue 类来实现堆: ```cpp class Heap { public: Heap() {} void push(int x) { pq.push(x); } void pop() { pq.pop(); } int top() { return pq.top(); } bool empty() { return pq.empty(); } private: std::priority_queue<int> pq; }; // 创建一个最大堆,并添加一些元素 Heap h; h.push(1); h.push(3); h.push(2); h.pop(); // 弹出堆顶元素 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值