Thread: i am really stuck, i need hlp plz

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Post i am really stuck, i need hlp plz

    i am writing a program, one part of this program consists of a function that needs to alphabetize 100 words (strings) that were read in from a file before, i am simply looking for a function besides (basic_string::compare()), some simple function that has a capability to put the words in alphabetized manner. Right now i am playing around with different string manipulators like strcmp(str1, str2) but it's taking me too many different checks and if statments, or maybe just a simple function that analizes every character of the string...???

    i am also looking for a function that specifies where the output to the file could be placed, for example: if you want to put a string "hello" at the beginning of the 15th column of the 7th row...???

    i am working on a qsort() now, it seems like it doesn't want to do the job

    please post something....????
    you can e-mail me at [email protected]

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to use STL it can be done like this -

    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<string> mystrings(3);
    
        mystrings[0] = "cccc";
        mystrings[1] = "bbbb";
        mystrings[2] = "aaaa";
    
        sort(mystrings.begin(),mystrings.end());
    
        for (int i=0;i<3;i++)
            cout << mystrings[i] << endl;
    
    
    
        return 0;
    }
    
    

    If you want to do with quicksort, I don't know why a strcmp type function wouldn't work. Perhaps you could post some code.

    I'm not sure what you mean by your second queston. Are you trying to sort the strings according to where they should be outputted in the file? If so you could map a number to each string a sort the strings by this number.

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Thumbs up nice.. nice....

    i am pretty impressed not too many people cam up with the solution that would be as close to the correct one as you....
    i appreciate it, thanx

    i was playing around with it, it works pretty good but not perfect i think i have to truncate every entry to the same amount of bytes and make all strings lowercase b/c here and there i get bugs, certain words are not on its place....

    what do you think...???

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you use the resize() member function then you can truncate the strings (you may lose data) and looping through the string using tolower() will convert to lowercase (if you know that only the first letter of a string will be uppercase you won't have to loop through the whole string) -

    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        vector<string> mystrings(3);
    
        mystrings[0] = "CCCC";
        mystrings[1] = "BBBB";
        mystrings[2] = "AAAA";
    
        sort(mystrings.begin(),mystrings.end());
    
        for (int i=0;i<3;i++)
        {
            for (int j=0;j<mystrings[i].size();j++)
            {
                mystrings[i][j] = tolower(mystrings[i][j]);
            }
            mystrings[i].resize(2);
            cout << mystrings[i] << endl;
        }
    
    
    
        return 0;
    }
    
    

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    You might want to use a list rather than a vector.
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Witch King is right in that you may want to use lists for sorting non-built in types(such as string) as the list sort is quicker as it uses pointers, however they are a bit more awkward to set up so if speed isn't critical (or you are using built in types) then you may prefer vectors. Here's the above done with a STL list.

    Code:
    #include <string>
    #include <iostream>
    #include <list>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        list<string> mystrings;
    
        list<string>::iterator i,listit = mystrings.begin();
    
        mystrings.insert(listit++,1,"CCCC");
        mystrings.insert(listit++,1,"BBBB");
        mystrings.insert(listit++,1,"AAAA");
    
        mystrings.sort();
    
        for ( i=mystrings.begin();i!=mystrings.end();++i)
        {
            for (int j=0;j<i->size();j++)
            {
                (*i)[j] = tolower((*i)[j]);
            }
            i->resize(2);
            cout << *i << endl;
        }
    
        return 0;
    }
    
    

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Using the standard template library is a real help in a number of projects, however (IMO) it doesn't substitute for knowledge of what is happening behind the scenes. Knowing how to compare c_style strings in addition to STL strings will (undoubtably) be important to anyone who does any serious C++ programming (unlike duffers like myself).

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Knowing how to compare c_style strings in addition to STL strings will (undoubtably) be important to anyone who does any serious C++ programming
    Libraries are a large component of C++ and also need to be studied. It seems that managed langauges prevade professional programming. I agree with you though, that knowing C is a great asset.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-31-2008, 10:00 AM
  2. Plz help stuck
    By kennny2004 in forum C++ Programming
    Replies: 7
    Last Post: 09-27-2005, 01:27 AM
  3. I'm stuck with function, help plz
    By playboy1620 in forum C Programming
    Replies: 7
    Last Post: 03-27-2002, 08:23 AM
  4. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:58 PM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM