Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strings

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How would I create a function that receives an array of pointers to string charachters and sort them manipulating the pointers. Hope thats understood.
 
#include <iostream>
#include <string>

void SortArray( std::string* array[],int iElems );
void OutArray( std::string* array[],int iElems );

int main()
{
std::string first =&quot;abolone&quot;;
std::string second=&quot;bobby&quot;;
std::string third =&quot;caral&quot;;
std::string fourth=&quot;danny&quot;;
std::string *sArray[4];

sArray[0]=&second;
sArray[1]=&fourth;
sArray[2]=&first;
sArray[3]=&third;

std::cout << &quot;Before sort:&quot; <<
std::endl;
OutArray( sArray,4 );
std::cout << &quot;After sort:&quot; <<
std::endl;
SortArray( sArray,4 );
OutArray( sArray,4 );

return 0;
}

void SortArray( std::string* array[],int iElems )
{
std::string sTemp,sHold;
int iPass=1;
bool bExchanges=true;

while ( iPass < iElems && bExchanges )
{
bExchanges=false;
for ( int i=0;i<iElems-1;i++ )
{
sTemp=*array;
sHold=*array[i+1];

if( sTemp.compare( sHold )==1 )
{
sTemp =*array;
*array =*array[i+1];
*array[i+1]=sTemp;
bExchanges =true;
}

}

}

}

void OutArray( std::string* array[],int iElems )
{
for( int i=0;i<iElems;i++ )
{
std::cout << &quot;array[&quot; << i << &quot;]=&quot; << *array <<
std::endl;
}
}

Mike L.G.
mlg400@blazemail.com
 
You could also just use a CStringList type and do an insertion sort with it. CStringList has 2 member functions and they are InsertBefore and InsertAfter.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top