-2

So I have a list of char * that is storing words from a text file.

char* tempWord = new char[20];
list<DT> list1= new <DT>list();
while (!cin.eof()) {
     cin >> tempWord;
     list1.push_back(tempWord);
}
list1.sort();

But the sort is not working. It is not sorting. If I add APPLE APPIE APPLS in the txt, I get the same after sort, I should be getting APPIE APPLE APPLS. Any ideas why or how do I sort it?

  • `list
    list1= new
    list();` this hardly looks like C++.
    – paolo Jul 11 '22 at 08:18
  • Odd syntax notwithstanding, try a custom comparator (https://en.cppreference.com/w/cpp/container/list/sort), e.g. `list.sort([](const char *fst, const char *snd) { return strcmp(fst, snd) < 0; });` – mitch_ Jul 11 '22 at 08:19
  • 1
    Why not use `std::string` for the words ? – wohlstad Jul 11 '22 at 08:19
  • 1
    May be API restrictions, making a `std::string` out of a provided `const char *` is an unnecessary allocation. If possible `std::string` would be preferable, but bear in mind this may not be possible for all cases. – mitch_ Jul 11 '22 at 08:21
  • What do you want to sort by? Length or something else? – Worry Nice Jul 11 '22 at 08:22
  • You didn't give the context, but for most use-case `std::vector` is preferable over `std::list`. Some more info here: https://isocpp.org/blog/2014/06/stroustrup-lists. – wohlstad Jul 11 '22 at 08:23
  • For words use std::string, not new char[20]. Put the words in a std::vector and use std::sort from to do your sorting. Oh and learn to type std:: instead of using "using namespace std" – Pepijn Kramer Jul 11 '22 at 08:27
  • `list
    list1 = new
    list();` ... this looks like something I'd expect coming from a java programmer trying out C++. Just do `list
    list1;` - you nearly never need to use `new` / `delete` in modern C++.
    – Ted Lyngmo Jul 11 '22 at 08:28
  • In general if you have to type new/delete in C++ you might need to think again, Either use an STL datatype directly or if you have to allocate memory use std::make_unique. I know most people start learning datastructures using C++ (with a lot of new/delete and pointers), but that's not the same as learning C++ as it should be used. – Pepijn Kramer Jul 11 '22 at 08:29
  • 3
    Also read: [Why is `iostream::eof()` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo Jul 11 '22 at 08:30
  • **Never** do this: `char* tempWord = new char[20]; cin >> tempWord;` since it will write out of bounds if anyone enters a word longer than 19 `char`s. The overload that takes a `char*` was removed in C++20 for this reason and replaced by an overload that takes a reference to a `char[N]` instead. – Ted Lyngmo Jul 11 '22 at 08:40

1 Answers1

2

Closer to what you want.

std::string s;
vector<string> vec;

while (cin >> s) {
     vec.push_back(s);
}

std::sort(vec.begin(), vec.end());
selbie
  • 100,020
  • 15
  • 103
  • 173