On Apr 20, 8:37 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
On Apr 19, 9:48 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
<aparnakakkar2. ..@gmail.comwro te in message
news:11******** **************@ n76g2000hsh.goo glegroups.com.. .
On Apr 19, 8:20 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
><aparnakakkar2 ...@gmail.comwr ote in message
>>news:11****** *************** @p77g2000hsh.go oglegroups.com. ..
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[i] ) < std::tolower( Elem2[i] ) )
> return true;
> else if ( std::tolower( Elem1[i] ) std::tolower( Elem2[i] ) )
> 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>::cons t_iterator it = Data.begin();
>it
>!=
>Data.end(); ++it )
> std::cout << (*it) << "\n";
>}
>int main()
>{
> std::vector<std ::stringData;
> 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: :stringdata)
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<stringVR_S TRINGList;
bool CILessthan( std::list<std:: string>::iterat or Elem1,
std::list<std:: string>::iterat or Elem2 )
{
//std::list<std:: string::iterato r i;
int i;
int arrayLength = Elem1->length();
for ( i = 0; i < arrayLength; ++i )
{
if ( strcmp(Elem1->c_str(),Elem 2->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:: stringData;
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(CILes sthan);
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