sorting a vector of LPTSTR

This is a discussion on sorting a vector of LPTSTR within the C++ Programming forums, part of the General Programming Boards category; I have a vector of LPTSTRs that I want to sort. I used the STL sort algorithm, and it sorted ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    sorting a vector of LPTSTR

    I have a vector of LPTSTRs that I want to sort. I used the STL sort algorithm, and it sorted it alright, but it only sorted by the pointer addresses of the vector elements.

    How can I have the sort algorithm sort by the contents of the string instead of the pointer value that it's being represented by?


    here is my code below, if you wanna look at it:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <vector>
    #include <tchar.h>
    #include <strsafe.h>
    #include <algorithm>
    
    void sort_ascending( std::vector<LPTSTR>& inputVector );
    
    int main()
    {
    	DWORD				numberOfBytesWritten		= 0;
    	DWORD				numberOfBytesToWrite		= 0;
    	size_t				numberOfBytesToWriteSize_t	= 0;
    	std::vector<LPTSTR> inputVectorString;
    	
    	LPTSTR newline = TEXT("\n");
    	DWORD sizeOfNewline = 0;
    	size_t sizeOfNewlineSize_t = 0;
    	StringCbLength(
    		newline,
    		10,
    		&sizeOfNewlineSize_t);
    	sizeOfNewline = sizeOfNewlineSize_t;
    
    	inputVectorString.push_back(TEXT("9"));
    	inputVectorString.push_back(TEXT("6"));
    	inputVectorString.push_back(TEXT("8"));
    
    	HANDLE hFile = CreateFile(
    		TEXT("sampleOutputFile.txt"),
    		GENERIC_WRITE,
    		FILE_SHARE_READ|
    		FILE_SHARE_WRITE,
    		NULL,
    		CREATE_ALWAYS,
    		NULL,
    		NULL);
    
    	for ( int i = 0; i<inputVectorString.size(); i++ )
    	{
    		numberOfBytesToWrite		= 0;
    		numberOfBytesToWriteSize_t	= 0;
    
    		HRESULT hResult = StringCbLength(
    			inputVectorString[i],
    			2048,
    			&numberOfBytesToWriteSize_t);
    
    		numberOfBytesToWrite = numberOfBytesToWriteSize_t;
    
    		WriteFile(
    			hFile,
    			inputVectorString[i],
    			numberOfBytesToWrite,
    			&numberOfBytesWritten,
    			NULL);
    
    		numberOfBytesToWrite = sizeOfNewline;
    
    		WriteFile(
    			hFile,
    			newline,
    			numberOfBytesToWrite,
    			&numberOfBytesWritten,
    			NULL);
    	}
    
    	CloseHandle(hFile);
    
    	_tprintf(TEXT("File created\n"));
    
    
    
    //==========================================================================
    
    
    
    
    	_tprintf(TEXT("Reading file:\n"));
    
    	std::vector<LPTSTR> outputVector;
    
    	HANDLE hFile2 = CreateFile(
    		TEXT("sampleOutputFile.txt"),
    		GENERIC_READ,
    		FILE_SHARE_READ|
    		FILE_SHARE_WRITE,
    		NULL,
    		OPEN_EXISTING,
    		NULL,
    		NULL);
    
    	LPTSTR inputBuffer;
    
    	DWORD numberOfBytesRead = 0;
    
    	DWORD fileSizeHigh = 0;
    	DWORD fileSize = GetFileSize(
    		hFile2,
    		&fileSizeHigh);
    
    	inputBuffer = new TCHAR[(fileSize / sizeof(TCHAR)) + 1];
    
    	ReadFile(
    		hFile2,
    		inputBuffer,
    		fileSize,
    		&numberOfBytesRead,
    		NULL);
    
    	std::vector<DWORD> newlineIndicies;
    
    	for ( int j = 0; j<fileSize / sizeof(TCHAR); j++ )
    	{
    		if ( inputBuffer[j] == 0x0A )
    		{
    			newlineIndicies.push_back( j );
    		}		
    	}
    
    	_tprintf(TEXT("Outputting currentLine before appending to vector:"));
    	_tprintf(newline);
    
    	for ( int k = 0; k<newlineIndicies.size(); k++ )
    	{
    		if ( k == 0 )
    		{
    			LPTSTR currentLine;
    			DWORD currentLineSize = newlineIndicies[k];
    
    			currentLine = new TCHAR[ currentLineSize + 1 ];
    			memset( currentLine, '\0', (newlineIndicies[k] + 1) * sizeof(TCHAR) );
    
    			for ( int m = 0; m<newlineIndicies[k]; m++ )
    			{
    				currentLine[m] = inputBuffer[m];
    			}
    
    			_tprintf(TEXT("currentLine: "));
    			_tprintf(currentLine);
    			_tprintf(newline);
    
    			outputVector.push_back( currentLine );
    		}
    
    		else
    		{
    			LPTSTR currentLine;
    			DWORD currentLineSize = newlineIndicies[k] - newlineIndicies[k-1] - 1;
    
    			currentLine = new TCHAR[ currentLineSize + 1 ];
    			memset( currentLine, '\0', ( currentLineSize + 1) * sizeof(TCHAR) );
    
    			for ( int m = 0; m< ( currentLineSize ); m++ ) //&& inputBuffer[ newlineIndicies[k] + m + 1 ] != 0x0A; m++ )
    			{
    				currentLine[m] = inputBuffer[ newlineIndicies[k - 1] + m + 1 ];
    			}
    
    			_tprintf(TEXT("currentLine: "));
    			_tprintf(currentLine);
    			_tprintf(newline);
    
    			outputVector.push_back( currentLine );
    		}
    	}
    
    	CloseHandle(hFile2);
    
    	delete inputBuffer;
    
    //==========================================================================================
    //	sorting vector:
    	_tprintf(TEXT("Pointer addresses before sorting:\n"));
    	for ( int a = 0; a<outputVector.size(); a++ )
    	{
    		std::cout<<outputVector[a] <<std::endl;
    	}
    	_tprintf(TEXT("Sorting vector:\n"));
    	sort_ascending( outputVector );
    	for ( int z = 0; z<outputVector.size(); z++ )
    	{
    		std::cout<<outputVector[z] <<std::endl;
    		//_tprintf(outputVector[z]);
    		//_tprintf(newline);
    	}
    
    //==========================================================================================
    
    
    	for ( int x = 0; x<outputVector.size(); x++ )
    	{
    		delete outputVector[x];
    	}
    
    	system("PAUSE");
    
    
    
    	return 0;
    }
    
    
    void sort_ascending( std::vector<LPTSTR>& inputVector )
    {
    	std::sort( inputVector.begin(), inputVector.end() );
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    std::sort has an overloaded version that takes a comparison function object. The comparison function object must conform to the model of StrictWeakOrdering. StrictWeakOrdering is a Binary Predicate. A Binary Predicate is a function object that takes two arguments and returns true or false depending on whether a condition is satisfied. For StrictWeakOrdering, the condition that must be satisfied is that the first argument is less than the second argument.

    If you've followed all that, you should get something like this:
    Code:
    /* A Binary Predicate function that satisfies the requirements of StrictWeakOrdering. */
    bool is_less_than(LPTSTR s1, LPTSTR s2)
    {
        return (lstrcmp(s1, s2) < 0);
    }
    
    void sort_ascending( std::vector<LPTSTR>& inputVector )
    {
        std::sort( inputVector.begin(), inputVector.end(), is_less_than );
    }

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    Hey, thanks for the great reply! The little function you gave me works perfectly!

    And that SGI site looks like some good reference material

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 05:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21