Adding struct in container in sorted way

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 33
Reputation: bunnyboy is an unknown quantity at this point 
Solved Threads: 1
bunnyboy's Avatar
bunnyboy bunnyboy is offline Offline
Light Poster

Adding struct in container in sorted way

 
0
  #1
Oct 28th, 2009
So I would like to make a function which would add elements in container in sorted manner.

Let's say we have a struct with two integer. I randomly initialize them and the add it to deque. And now what I want is that my function would add them in a way that values would be sorted. Example:

We generate numbers for 6 struct.
1 0
3 6
2 4
0 2
2 1
0 5

What I want now is that my function would add them in that order:
0 2
0 5
1 0
2 1
2 4
3 6

I came up with that, but it does not work. I am kinda new with iterators ... any help is appreciated.

  1. #include <iostream>
  2. #include <deque>
  3. #include <ctime>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. struct Str
  9. {
  10. int a,b;
  11. Str(int aa, int bb): a(aa), b(bb){}
  12. };
  13.  
  14. void m_Add(const Str& str, deque<Str>& deq)
  15. {
  16. if (deq.empty()) deq.push_back(str);
  17. else
  18. {
  19. deque<Str>::iterator it = deq.begin();
  20. for (int i = 0, size = deq.size(); i < size; i++)
  21. {
  22. if (str.a != deq[i].a)
  23. {
  24. it++;
  25. }
  26. else break;
  27. }
  28.  
  29. for (int i = 0, size = deq.size(); i < size; i++)
  30. {
  31. if (str.b > deq[i].b)
  32. {
  33. it++;
  34. }
  35. else break;
  36. }
  37.  
  38. deq.insert(it, str);
  39. }
  40. }
  41.  
  42. int _tmain()
  43. {
  44. srand((unsigned)time(NULL));
  45. deque<Str> deq;
  46.  
  47. for (int i(0); i < 10; i++)
  48. {
  49. for (int j = 0; j < 15; j++)
  50. {
  51. Str a(rand()%3, rand()%6);
  52. m_Add(a,deq);
  53. }
  54. }
  55.  
  56. for (int i = 0, size = deq.size(); i < size; i++)
  57. {
  58. cout << deq[i].a << " " << deq[i].b << endl;
  59. }
  60.  
  61. return 0;
  62. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
0
  #2
Oct 28th, 2009
Let me offer this suggestion:

Using the sort() function from <algorithm> we find that there is an overloaded version of sort() that will allow us to supply our own custom-made 'test' function. Let's use this in order to specifically perform a test between two 'deques of structs'. We'll base our test on the 'int a' attribute of the struct:

Here are the two versions of sort():
template <class RandomAccessIterator>
  void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp }; 
The version we will be using is in green (above).

  1. #include<algorithm>
  2. #include<deque>
  3. #include<ctime>
  4. ...
  5. ...
  6. bool deq_test(Str& deq1, Str& deq2){Return (deq1.a < deq2.b);}
  7. ...
  8. Str MyStr;
  9. ...
  10. srand(time(NULL));
  11. ...
  12. //Randomly populate ye' deque
  13. for(int i=0; i<6; i++)
  14. {
  15. MyStr.a = rand()%10;
  16. MyStr.b = rand()%10;
  17.  
  18. deq.push_back(MyStr);
  19. }
  20. ...
  21. //Deque sorting goodness
  22. sort(deq.begin(), deq.end(), deq_test);
  23.  
  24. //Now your deque is sorted according to the 'int a' Str struct attribute in ascending order.

The above code is untested and also I have never done this before but according to the instructions all this should work bueno.

Let me know how this works for ye' if you decide to use this.
Last edited by Clinton Portis; Oct 28th, 2009 at 11:08 pm. Reason: your mom.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
0
  #3
Oct 28th, 2009
Error - Line #6, Should be:
  1. bool deq_test(Str& deq1, Str& deq2){Return (deq1.a < deq2.a);}
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 33
Reputation: bunnyboy is an unknown quantity at this point 
Solved Threads: 1
bunnyboy's Avatar
bunnyboy bunnyboy is offline Offline
Light Poster
 
0
  #4
Oct 29th, 2009
I have already try this ... and i have not been able to successfully write the comparison function which would sort deque by both values.

With your code deque is sorted only by a value, so youl'll get
let's say:
02
01
05
13
11
15
21
20

instead of :
01
02
05
11
13
15
20
21
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 343
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 63
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
0
  #5
Oct 29th, 2009
Originally Posted by bunnyboy View Post
I have already try this ... and i have not been able to successfully write the comparison function which would sort deque by both values.

With your code deque is sorted only by a value, so youl'll get
let's say:
02
01
05
13
11
15
21
20

instead of :
01
02
05
11
13
15
20
21
I've not actually tried this, but would something like this work for the deq_test function?
  1. bool deq_test(Str& deq1, Str& deq2)
  2. {
  3.  
  4. if(deq1.a==deq2.a)
  5. {
  6. return(deq1.b<deq2.b);
  7. }
  8. else
  9. return(deq1.a<deq2.a);
  10. }
In other words, if the values of deq1.a and deq2.a are the same, you sort them by their b values. Otherwise you sort them by their a values.

As mentioned, I've not tested this but I think it should work!
Cheers for now,
Jas.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 343
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 63
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
1
  #6
Oct 29th, 2009
Aha, I've just quickly tested it and it seems to work!
Here's a little program I knocked up, loosely based around previously posted code in this thread:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <deque>
  4. #include <ctime>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9. struct Str
  10. {
  11. int a,b;
  12. };
  13.  
  14. bool deq_test(Str& deq1, Str& deq2)
  15. {
  16. if(deq1.a==deq2.a)
  17. {
  18. return(deq1.b<deq2.b);
  19. }
  20. else
  21. return(deq1.a<deq2.a);
  22. }
  23.  
  24. int main()
  25. {
  26. deque<Str> deq;
  27. srand(time(NULL));
  28. Str MyStr;
  29.  
  30. cout << "Generating values and populating deque:" << endl;
  31. for(int i=0; i<6; ++i)
  32. {
  33. MyStr.a = rand()%10;
  34. MyStr.b = rand()%10;
  35. cout << "a = " << MyStr.a << ", b = " << MyStr.b << endl;
  36. deq.push_back(MyStr);
  37. }
  38.  
  39. cout << endl << "Sorting Deque..." << endl << endl;
  40. sort(deq.begin(), deq.end(), deq_test);
  41.  
  42. cout << "Sorted values are: " << endl;
  43. for(deque<Str>::iterator iter = deq.begin(); iter!=deq.end(); iter++)
  44. {
  45. cout << "a = " << (*iter).a << ", b = " << (*iter).b << endl;
  46. }
  47. }

And some sample output is shown in the attached .png.
Looking at the png, you can see that there are several values which have an 'a' value of 2. These have successfully been sorted by their 'b' values.

Problem solved??

Cheers for now,
Jas.
Attached Thumbnails
SortedDeque.png  
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 33
Reputation: bunnyboy is an unknown quantity at this point 
Solved Threads: 1
bunnyboy's Avatar
bunnyboy bunnyboy is offline Offline
Light Poster
 
0
  #7
Oct 29th, 2009
Yes it does work! I'm very grateful to you ... TNX!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
1
  #8
Oct 29th, 2009
I just want to say that the deque class reminds me of Dairy Queen teehee.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 270 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC