如何分类一个类的carray [英] How to sort carray of a class

查看:94
本文介绍了如何分类一个类的carray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// Consider below class

class CAnimalData 
{
public:
	CAnimalData(int id, CString text)
	{
		m_id = id;
		m_text = text;
	}
	
	int		m_id;
	CString	m_text;


};

//lets create an array of class

typedef CArray <CAnimalData, CAnimalData&> RADARDATACONTAINER;

//create object of array
RADARDATACONTAINER  objContainer


//Now lets say we create 4 objects of class CAnimalData and keep storing all objects in CArray

CAnimalData data1 ( 0, "Simple")
objContainer.SetAtGrow(data1.m_id, dataBool);

CAnimalData data2 ( 1, "Cat")
objContainer.SetAtGrow(data2.m_id, dataBool);

CAnimalData data3 2, "Aargalis")
objContainer.SetAtGrow(data3.m_id, dataBool);

CAnimalData data4 ( 3, "Dog")
objContainer.SetAtGrow(data4.m_id, dataBool);

store all objects in RADARDATACONTAINER.

The class had two variables, please consider below values.

m_id   m_text
0	   Simple
1      Cat
2     Aargalis
3     Dog


Now I want to sort the CArray by m_text ( not by m_id), so below code should give me result in alphabetical order , how can I sort CArray?

CAnimalData dataStore;
iUBound = objContainer.GetUpperBound();

for(int i = 0; i <= iUBound; i++)

{
		dataStore = objContainer.GetAt(i);
	    Cout<< dataStore.m_id << "   "   << dataStore.m_text;
		
		
	    
}

I see below output:

0	   Simple
1      Cat
2     Aargalis
3     Dog


But I need below output: ( sorting based on text)

2     Aargalis
1     Cat
3     Dog
0	  Simple

Can anybody help me on this?





我尝试了什么:



尝试过但不起作用,不确定CArray课程中是否有现成的功能。

如果有人可以在这里帮助我将会很棒



What I have tried:

tried but did not work, not sure if there is readymade funtion available in CArray class.
Will be great if anyone can help me here

推荐答案

你不能使用像qsort这样的RTL函数,所以你必须自己编写。对于一小组数据,冒泡排序可以充分发挥作用。您还必须编写自己的交换例程,这些例程通常非常简单。通常,冒泡排序是这样的:

You can't use RTL functions like qsort so you will have to write your own. For a very small set of data a bubble sort will work adequately. You will also have to write your own swap routine and those are usually fairly simple. Typically, a bubble sort is something like this :
int count = collection.size();
for( int m = 0; m < count-1; ++m )
{
   for( int n = m + 1; n < count; ++n )
   {
       if( collection[m] > collection[n] )
       {
           collection.swap( m, n );
       }
   }
}

以下是交换示例:

Here's an example of swapping :

temp = a;
a = b;
b = temp;

在这种情况下,a和b是在冒泡排序中检查的两个项目。



您的任务将此伪代码转换为实际代码。我不会那样做,因为你不会从中学到任何东西。希望您可以在编写此算法的实际代码的过程中学到一些东西。例如,伪代码显示集合中两个项目的比较,那么如果要根据文本进行排序,您将如何执行该比较?答案是用于CString类的比较方法。我也会让你弄明白这一点。



请记住,调试器是你的朋友,所以如果遇到麻烦,请用它来帮助你弄清楚发生了什么。我仍然每天使用它,所以最好熟悉它。



一个提示:您可以通过更改比较来切换排序顺序大于小于。如果你有时间,你应该尝试两者来验证你的实施。祝你好运。

In this case, a and b were the two items being checked in the bubble sort.

Your task will be to translate this pseudo code into real code. I won't do that for you because you will learn nothing from that. Hopefully, you can learn something in the process of writing real code for this algorithm. For example, the pseudo code shows a comparison of two items in the collection so how would you perform that comparison if you want to sort on the basis of the text? The answer is use to a comparison method of the CString class. I will let you figure that out too.

Remember, the debugger is your friend so if you run into troubles use it to help you figure out what is going on. I still use it daily so it is a good idea to become very familiar with it.

One tip: you can switch the order of the sort by changing the comparison from greater than to less than. You should try both to verify your implementation if you have the time. Best of luck.


这篇关于如何分类一个类的carray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