Sorting strings using pointer array

I am working on sorting an array of strings using pointers,( I want to keep the original order but have the pointers sorted so that I can print out a sorted order).

This is just a MINI program so that I understand the HOW. I know the logic isn't 100% but I am more concerned with working thru the syntax and then fixing it later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

void swap(string * , string * );



int main()
{
 string names[5]={"Marsh, alan", "turtle, tami", "booby, google",
                   "turner, ted", "rodgers, mimi"};
 int ages [5]={10,20,30,40,50};
 string * strPtr[5];

 // load up pointers addresses
  for (int i = 0; i <5; i++)
  {
    strPtr[i] = &names[i];    
  }

 int smallest =1; 
  if(names[1] > names[2])
 {
   cout << "swap pointers " << endl;
   swap( &strPtr[ 1 ], &strPtr[ smallest ] );
}  
 return 0;
  
}

void swap( string * element1Ptr, string *  element2Ptr )
  {                                                            
     string * hold = *element1Ptr;                                  
     *element1Ptr = *element2Ptr;                              
     *element2Ptr = hold;                                      
  } 



Syntax Error Messages

1
2
3
4
5
6
7
8
28 C:\Dev-Cpp\ifTest.cpp no matching function for call to `swap(std::string**, std::string**)' 
 note C:\Dev-Cpp\ifTest.cpp:7 candidates are: void swap(std::string*, std::string*) 
 note C:\Dev-Cpp\ifTest.cpp:7                 void std::swap(_Tp&, _Tp&) [with _Tp = std::string**] 

36 C:\Dev-Cpp\ifTest.cpp cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `std::string*' in initialization 
38 C:\Dev-Cpp\ifTest.cpp invalid conversion from `std::string*' to `char' 
38 C:\Dev-Cpp\ifTest.cpp   initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' 


Your prototype on line 7 should read

7 void swap( string **, string ** );

And line 34 should match

34 void swap( string ** element1Ptr, string ** element2Ptr )


I always recommend a little display function to see what is happening at each step:
1
2
3
4
5
6
void display( string * namePtrs[], unsigned count )
  {
    for (unsigned n = 0; n < count; n++)
      cout << n << " " << *namePtrs[n] << endl;
    cout << endl;
  }
And you can use it thus:
display( strPtr, 5 );


Your sort procedure still needs a lot of help... but I presume you know that. Oh, yeah, before I forget: you shouldn't need to refer to names[] when sorting -- just strPtr[].

Hope this helps.
Last edited on
What does this mean in the function prototype and header? I have not seen the double asterisks yet (**)

string **
Pointer to a pointer to a string.
Topic archived. No new replies allowed.