Quicksort for list of string

Discussion in 'C++' started by aparnakakkar2003@gmail.com, Apr 19, 2007.

  1. Guest

    can any one tell me if I give the followiing string in input:
    ABC
    abc
    BBC

    then how I can get
    ABC
    abc
    BBC


    or

    abc
    ABC
    BBC
    as my output usiing quicksort.
     
    , Apr 19, 2007
    #1
    1. Advertising

  2. On 19 Apr, 13:50, wrote:
    > can any one tell me if I give the followiing string in input:
    > ABC
    > abc
    > BBC
    >
    > then how I can get
    > ABC
    > abc
    > BBC
    >
    > or
    >
    > abc
    > ABC
    > BBC
    > as my output usiing quicksort.


    Don't know about quicksort but if it's sorting you want then given a
    number of strings read from some input store them in a std::vector and
    use std::sort() on them. Don't know the exact ordering of strings but
    I'd guess that capital letters are sorted before lowercase letters so
    the most likely result would be:

    ABC
    abc
    BBC

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 19, 2007
    #2
    1. Advertising

  3. Erik Wikström wrote:
    > On 19 Apr, 13:50, wrote:
    >> can any one tell me if I give the followiing string in input:
    >> ABC
    >> abc
    >> BBC
    >>
    >> then how I can get
    >> ABC
    >> abc
    >> BBC
    >>
    >> or
    >>
    >> abc
    >> ABC
    >> BBC
    >> as my output usiing quicksort.

    >
    > Don't know about quicksort but if it's sorting you want then given a
    > number of strings read from some input store them in a std::vector and
    > use std::sort() on them. Don't know the exact ordering of strings but
    > I'd guess that capital letters are sorted before lowercase letters so
    > the most likely result would be:
    >
    > ABC
    > abc
    > BBC


    *All* capital letters come before *any* lowercase ones. So, the result
    *should* be

    ABC
    BBC
    abc

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 19, 2007
    #3
  4. Guest

    On Apr 19, 6:42 pm, "Victor Bazarov" <> wrote:
    > Erik Wikström wrote:
    > > On 19 Apr, 13:50, wrote:
    > >> can any one tell me if I give the followiing string in input:
    > >> ABC
    > >> abc
    > >> BBC

    >
    > >> then how I can get
    > >> ABC
    > >> abc
    > >> BBC

    >
    > >> or

    >
    > >> abc
    > >> ABC
    > >> BBC
    > >> as my output usiing quicksort.

    >
    > > Don't know about quicksort but if it's sorting you want then given a
    > > number of strings read from some input store them in a std::vector and
    > > use std::sort() on them. Don't know the exact ordering of strings but
    > > I'd guess that capital letters are sorted before lowercase letters so
    > > the most likely result would be:

    >
    > > ABC
    > > abc
    > > BBC

    >
    > *All* capital letters come before *any* lowercase ones. So, the result
    > *should* be
    >
    > ABC
    > BBC
    > abc
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -


    but I want in that way only can you tell me how ,that sort function
    will not give this result.
     
    , Apr 19, 2007
    #4
  5. wrote:
    > On Apr 19, 6:42 pm, "Victor Bazarov" <> wrote:
    >> Erik Wikström wrote:
    >>> On 19 Apr, 13:50, wrote:
    >>>> can any one tell me if I give the followiing string in input:
    >>>> ABC
    >>>> abc
    >>>> BBC

    >>
    >>>> then how I can get
    >>>> ABC
    >>>> abc
    >>>> BBC

    >>
    >>>> or

    >>
    >>>> abc
    >>>> ABC
    >>>> BBC
    >>>> as my output usiing quicksort.

    >>
    >>> Don't know about quicksort but if it's sorting you want then given a
    >>> number of strings read from some input store them in a std::vector
    >>> and use std::sort() on them. Don't know the exact ordering of
    >>> strings but I'd guess that capital letters are sorted before
    >>> lowercase letters so the most likely result would be:

    >>
    >>> ABC
    >>> abc
    >>> BBC

    >>
    >> *All* capital letters come before *any* lowercase ones. So, the
    >> result
    >> *should* be
    >>
    >> ABC
    >> BBC
    >> abc
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask- Hide
    >> quoted text -
    >>
    >> - Show quoted text -

    >
    > but I want in that way only can you tell me how ,that sort function
    > will not give this result.


    Well, I wasn't replying to you, I was replyting to Erik.

    And, sorry, I don't understand the last sentence you posted. Could
    you perhaps rephrase it using a few shorter sentences?

    Neither C++ strings (objects of type 'std::string') nor C strings
    (arrays of char) can be sorted using 'quicksort' (generally speaking).

    To sort 'std::string' objects, use 'std::sort' (which most likely
    implements Quick Sort algorithm), or the 'sort' member of the 'list'
    container (if your strings are in a 'list' container).

    In order to use 'qsort' function, you'd have to define your own
    data structures, your own comparator, and then place your data in
    an array, and then call 'qsort'. If that's your how you want to do
    that, fine; it's not the best C++ way, however.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 19, 2007
    #5
  6. Jim Langston Guest

    <> wrote in message
    news:...
    > can any one tell me if I give the followiing string in input:
    > ABC
    > abc
    > BBC
    >
    > then how I can get
    > ABC
    > abc
    > BBC
    >
    >
    > or
    >
    > abc
    > ABC
    > BBC
    > as my output usiing quicksort.


    Well, this doesn't use quicksort but std::sort which is O( N log N ).

    Output is:

    Before sort:
    ABC
    abc
    BBC
    ABCD
    aB

    After sort:
    ABC
    ABCD
    BBC
    aB
    abc

    After Case Insensitive sort:
    aB
    ABC
    abc
    ABCD
    BBC

    There may be a better/faster algorithm for the case insensitve comparing of
    std::strings. I just did this one rather quickly.

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cctype>

    bool CILessthan( const std::string Elem1, const std::string Elem2 )
    {
    for ( std::size_t i = 0; i < Elem1.length(); ++i )
    {
    if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    return true;
    else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    return false;
    }

    // Equal up to this point, but one may be longer
    if ( Elem1.length() < Elem2.length() )
    return true;

    return false;
    }

    void ShowData( const std::vector<std::string>& Data )
    {
    for ( std::vector<std::string>::const_iterator it = Data.begin(); it !=
    Data.end(); ++it )
    std::cout << (*it) << "\n";
    }

    int main()
    {
    std::vector<std::string> Data;
    Data.push_back( "ABC" );
    Data.push_back( "abc" );
    Data.push_back( "BBC" );
    Data.push_back( "ABCD" );
    Data.push_back( "aB" );

    std::cout << "Before sort:\n";
    ShowData( Data );

    std::sort( Data.begin(), Data.end() );
    std::cout << "\nAfter sort:\n";
    ShowData( Data );

    std::sort( Data.begin(), Data.end(), CILessthan );
    std::cout << "\nAfter Case Insensitive sort:\n";
    ShowData( Data );

    std::string wait;
    std::getline( std::cin, wait );
    }
     
    Jim Langston, Apr 19, 2007
    #6
  7. Guest

    On Apr 19, 8:20 pm, "Jim Langston" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > can any one tell me if I give the followiing string in input:
    > > ABC
    > > abc
    > > BBC

    >
    > > then how I can get
    > > ABC
    > > abc
    > > BBC

    >
    > > or

    >
    > > abc
    > > ABC
    > > BBC
    > > as my output usiing quicksort.

    >
    > Well, this doesn't use quicksort but std::sort which is O( N log N ).
    >
    > Output is:
    >
    > Before sort:
    > ABC
    > abc
    > BBC
    > ABCD
    > aB
    >
    > After sort:
    > ABC
    > ABCD
    > BBC
    > aB
    > abc
    >
    > After Case Insensitive sort:
    > aB
    > ABC
    > abc
    > ABCD
    > BBC
    >
    > There may be a better/faster algorithm for the case insensitve comparing of
    > std::strings. I just did this one rather quickly.
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    > #include <algorithm>
    > #include <cctype>
    >
    > bool CILessthan( const std::string Elem1, const std::string Elem2 )
    > {
    > for ( std::size_t i = 0; i < Elem1.length(); ++i )
    > {
    > if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    > return true;
    > else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    > return false;
    > }
    >
    > // Equal up to this point, but one may be longer
    > if ( Elem1.length() < Elem2.length() )
    > return true;
    >
    > return false;
    >
    > }
    >
    > void ShowData( const std::vector<std::string>& Data )
    > {
    > for ( std::vector<std::string>::const_iterator it = Data.begin(); it !=
    > Data.end(); ++it )
    > std::cout << (*it) << "\n";
    >
    > }
    >
    > int main()
    > {
    > std::vector<std::string> Data;
    > Data.push_back( "ABC" );
    > Data.push_back( "abc" );
    > Data.push_back( "BBC" );
    > Data.push_back( "ABCD" );
    > Data.push_back( "aB" );
    >
    > std::cout << "Before sort:\n";
    > ShowData( Data );
    >
    > std::sort( Data.begin(), Data.end() );
    > std::cout << "\nAfter sort:\n";
    > ShowData( Data );
    >
    > std::sort( Data.begin(), Data.end(), CILessthan );
    > std::cout << "\nAfter Case Insensitive sort:\n";
    > ShowData( Data );
    >
    > std::string wait;
    > std::getline( std::cin, wait );
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    please tell me how is it possible with (std::list<std::string> data)
    as an argument
     
    , Apr 19, 2007
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > On Apr 19, 8:20 pm, "Jim Langston" <> wrote:
    >> <> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >>
    >>
    >> > can any one tell me if I give the followiing string in input:
    >> > ABC
    >> > abc
    >> > BBC

    >>
    >> > then how I can get
    >> > ABC
    >> > abc
    >> > BBC

    >>
    >> > or

    >>
    >> > abc
    >> > ABC
    >> > BBC
    >> > as my output usiing quicksort.

    >>
    >> Well, this doesn't use quicksort but std::sort which is O( N log N ).
    >>
    >> Output is:
    >>
    >> Before sort:
    >> ABC
    >> abc
    >> BBC
    >> ABCD
    >> aB
    >>
    >> After sort:
    >> ABC
    >> ABCD
    >> BBC
    >> aB
    >> abc
    >>
    >> After Case Insensitive sort:
    >> aB
    >> ABC
    >> abc
    >> ABCD
    >> BBC
    >>
    >> There may be a better/faster algorithm for the case insensitve comparing
    >> of
    >> std::strings. I just did this one rather quickly.
    >>
    >> #include <iostream>
    >> #include <string>
    >> #include <vector>
    >> #include <algorithm>
    >> #include <cctype>
    >>
    >> bool CILessthan( const std::string Elem1, const std::string Elem2 )
    >> {
    >> for ( std::size_t i = 0; i < Elem1.length(); ++i )
    >> {
    >> if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    >> return true;
    >> else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    >> return false;
    >> }
    >>
    >> // Equal up to this point, but one may be longer
    >> if ( Elem1.length() < Elem2.length() )
    >> return true;
    >>
    >> return false;
    >>
    >> }
    >>
    >> void ShowData( const std::vector<std::string>& Data )
    >> {
    >> for ( std::vector<std::string>::const_iterator it = Data.begin(); it
    >> !=
    >> Data.end(); ++it )
    >> std::cout << (*it) << "\n";
    >>
    >> }
    >>
    >> int main()
    >> {
    >> std::vector<std::string> Data;
    >> Data.push_back( "ABC" );
    >> Data.push_back( "abc" );
    >> Data.push_back( "BBC" );
    >> Data.push_back( "ABCD" );
    >> Data.push_back( "aB" );
    >>
    >> std::cout << "Before sort:\n";
    >> ShowData( Data );
    >>
    >> std::sort( Data.begin(), Data.end() );
    >> std::cout << "\nAfter sort:\n";
    >> ShowData( Data );
    >>
    >> std::sort( Data.begin(), Data.end(), CILessthan );
    >> std::cout << "\nAfter Case Insensitive sort:\n";
    >> ShowData( Data );
    >>
    >> std::string wait;
    >> std::getline( std::cin, wait );
    >>
    >>
    >>
    >> }- Hide quoted text -
    >>
    >> - Show quoted text -- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > please tell me how is it possible with (std::list<std::string> data)
    > as an argument


    Just replace
    std::sort( Data.begin(), Data.end(); CILessthan );
    with
    Data.sort( CILessthan );

    As long as it's a list and not a vector.
     
    Jim Langston, Apr 19, 2007
    #8
  9. James Kanze Guest

    On Apr 19, 3:42 pm, "Victor Bazarov" <> wrote:
    > Erik Wikström wrote:
    > > On 19 Apr, 13:50, wrote:
    > >> can any one tell me if I give the followiing string in input:
    > >> ABC
    > >> abc
    > >> BBC


    > >> then how I can get
    > >> ABC
    > >> abc
    > >> BBC


    > >> or


    > >> abc
    > >> ABC
    > >> BBC
    > >> as my output usiing quicksort.


    > > Don't know about quicksort but if it's sorting you want then given a
    > > number of strings read from some input store them in a std::vector and
    > > use std::sort() on them. Don't know the exact ordering of strings but
    > > I'd guess that capital letters are sorted before lowercase letters so
    > > the most likely result would be:


    > > ABC
    > > abc
    > > BBC


    > *All* capital letters come before *any* lowercase ones.


    That's actually platform dependant (and so off-topic here:).
    On an IBM mainframe, capital letters come after lowercase (and
    there are some punctuation signs which show up in the middle of
    the alphabet). On my system (Solaris), some of the capitals
    even have negative values---forcing them to an unsigned char
    results in two blocks of capitals, and two of small letters,
    interleaved (with some punctuation and some control characters
    between them).

    That's why when the order is significant to a human being,
    you'll almost always use a locale specific collating function.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 19, 2007
    #9
  10. Guest

    On Apr 19, 10:21 pm, James Kanze <> wrote:
    > On Apr 19, 3:42 pm, "Victor Bazarov" <> wrote:
    >
    >
    >
    >
    >
    > > Erik Wikström wrote:
    > > > On 19 Apr, 13:50, wrote:
    > > >> can any one tell me if I give the followiing string in input:
    > > >> ABC
    > > >> abc
    > > >> BBC
    > > >> then how I can get
    > > >> ABC
    > > >> abc
    > > >> BBC
    > > >> or
    > > >> abc
    > > >> ABC
    > > >> BBC
    > > >> as my output usiing quicksort.
    > > > Don't know about quicksort but if it's sorting you want then given a
    > > > number of strings read from some input store them in a std::vector and
    > > > use std::sort() on them. Don't know the exact ordering of strings but
    > > > I'd guess that capital letters are sorted before lowercase letters so
    > > > the most likely result would be:
    > > > ABC
    > > > abc
    > > > BBC

    > > *All* capital letters come before *any* lowercase ones.

    >
    > That's actually platform dependant (and so off-topic here:).
    > On an IBM mainframe, capital letters come after lowercase (and
    > there are some punctuation signs which show up in the middle of
    > the alphabet). On my system (Solaris), some of the capitals
    > even have negative values---forcing them to an unsigned char
    > results in two blocks of capitals, and two of small letters,
    > interleaved (with some punctuation and some control characters
    > between them).
    >
    > That's why when the order is significant to a human being,
    > you'll almost always use a locale specific collating function.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -
    >
    > - Show quoted text -


    can any one tell me i f the code below will work or not

    void QuickSortList(VR_STRINGList::iterator
    pLeft,VR_STRINGList::iterator pRight)
    {
    VR_STRINGList ::iterator pStart;
    VR_STRINGList ::iterator pCurrent;
    // VR_STRINGList ::iterator nCopyInteger; // If the left and
    right pointers are the same, then return

    VR_STRING nCopyInteger;

    if (pLeft == pRight)
    return;

    pStart = pLeft;
    pCurrent = pStart++; // Loop forever (well until we get to the
    right)
    while (1)
    {
    if(strcmpi(pStart->c_str(),pCurrent->c_str())<0)
    {


    nCopyInteger = *pCurrent;
    *pCurrent = *pStart;
    *pStart = nCopyInteger;
    }
    if (pCurrent == pRight)
    break;
    pCurrent = pCurrent++;
    }

    nCopyInteger = *pLeft;
    *pLeft = *pCurrent;
    *pCurrent= nCopyInteger;

    VR_STRINGList ::iterator pOldCurrent;


    *pOldCurrent = *pCurrent;
    pCurrent = pCurrent--;

    if (pCurrent != NULL)
    {
    if ((pLeft-- != pCurrent) && (pCurrent++ != pLeft))
    QuickSortList(pLeft,pCurrent);
    }

    pCurrent = pOldCurrent;
    pCurrent = pCurrent++;
    if (pCurrent != NULL)
    {
    if ((pCurrent-- != pRight) && (pRight++ != pCurrent))
    QuickSortList(pCurrent, pRight);
    }
    }
    Tell me problem please I am not getting the output.
     
    , Apr 20, 2007
    #10
  11. Jim Langston Guest

    On Apr 19, 9:48 pm, "Jim Langston" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > On Apr 19, 8:20 pm, "Jim Langston" <> wrote:
    > >> <> wrote in message

    >
    > >>news:...

    >
    > >> > can any one tell me if I give the followiing string in input:
    > >> > ABC
    > >> > abc
    > >> > BBC

    >
    > >> > then how I can get
    > >> > ABC
    > >> > abc
    > >> > BBC

    >
    > >> > or

    >
    > >> > abc
    > >> > ABC
    > >> > BBC
    > >> > as my output usiing quicksort.

    >
    > >> Well, this doesn't use quicksort but std::sort which is O( N log N ).

    >
    > >> Output is:

    >
    > >> Before sort:
    > >> ABC
    > >> abc
    > >> BBC
    > >> ABCD
    > >> aB

    >
    > >> After sort:
    > >> ABC
    > >> ABCD
    > >> BBC
    > >> aB
    > >> abc

    >
    > >> After Case Insensitive sort:
    > >> aB
    > >> ABC
    > >> abc
    > >> ABCD
    > >> BBC

    >
    > >> There may be a better/faster algorithm for the case insensitve
    > >> comparing
    > >> of
    > >> std::strings. I just did this one rather quickly.

    >
    > >> #include <iostream>
    > >> #include <string>
    > >> #include <vector>
    > >> #include <algorithm>
    > >> #include <cctype>

    >
    > >> bool CILessthan( const std::string Elem1, const std::string Elem2 )
    > >> {
    > >> for ( std::size_t i = 0; i < Elem1.length(); ++i )
    > >> {
    > >> if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    > >> return true;
    > >> else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    > >> return false;
    > >> }

    >
    > >> // Equal up to this point, but one may be longer
    > >> if ( Elem1.length() < Elem2.length() )
    > >> return true;

    >
    > >> return false;

    >
    > >> }

    >
    > >> void ShowData( const std::vector<std::string>& Data )
    > >> {
    > >> for ( std::vector<std::string>::const_iterator it = Data.begin();
    > >> it
    > >> !=
    > >> Data.end(); ++it )
    > >> std::cout << (*it) << "\n";

    >
    > >> }

    >
    > >> int main()
    > >> {
    > >> std::vector<std::string> Data;
    > >> Data.push_back( "ABC" );
    > >> Data.push_back( "abc" );
    > >> Data.push_back( "BBC" );
    > >> Data.push_back( "ABCD" );
    > >> Data.push_back( "aB" );

    >
    > >> std::cout << "Before sort:\n";
    > >> ShowData( Data );

    >
    > >> std::sort( Data.begin(), Data.end() );
    > >> std::cout << "\nAfter sort:\n";
    > >> ShowData( Data );

    >
    > >> std::sort( Data.begin(), Data.end(), CILessthan );
    > >> std::cout << "\nAfter Case Insensitive sort:\n";
    > >> ShowData( Data );

    >
    > >> std::string wait;
    > >> std::getline( std::cin, wait );

    >
    > >> }- Hide quoted text -

    >
    > >> - Show quoted text -- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > please tell me how is it possible with (std::list<std::string> data)
    > > as an argument

    >
    > Just replace
    > std::sort( Data.begin(), Data.end(); CILessthan );
    > with
    > Data.sort( CILessthan );
    >
    > As long as it's a list and not a vector.- Hide quoted text -


    Its not working I want Data to be of type " std::list<std::string>
    Data;"

    --------

    Please don't send questions to my e-mail, post them here.

    In this program if you replace every occurance of "vector" with "list",
    change
    std::sort( Data.begin(), Data.end(); CILessthan );
    with
    Data.sort( CILessthan );

    and compile it should run fine.
     
    Jim Langston, Apr 20, 2007
    #11
  12. Guest

    On Apr 20, 8:37 am, "Jim Langston" <> wrote:
    > On Apr 19, 9:48 pm, "Jim Langston" <> wrote:
    >
    >
    >
    >
    >
    > > <> wrote in message

    >
    > >news:...

    >
    > > > On Apr 19, 8:20 pm, "Jim Langston" <> wrote:
    > > >> <> wrote in message

    >
    > > >>news:...

    >
    > > >> > can any one tell me if I give the followiing string in input:
    > > >> > ABC
    > > >> > abc
    > > >> > BBC

    >
    > > >> > then how I can get
    > > >> > ABC
    > > >> > abc
    > > >> > BBC

    >
    > > >> > or

    >
    > > >> > abc
    > > >> > ABC
    > > >> > BBC
    > > >> > as my output usiing quicksort.

    >
    > > >> Well, this doesn't use quicksort but std::sort which is O( N log N ).

    >
    > > >> Output is:

    >
    > > >> Before sort:
    > > >> ABC
    > > >> abc
    > > >> BBC
    > > >> ABCD
    > > >> aB

    >
    > > >> After sort:
    > > >> ABC
    > > >> ABCD
    > > >> BBC
    > > >> aB
    > > >> abc

    >
    > > >> After Case Insensitive sort:
    > > >> aB
    > > >> ABC
    > > >> abc
    > > >> ABCD
    > > >> BBC

    >
    > > >> There may be a better/faster algorithm for the case insensitve
    > > >> comparing
    > > >> of
    > > >> std::strings. I just did this one rather quickly.

    >
    > > >> #include <iostream>
    > > >> #include <string>
    > > >> #include <vector>
    > > >> #include <algorithm>
    > > >> #include <cctype>

    >
    > > >> bool CILessthan( const std::string Elem1, const std::string Elem2 )
    > > >> {
    > > >> for ( std::size_t i = 0; i < Elem1.length(); ++i )
    > > >> {
    > > >> if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    > > >> return true;
    > > >> else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    > > >> return false;
    > > >> }

    >
    > > >> // Equal up to this point, but one may be longer
    > > >> if ( Elem1.length() < Elem2.length() )
    > > >> return true;

    >
    > > >> return false;

    >
    > > >> }

    >
    > > >> void ShowData( const std::vector<std::string>& Data )
    > > >> {
    > > >> for ( std::vector<std::string>::const_iterator it = Data.begin();
    > > >> it
    > > >> !=
    > > >> Data.end(); ++it )
    > > >> std::cout << (*it) << "\n";

    >
    > > >> }

    >
    > > >> int main()
    > > >> {
    > > >> std::vector<std::string> Data;
    > > >> Data.push_back( "ABC" );
    > > >> Data.push_back( "abc" );
    > > >> Data.push_back( "BBC" );
    > > >> Data.push_back( "ABCD" );
    > > >> Data.push_back( "aB" );

    >
    > > >> std::cout << "Before sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::sort( Data.begin(), Data.end() );
    > > >> std::cout << "\nAfter sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::sort( Data.begin(), Data.end(), CILessthan );
    > > >> std::cout << "\nAfter Case Insensitive sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::string wait;
    > > >> std::getline( std::cin, wait );

    >
    > > >> }- Hide quoted text -

    >
    > > >> - Show quoted text -- Hide quoted text -

    >
    > > >> - Show quoted text -

    >
    > > > please tell me how is it possible with (std::list<std::string> data)
    > > > as an argument

    >
    > > Just replace
    > > std::sort( Data.begin(), Data.end(); CILessthan );
    > > with
    > > Data.sort( CILessthan );

    >
    > > As long as it's a list and not a vector.- Hide quoted text -

    >
    > Its not working I want Data to be of type " std::list<std::string>
    > Data;"
    >
    > --------
    >
    > Please don't send questions to my e-mail, post them here.
    >
    > In this program if you replace every occurance of "vector" with "list",
    > change
    > std::sort( Data.begin(), Data.end(); CILessthan );
    > with
    > Data.sort( CILessthan );
    >
    > and compile it should run fine.- Hide quoted text -
    >
    > - Show quoted text -


    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cctype>

    #include <list>
    #include <iostream>
    using namespace std ;
    typedef list<string> VR_STRINGList;
    bool CILessthan( std::list<std::string>::iterator Elem1,
    std::list<std::string>::iterator Elem2 )
    {
    //std::list<std::string> ::iterator i;
    int i;
    int arrayLength = Elem1->length();
    for ( i = 0; i < arrayLength; ++i )
    {
    if ( strcmp(Elem1->c_str(),Elem2->c_str())<=0 )
    return true;
    else if ( strcmp(Elem1->c_str(), Elem2->c_str())>0)
    return false;
    }


    // Equal up to this point, but one may be longer
    if ( Elem1->length() < Elem2->length())
    return true;


    return false;



    }

    void ShowData( const std::list<std::string>& Data )
    {
    for ( std::list<std::string>::const_iterator it = Data.begin();
    it != Data.end(); ++it )
    std::cout << (*it) << "\n";


    }

    int main()
    {
    std::list<std::string> Data;
    Data.push_back( "ABC" );
    Data.push_back( "abc" );
    Data.push_back( "BBC" );
    std::cout << "Before sort:\n";
    ShowData( Data );


    // std::sort( Data.begin(), Data.end() );
    std::cout << "\nAfter sort:\n";
    ShowData( Data );

    // std::sort( Data.begin(), Data.end(), CILessthan );
    Data.sort(CILessthan);
    std::cout << "\nAfter Case Insensitive sort:\n";
    ShowData( Data );


    std::string wait;
    std::getline( std::cin, wait );


    }
    I have done changes above for list but still getting error
     
    , Apr 20, 2007
    #12
  13. Guest

    On Apr 20, 8:37 am, "Jim Langston" <> wrote:
    > On Apr 19, 9:48 pm, "Jim Langston" <> wrote:
    >
    >
    >
    >
    >
    > > <> wrote in message

    >
    > >news:...

    >
    > > > On Apr 19, 8:20 pm, "Jim Langston" <> wrote:
    > > >> <> wrote in message

    >
    > > >>news:...

    >
    > > >> > can any one tell me if I give the followiing string in input:
    > > >> > ABC
    > > >> > abc
    > > >> > BBC

    >
    > > >> > then how I can get
    > > >> > ABC
    > > >> > abc
    > > >> > BBC

    >
    > > >> > or

    >
    > > >> > abc
    > > >> > ABC
    > > >> > BBC
    > > >> > as my output usiing quicksort.

    >
    > > >> Well, this doesn't use quicksort but std::sort which is O( N log N ).

    >
    > > >> Output is:

    >
    > > >> Before sort:
    > > >> ABC
    > > >> abc
    > > >> BBC
    > > >> ABCD
    > > >> aB

    >
    > > >> After sort:
    > > >> ABC
    > > >> ABCD
    > > >> BBC
    > > >> aB
    > > >> abc

    >
    > > >> After Case Insensitive sort:
    > > >> aB
    > > >> ABC
    > > >> abc
    > > >> ABCD
    > > >> BBC

    >
    > > >> There may be a better/faster algorithm for the case insensitve
    > > >> comparing
    > > >> of
    > > >> std::strings. I just did this one rather quickly.

    >
    > > >> #include <iostream>
    > > >> #include <string>
    > > >> #include <vector>
    > > >> #include <algorithm>
    > > >> #include <cctype>

    >
    > > >> bool CILessthan( const std::string Elem1, const std::string Elem2 )
    > > >> {
    > > >> for ( std::size_t i = 0; i < Elem1.length(); ++i )
    > > >> {
    > > >> if ( std::tolower( Elem1 ) < std::tolower( Elem2 ) )
    > > >> return true;
    > > >> else if ( std::tolower( Elem1 ) > std::tolower( Elem2 ) )
    > > >> return false;
    > > >> }

    >
    > > >> // Equal up to this point, but one may be longer
    > > >> if ( Elem1.length() < Elem2.length() )
    > > >> return true;

    >
    > > >> return false;

    >
    > > >> }

    >
    > > >> void ShowData( const std::vector<std::string>& Data )
    > > >> {
    > > >> for ( std::vector<std::string>::const_iterator it = Data.begin();
    > > >> it
    > > >> !=
    > > >> Data.end(); ++it )
    > > >> std::cout << (*it) << "\n";

    >
    > > >> }

    >
    > > >> int main()
    > > >> {
    > > >> std::vector<std::string> Data;
    > > >> Data.push_back( "ABC" );
    > > >> Data.push_back( "abc" );
    > > >> Data.push_back( "BBC" );
    > > >> Data.push_back( "ABCD" );
    > > >> Data.push_back( "aB" );

    >
    > > >> std::cout << "Before sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::sort( Data.begin(), Data.end() );
    > > >> std::cout << "\nAfter sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::sort( Data.begin(), Data.end(), CILessthan );
    > > >> std::cout << "\nAfter Case Insensitive sort:\n";
    > > >> ShowData( Data );

    >
    > > >> std::string wait;
    > > >> std::getline( std::cin, wait );

    >
    > > >> }- Hide quoted text -

    >
    > > >> - Show quoted text -- Hide quoted text -

    >
    > > >> - Show quoted text -

    >
    > > > please tell me how is it possible with (std::list<std::string> data)
    > > > as an argument

    >
    > > Just replace
    > > std::sort( Data.begin(), Data.end(); CILessthan );
    > > with
    > > Data.sort( CILessthan );

    >
    > > As long as it's a list and not a vector.- Hide quoted text -

    >
    > Its not working I want Data to be of type " std::list<std::string>
    > Data;"
    >
    > --------
    >
    > Please don't send questions to my e-mail, post them here.
    >
    > In this program if you replace every occurance of "vector" with "list",
    > change
    > std::sort( Data.begin(), Data.end(); CILessthan );
    > with
    > Data.sort( CILessthan );
    >
    > and compile it should run fine.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks,its working fine.
     
    , Apr 20, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sarah
    Replies:
    2
    Views:
    3,292
    Roedy Green
    Oct 23, 2003
  2. hiwa
    Replies:
    4
    Views:
    684
    Esmond Pitt
    Mar 29, 2005
  3. Exekute

    c++ and quicksort

    Exekute, Sep 27, 2003, in forum: C++
    Replies:
    6
    Views:
    9,082
    Jerry Coffin
    Sep 28, 2003
  4. Baltazar007

    linked list quicksort

    Baltazar007, Nov 11, 2006, in forum: C Programming
    Replies:
    6
    Views:
    1,215
    Flash Gordon
    Nov 11, 2006
  5. Baltazar007

    linked list quicksort

    Baltazar007, Nov 11, 2006, in forum: C++
    Replies:
    6
    Views:
    1,444
    Flash Gordon
    Nov 11, 2006
Loading...

Share This Page