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() );
}