| | |
Adding struct in container in sorted way
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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)
#include <iostream> #include <deque> #include <ctime> #include <cstdlib> using namespace std; struct Str { int a,b; Str(int aa, int bb): a(aa), b(bb){} }; void m_Add(const Str& str, deque<Str>& deq) { if (deq.empty()) deq.push_back(str); else { deque<Str>::iterator it = deq.begin(); for (int i = 0, size = deq.size(); i < size; i++) { if (str.a != deq[i].a) { it++; } else break; } for (int i = 0, size = deq.size(); i < size; i++) { if (str.b > deq[i].b) { it++; } else break; } deq.insert(it, str); } } int _tmain() { srand((unsigned)time(NULL)); deque<Str> deq; for (int i(0); i < 10; i++) { for (int j = 0; j < 15; j++) { Str a(rand()%3, rand()%6); m_Add(a,deq); } } for (int i = 0, size = deq.size(); i < size; i++) { cout << deq[i].a << " " << deq[i].b << endl; } return 0; }
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():
The version we will be using is in green (above).
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.
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 }; C++ Syntax (Toggle Plain Text)
#include<algorithm> #include<deque> #include<ctime> ... ... bool deq_test(Str& deq1, Str& deq2){Return (deq1.a < deq2.b);} ... Str MyStr; ... srand(time(NULL)); ... //Randomly populate ye' deque for(int i=0; i<6; i++) { MyStr.a = rand()%10; MyStr.b = rand()%10; deq.push_back(MyStr); } ... //Deque sorting goodness sort(deq.begin(), deq.end(), deq_test); //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.
0
#3 Oct 28th, 2009
Error - Line #6, Should be:
C++ Syntax (Toggle Plain Text)
bool deq_test(Str& deq1, Str& deq2){Return (deq1.a < deq2.a);}
0
#5 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
C++ Syntax (Toggle Plain Text)
bool deq_test(Str& deq1, Str& deq2) { if(deq1.a==deq2.a) { return(deq1.b<deq2.b); } else return(deq1.a<deq2.a); }
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....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
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:
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.
Here's a little program I knocked up, loosely based around previously posted code in this thread:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <algorithm> #include <deque> #include <ctime> #include <iterator> using namespace std; struct Str { int a,b; }; bool deq_test(Str& deq1, Str& deq2) { if(deq1.a==deq2.a) { return(deq1.b<deq2.b); } else return(deq1.a<deq2.a); } int main() { deque<Str> deq; srand(time(NULL)); Str MyStr; cout << "Generating values and populating deque:" << endl; for(int i=0; i<6; ++i) { MyStr.a = rand()%10; MyStr.b = rand()%10; cout << "a = " << MyStr.a << ", b = " << MyStr.b << endl; deq.push_back(MyStr); } cout << endl << "Sorting Deque..." << endl << endl; sort(deq.begin(), deq.end(), deq_test); cout << "Sorted values are: " << endl; for(deque<Str>::iterator iter = deq.begin(); iter!=deq.end(); iter++) { cout << "a = " << (*iter).a << ", b = " << (*iter).b << endl; } }
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.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
![]() |
Similar Threads
- Alphabetically sort lines in text file (C++)
- Orderby equivalent in VB Scripting (Visual Basic 4 / 5 / 6)
- Using UserControls in ASP.NET (ASP.NET)
- adding a JFrame component to JTabbedPane (Java)
- How to position buttons/boxes in a JTabbed Pane? (Java)
- how to reduce time required to add panel to container (Java)
- STL set vs. map (C++)
- Java Internal frames (Java)
Other Threads in the C++ Forum
- Previous Thread: help with binary search trees and saving them
- Next Thread: Urgent help neede! Need to extract speech transcript from video
Views: 270 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





I'm very grateful to you ... 