![]() |
| ||
| Vectors, Functions, and Sorting... oh my! Yeah, it's your typical homework help topic, but the problem is this is stuff we NEVER COVERED IN CLASS and it was NOT IN OUR BOOK, even our TA can't figure out why our professor assigned this to our 101 class. I have a good chunk of it done, I think. The stuff we DID learn in class. But of course, the one part I don't know where to begin on (aka the one part that's not in our book at all or discussed in lecture) is the only part our TA was specifically told he could NOT help us with... It involves functions and vectors. Basically we need to be able to sort a chunk of input like this: 3 4 2 5 1 5 2 5 1 6 3 2 2 4 5 6 So that it sorts it by whatever column that is specified, and if the value is the same in two or more rows, it goes onto sorting by the next column specified, and so on so forth. We just repeat it a bunch of times with a different column order and send it to an output file, actually a different output for each sort. So here's the program so far: //This part makes sense, the basic stuff So yeah. I just don't know where to begin on that one part, nor what to look for in a tutorial... I just keep getting how-to guides on sorting just one vector. I just don't know how to get the input from the first part sorted.. As the code stands now it gives errors, which doesn't surprise me. Probably due to the fact that just that one piece is missing, really. But here it is in case it's something more: c:\documents and settings\chris\my documents\ass4mod.cpp(72) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,st d::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information c:\documents and settings\chris\my documents\ass4mod.cpp(72) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::bas ic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information c:\documents and settings\chris\my documents\ass4mod.cpp(80) : error C2065: 'v2' : undeclared identifier c:\documents and settings\chris\my documents\ass4mod.cpp(56) : see reference to function template instantiation 'void __cdecl SORT(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,clas s std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocat or<int> >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocator<int> >)' being compiled Error executing cl.exe. 1 error(s), 2 warning(s) Also, once I get that one chunk of code... would I have to repeat the code X number of times for X different output files, or is there a way to put it in a loop but write to a different output file each time? If it's possible, I think I know what to do for it, but I'm more concerned with the program itself right now.... I've been working on this for quite a while so any help is greatly appriciated. |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Will this work to begin sorting in descending order by vector? if (int y = 0; int z = 1; O[y] > O[z]; y++; z++;) Or do I need to do something radically different? |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Do you have to use vector? If you can use list instead, that has built-in sorting functionality, all you need is to pass the sort function of list a pointer to a boolean function that compares 2 elements. |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Yeah, we have to use vectors - that's about the only certain bit of info we were given. This is what I have now, and it looks promising (if not lenghty...) [php] for (z == size() ); { int y = 0; int z = 1; int c; if (O[y] > O[z]); { c = 1; return 0; } else if (O[y] < O[z]); { c = -1; return 0; } else { if (Amino[y] > Amino[z]); { c = 1; return 0; } else if (Amino[y] < Amino[z]); { c = -1; return 0; } else { if (C[y] > C[z]); { c = 1; return 0; } else if (C[y] < C[z]); { c = -1; return 0; } else { if (N[y] > N[z]); { c = 1; return 0; } else if (N[y] < N[z]); { c = -1; return 0; } else {if (S[y] > S[z]); { c = 1; return 0; } else if (S[y] < S[z]); { return 0; } else {if (H[y] > H[z]); { c = 1; return 0; } else if (H[y] < H[z]); { c = -1; return 0; } if (c = -1); { hold = O[z]; O[z] = O[y]; O[y] = hold; hold = Amino[z]; Amino[z] = Amino[y]; Amino[y] = hold; hold = C[z]; C[z] = C[y]; C[y] = hold; hold = N[z]; N[z] = N[y]; N[y] = hold; hold = S[z]; S[z] = S[y]; S[y] = hold; hold = H[z]; H[z] = H[y]; H[y] = hold; } } } y++; z++; return 0; } [/php] Does this look about right to sort in descending order yet keep the horizontal rows the same (merely shifting their position)? The only reason I'm hesitant to try it out is I have to do 6 iterations of this sorting order, and it'll get really long REALLY fast so there must be a shorter way. Not to mention, I'm not sure where exactly this is supposed to go in my program other than "towards the bottom..." part of me doesn't want to monkey around with what I was already given but it doesn't seem to work otherwise.. |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Deeply nested conditionals always tick me off. There's always (well, almost) a better (read, cleaner) way of doing things. |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Another chunk of code based off some help I was given: #include <algorithm>Only error I get is "unexpected end of file" but other than that, will this give me what I want? |
| ||
| Re: Vectors, Functions, and Sorting... oh my! Have you thought of using the sort algorithm in the stl, and a compare func struct containerCompare { bool operator()(const Type& p1, const Type& p2) const { //compare your elements here } }; std::sort(container.begin(),container.end(),containerCompare()); |
| All times are GMT -4. The time now is 10:30 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC