如何对list中部分元素排序

直接上代码

// main.h

#include "stdafx.h"
#include "DateFile.h"

int _tmain(int argc, _TCHAR* argv[])
{
	WORD wNum = 4;

	InitDlgList(wNum);
	WORD* pWordTar = GenerateDerangeOrder(wNum);
	DerangeDlglist(pWordTar);

	system("pause");
	return 0;
}

// DateFile.h

#pragma once

#include <windows.h>

#include <list>
#include <map>
#include <vector>
#include <iostream>
using namespace std;


typedef enum {
	DONT_SORT = 0,		// 不需要排序
	NEED_SORT		// 需要排序
} SORTTYPE;

typedef struct tagNODE{
	SORTTYPE	nType;
	WORD		wSrcIndex;
	WORD		wTarIndex;

	const bool operator < (const tagNODE& node) const {
		if (nType == DONT_SORT && node.nType == NEED_SORT) {
			if (wTarIndex == node.wTarIndex) {
				return true;
			}
		} else if (nType == NEED_SORT && node.nType == DONT_SORT) {
			if (wTarIndex == node.wTarIndex) {
				return false;
			}
		}

		bool bRet = (wTarIndex < node.wTarIndex);
		return bRet;
	}
} NODE, *PNODE;

typedef std::list<NODE> CNodeList;
CNodeList	g_lstNode;

void InitDlgList(__in WORD wNum)
{
	for (WORD i = 0; i < wNum; i ++) {
		NODE	node;
		node.nType = NEED_SORT;
		if (i+1 == 3 || i+1 == 1) {
			NODE node;
			node.nType = DONT_SORT;
			g_lstNode.push_back(node);
		}

		g_lstNode.push_back(node);
	}
}

WORD* GenerateDerangeOrder(__in WORD wNum)
{
	// 1. 初始化待分配数组
	WORD * pWordSrc = new WORD[wNum];
	for (WORD i = 0; i < wNum; i ++) {
		pWordSrc[i] = i + 1;
	}
	WORD * pWordTar = new WORD[wNum];
	memset(pWordTar, 0, sizeof(WORD)*wNum);

	// 2. 给目标数组赋不重复的值
	srand(GetTickCount());
	//CDlgList::iterator it = g_lstDlg.begin();
	for (WORD i = 0; i < wNum; ) {
		WORD wIndex = rand() % wNum;
		if (pWordSrc[wIndex] != 0) {
			pWordTar[i] = pWordSrc[wIndex];

			//it->wTarIndex = pWordSrc[wIndex];

			pWordSrc[wIndex] = 0;
			i ++;
			//it ++;
		}
	}

	// 3. dump出来
	for (WORD i = 0; i < wNum; i ++) {
		//cout << pWordTar[i] << "\t";
	}

	// 4. 析构
	delete [] pWordSrc;
	//delete [] pWordTar;

	return pWordTar;
}

void DerangeDlglist(WORD * pOrder)
{
	CNodeList::iterator it = g_lstNode.begin();
	for (int i = 0,j=0; it != g_lstNode.end(); it ++) {
		
		if (it->nType == DONT_SORT) {
			it->wSrcIndex = 0;		// 暂时用-1代表GROUPDLG的原序
			it->wTarIndex = (j == 0 ? 1 : j);
		}
		j ++;
		
		if (it->nType == NEED_SORT) {
			it->wSrcIndex = i+1;
			it->wTarIndex = pOrder[i];
			i ++;
		}
	}

	cout << "Befor sort:" << endl;
	// dump result
	it = g_lstNode.begin();
	for ( ; it != g_lstNode.end(); it ++) {
		if (it->nType == DONT_SORT) {
			cout << "DONT_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
		} else if (it->nType == NEED_SORT) {
			cout << "NEED_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
		}
	}

	g_lstNode.sort();

	cout << endl << "After sort:" << endl;
	// dump result
	it = g_lstNode.begin();
	for ( ; it != g_lstNode.end(); it ++) {
		if (it->nType == DONT_SORT) {
			cout << "DONT_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
		} else if (it->nType == NEED_SORT) {
			cout << "NEED_SORT:" << it->wSrcIndex << " " << it->wTarIndex << endl;
		}
	}

	delete [] pOrder;
	pOrder = NULL;
}


1. 区分排序与勿需排序的点.

list中每个节点都有一个 SORTTYPE 结构来判断当前节点是需要排序的点, 还是不需要排序的点.

2. 如何比较排序与勿需排序的点

这时需要设计list的sort函数,  我们需要知道list中所有排序与勿需排序的点之前的位置,

然后在比较的时候,  根据设定的排序值来比较.



另附一张程序过程图



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值