查找(adjacent_find)-5

本文介绍了一个C++程序,该程序利用STL中的adjacent_find算法来查找向量中相邻的重复元素及满足特定条件的元素对。通过自定义谓词函数doubled,实现了查找第一个元素是第二个元素两倍的相邻元素对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool doubled(int elem1, int elem2)
{
	return elem1 * 2 == elem2;
}

int main()
{
	vector<int> ivec;

	ivec.push_back(1);
	ivec.push_back(3);
	ivec.push_back(3);
	ivec.push_back(5);
	ivec.push_back(5);
	ivec.push_back(5);
	ivec.push_back(7);
	ivec.push_back(8);

	auto pos = adjacent_find(ivec.begin(), ivec.end());//找连续相等的
	if (pos != ivec.end())
		cout << "fins ok:" << distance(ivec.begin(), pos) + 1 << endl;

	//用一个谓词去找
	//找连续的两个数,第一个数是第二个数的两倍
	auto pos1 = adjacent_find(ivec.begin(), ivec.end(), doubled);
	if (pos1 != ivec.end())
		cout << "fins ok:" << distance(ivec.begin(), pos1) + 1 << endl;
	else
		cout << "error" << endl;
	system("pause");
	return 0;
}
#include <stdio.h> #include <stdlib.h> const int Max_length=55;//最大内存 struct areaNode//管理分区的结构体 { int ID;//分区号 int size;//分区大小 int address;//分区地址 int flag;//使用状态,0为未占用,1为已占用 }; typedef struct DuNode//双向链表结点 { struct areaNode data;//数据域 struct DuNode *prior;//指针域 struct DuNode *next; }*DuLinkList; DuLinkList m_head = new DuNode, m_last = new DuNode;//双向链表首尾指针 void init()//分区链表初始化 { m_head->prior = NULL; m_head->next = m_last; m_last->prior = m_head; m_last->next = NULL; m_head->data.size = 0; m_last->data.address = 0; m_last->data.size = Max_length; m_last->data.ID = 0; m_last->data.flag = 0; } void show() { DuNode *p = m_head->next;//指向空闲区队列的首地址 printf("+++++++++++++++++++++++++++++++++++++++\n"); while (p) { printf("分区号:"); if (p->data.ID == 0) printf("FREE\n"); else printf("%d\n",p->data.ID); printf("分区起始地址:%d\n",p->data.address); printf("分区大小:%d\n",p->data.size); printf("分区状态:"); if (p->data.flag) printf("已被占用\n"); else printf("空闲\n"); printf("——————————————————\n"); p = p->next; } } bool first_fit(int id,int m_size)//首次适应算法,id为作业号,m_size为作业大小 { //请补充使用首次适应算法给作业分配内存的函数代码 } void recycle(int id)//回收内存,id为释放内存的作业号 { //请补充回收作业内存的代码 } int main() { init(); printf("首次适应算法:\n"); first_fit(1,15); first_fit(2,30); recycle(1); first_fit(3,8); first_fit(4,6); recycle(2); show(); DuNode *p = m_head; while(p != NULL) { DuNode *temp = p; p = p->next; delete(temp); temp = NULL; } return 0; }
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值