944,061 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 516
  • C++ RSS
Oct 28th, 2009
0

Adding struct in container in sorted way

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
bunnyboy is offline Offline
55 posts
since Apr 2009
Oct 28th, 2009
0
Re: Adding struct in container in sorted way
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).

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 28th, 2009
0
Re: Adding struct in container in sorted way
Error - Line #6, Should be:
C++ Syntax (Toggle Plain Text)
  1. bool deq_test(Str& deq1, Str& deq2){Return (deq1.a < deq2.a);}
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 29th, 2009
0
Re: Adding struct in container in sorted way
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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
bunnyboy is offline Offline
55 posts
since Apr 2009
Oct 29th, 2009
0
Re: Adding struct in container in sorted way
Click to Expand / Collapse  Quote originally posted by bunnyboy ...
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?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Oct 29th, 2009
1
Re: Adding struct in container in sorted way
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:
C++ Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	SortedDeque.png
Views:	19
Size:	9.1 KB
ID:	12365  
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Oct 29th, 2009
0
Re: Adding struct in container in sorted way
Yes it does work! I'm very grateful to you ... TNX!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
bunnyboy is offline Offline
55 posts
since Apr 2009
Oct 29th, 2009
1
Re: Adding struct in container in sorted way
I just want to say that the deque class reminds me of Dairy Queen teehee.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with binary search trees and saving them
Next Thread in C++ Forum Timeline: Urgent help neede! Need to extract speech transcript from video





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC