472,811 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

string sort, yet again

5
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  *bunch of code
  3. */
  4. struct album{
  5.        string album;
  6.        string artist[6];
  7.        string songs[21];
  8. }
  9. /* morecode */
  10. int main(){
  11.   album p;
  12. //more code
then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct

the problem is that in the txt file the songs are listed as follows
1. Track1
2. Track2
3. Not the actual track names
et cetera. on to Track21.

i have to sort based on song name.
i know i need to search each string for the '.'
then check the next character after the space.

(oh yeah, the sort is a function)
something along the lines of
Expand|Select|Wrap|Line Numbers
  1. void ssongssort(string ar[]){
  2.      bool done;
  3.      for(int r,done = 0; !done ; ) {
  4.              for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
  5.                  string temp1= ar[r].substr(5, 1);// the problem is here
  6.                  string temp2 = ar[r+1].substr(5, 1);// and here
  7.                  temp2.assign(temp2,4,15);
  8.                  if(temp1 > temp2){
  9.                           swap(ar[r],ar[r+1]);
  10.                           done = 0;
  11.                           }
  12.                  }
  13.              }
  14.      }
i've seen a nother way, something like
(imagine the strings as " - Track1")
Expand|Select|Wrap|Line Numbers
  1. string gettrack(string st){
  2.        int t = st.find (" - ",0) + 2;// why would lentgh be 0? 
  3.        while (st[t] == ' ') t++;// the 2 would be 2 characters down.
  4.        return st.substr(t);
  5.        }
  6. //which returns the location of the '-'
however i don't think
Expand|Select|Wrap|Line Numbers
  1. p.albumname[0].find(" - ",0)+2;
