Hello everyone,

I'm writing some code for an assignment that reads in a line of characters into a STL list, sorts them alphabetically and then has to reorganize them vowels to the front.

Right now I'm stuck on the step of writing a function that accepts the list as a parameter. Without the function, I can just write the line

charList.sort();

and it works great. However, one of the requirements is that I have a sort function and I'm confused on how to pass the charList into the sort function. Here's what I have so far:

//function prototypes
void sort (list);

int main()
{
    
    char ch;
    list <char> charList;

  //declare file pointer for input
  fstream infile;

  //open file for input
  infile.open("charFile.txt", ios::in);
 
  //if statement to check if file exists
  if (!infile)
    cout<<"File not found."<<endl;
    
    infile.get(ch);
    charList.push_back(ch);
    while (!infile.eof())
    {
      cout<<ch;
      infile.get(ch);
      charList.push_back(ch);
      }
      
      infile.close();
      
      //charList.sort(); 
      sort(charList);
      list<char>::iterator listIt; 
      listIt = charList.begin();    
  
      while(listIt!= charList.end()){
      cout<<*listIt;
      cout<<" ";
      listIt++;} 
      
      return 0;
    
}


//functions
void sort(list ch)
{
     ch.sort();  
     
}

Among the compiler's many errors are "missing template arguments before "ch" " so I'm assuming I need to put more into the passing parameters then just list ch. I'm also thinking its a void function because I just want to sort the list and then print it out in main.

I've already tested it and it reads in the list normally from the file and sorts it without the function.

Thanks.

Recommended Answers

All 8 Replies

You create your list as list <char> but your function prototype never defines a template type, you just have list . So you could either make the function templated or you can always assume <char>

template <typename T>
void sort(list<T> ch)
{
  ch.sort();
}

sort<char>(ch);

// or

void sort (list <char> ch)
{
  ch.sort();
}

Thanks, I assumed <char> but now its outputting the list unsorted.

Is there something that needs to be put it front of ch.sort(); because its inside of a function?

Thanks.

Thanks, I assumed <char> but now its outputting the list unsorted.

Is there something that needs to be put it front of ch.sort(); because its inside of a function?

Thanks.

My guess is that it's not being passed by reference :)

Oh sorry, I assumed that STL lists were passed by reference automatically.

Amended code....

//function prototypes
void sort (list <char> &);

....

void sort(list <char> &ch)
{
     ch.sort();  
     
}

Thanks a bunch! I might be back later if I am having problems with the placing vowels ahead of consonants part so I'm going to leave this open if its okey.

I've looked through the STL List functions and am having difficulty finding a function that would accomplish my goal of moving just the vowels to the beginning of the list and the consonants further down like :

a b e g m n o t v x z

a e o b g m n t v x z

The closest thing I've been able to find is swap, but that works with two lists if I'm not mistaken and I just want to do a swap with one list.

I also looked at insert & push, but those work with adding new elements.

This is the list that I've been looking at.

http://www.cppreference.com/wiki/stl/list/start

Thanks.

I think I just need to tell the sort function sort by ascending order except when you encounter "a" "e" "i" "o" or "u" but am not sure how. Would it have something to do with the p parameter?

I think I just need to tell the sort function sort by ascending order except when you encounter "a" "e" "i" "o" or "u" but am not sure how. Would it have something to do with the p parameter?

Well if you want to do that, then use a compare function and
pass it to std::sort;

It might look something like this :

bool myComp(char a, char b)
{
  string str = "aeiou";
 if(  str.find(a) != string::npos)
     return true;
 if( str.find(b) != string::npos) 
     return true;
else return a < b;
}
std::sort(something.begin(),something.end(),myComp);

Thanks for that code example, I've adapted it to what I'm trying to do and I'm almost there. However, when I do a sort now it works when it encounters a & e, but skips o for some reason.

This is the expected output...

a e o b g m n t v x z

and this is what I'm getting.

a e b g m n o t v x z

First off, just to make sure I am understanding this correctly, I'm passing the compare function into the sort method for my charList with

charList.sort(compare);

and the sort method is what is iterating my function without a specific loop, right?

Second, I'm a little confused as to why there are two char parameters in the compare function when there is only one list of chars. Are we doing something similar to parallel arrays but with chars? Does the sort method automatically know to make copies of the chars from my file stream into both a & b? So would they be both the same like if it was reading a char a & b would = a in one iteration?

From the code, if none of the vowels are found in either char, return true...but what does true do exactly? Tell the sort method to output them first and then with the else return a < b order everything else alphabetically?

Thanks.

BTW, here's the modified function that I'm using that is skipping just the o and leaving it in its origional position.

bool compare(char a, char b)

{
string str = "aeiou";

if( (str.find(a) != string::npos) && (str.find(b) == string::npos) )

return true;

else return a < b;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.