would work.
Mar 28 '07 #1
8 1918
Savage
1,764 Expert 1GB
i have an array of strings that need sorting,(not so much an array of strings but a struct that con tains an array of strings) technically the strings are already sorted.
an example is in order.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  *bunch of code
  3. */
  4. struct album{
  5.        string album;
  6.        string artist[6];
  7.        string songs[21];
  8. }
  9. /* morecode */
  10. int main(){
  11.   album p;
  12. //more code
  13. /*then i read in a text file that contains all the struct's info but puts it into an array, then a big horrible if statement that coppies the info from the array to the struct
  14.  
  15. the problem is that in the txt file the songs are listed as follows
  16.  1. Track1
  17.  2. Track2
  18.  3. Not the actual track names
  19. et cetera. on to Track21.
  20.  
  21. i have to sort based on song name.
  22. i know i need to search each string for the '.'
  23.  then check the next character after the space.
  24.  
  25. (oh yeah, the sort is a function)
  26. something along the lines of
  27. void ssongssort(string ar[]){
  28.      bool done;
  29.      for(int r,done = 0; !done ; ) {
  30.              for (done = ! ( r = 0 ) ; r < 20 ; r + + ) {
  31.                  string temp1= ar[r].substr(5, 1);// the problem is here
  32.                  string temp2 = ar[r+1].substr(5, 1);// and here
  33.                  temp2.assign(temp2,4,15);
  34.                  if(temp1 > temp2){
  35.                           swap(ar[r],ar[r+1]);
  36.                           done = 0;
  37.                           }
  38.                  }
  39.              }
  40.      }
  41.  
  42. i've seen a nother way, something like
  43. (imagine the strings as " - Track1")
  44. string gettrack(string st){
  45.        int t = st.find (" - ",0) + 2;// why would lentgh be 0? 
  46.        while (st[t] == ' ') t++;// the 2 would be 2 characters down.
  47.        return st.substr(t);
  48.        }
  49. //which returns the location of the '-'
however i don't think
p.albumname[0].find(" - ",0)+2;
would work.

Hi, pabl0

First when posting please use code tags(Just select your code copied/wriiten

here and click #),it makes code easier to read.

Secound i don't think that your for loops are good designed,Why are you using

double for loop? You could only use one do-while loop,like:

Expand|Select|Wrap|Line Numbers
  1. do{
  2.  
  3.                  string temp1= ar[r].substr(5, 1);
  4.                  string temp2 = ar[r+1].substr(5, 1);
  5.                  temp2.assign(temp2,4,15);
  6.                  if(temp1 > temp2){
  7.                           swap(ar[r],ar[r+1]);
  8.                           r++;
  9.                           done = 0;
  10.                 }else done =1;
  11.  
  12. while(done==0);
Hope this helps.

Savage
Mar 28 '07 #2
pabl0
5
it's just a bubble sort (and i've seen some mighty fine one line bubble sorts),
and besides thats not the problem.
the string search is.
Mar 28 '07 #3
Savage
1,764 Expert 1GB
Just one qeustion(Myabe I haven't understood you well):

Is the txt file with no actual track names one that you put in your arrays or

is that a output txt file
Mar 29 '07 #4
pabl0
5
someting along the lines of

ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
Apr 2 '07 #5
Savage
1,764 Expert 1GB
someting along the lines of

ifstream in ("songlist.dat");
string z;
int i = 1;
while (getline(in, z)) {
ar[i] = z;
cout << setw(3) << i++ << " " << z << "\n";
}
So correct me if I'm wrong:

It's the file inputed.You have writen one song in one line and used getline to
get song name.You also set field width to 3 using setw,and couted i, then
couted z and forced start of new line.

Can you post one line of songlist.dat just to see how is it formated?


Savage
Apr 2 '07 #6
pabl0
5
the .dat file is formattd as follows.

Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...

I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
Apr 2 '07 #7
Savage
1,764 Expert 1GB
the .dat file is formattd as follows.

Wolfgang Amadeus Mozart //0th element of array
1. Bassoon Concerto in B flat major, K. 191 //1st
2. Concerto for Harp, Flute and Orchestra, K. 299 //2nd
3. Oboe Concerto in C major, K. 314 //3rd
4. Concerto for Clarinet and Orchestra in A major, K. 622 // 4th
5. Flute Concerto No. 1 in G Major, K. 313 // and so on and so forth...

I'm not sure wether this is written corectly for my purpus:
string temp1= ar[r].substr(5, 1);
string temp2 = ar[r+1].substr(5, 1);
setting the temp values as the 5th character of the string,
then later compares those temp values.
Maybe,you don't even have to use strings,instead use char arrays and compare
first char array with another using same array index.
Apr 2 '07 #8
pabl0
5
if i knew C i'd mess with the char arrays, but i don't know C.
however i finally got it to work, for the most part.
Expand|Select|Wrap|Line Numbers
  1. void SongSort( album b[], int r){
  2.      string str[n];
  3.      for(int j = 0; j < 5; j++){
  4.              for(int m = 0; m < r; m++){
  5.                      if( b[j].songs[m] != ""){
  6.                           string temp88 = b[j].songs[m];
  7.                           temp88 = temp88.substr(4,temp88.length()-1); // starts at the 4th character
  8.                           str[m] = temp88;// couldn't have b[j].songs[m].substr(4,temp88.length()-1)
  9.                           b[j].songs[m] = temp88;
  10.                           }
  11.                      }
  12.              for(int i,done = 0; !done;){
  13.                      for (done = !(i = 0); i < r-1; i++){
  14.                          if (str[i] > str[i+1]){
  15.                                     swap(str[i], str[i+1]);
  16.                                     swap(b[j].songs[i], b[j].songs[i+1]), done = 0;
  17.                                     }
  18.                          }
  19.                      }
  20.              }
  21.      }
Apr 9 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
11
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function...
3
by: TituscroW | last post by:
Hello, This is my first post, so I hope I include all the right information. I have a Table which has a list of numbers stored in a string, which are not in numerical order For example "10 2 35...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
6
by: Don | last post by:
Is it possible to somehow extend the string class (i.e. add new members to it)?
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.