1

I have also copied the exact code from Programming Principles & Practice but to no avail. I get an error message when I try to use std::sort(word) or sort(word):

<source>: In function 'int main()':
<source>:13:14: error: no matching function for call to 'sort(std::vector<std::__cxx11::basic_string<char> >&)'
   13 |     std::sort(words);
      |     ~~~~~~~~~^~~~~~~
[...]

The code:

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::string> words;
    for(std::string temp; std::cin >> temp;){
        words.push_back(temp);
    }
    std::cout << "Number of words: " << words.size() << "\n";

    std::sort(words);

     for(int i=0; i < words.size(); i++){
        if(i == 0; words[i-1]!=words[i]){
            std::cout << words[i] << "\n";
        }
    }
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
SILO
  • 73
  • 3
  • 10
  • 3
    Why do you think that error `no match for ‘operator<<’` has anything to do with sort? – eerorika Jul 07 '21 at 09:33
  • 7
    `words` vs `words.size()`; error message doesn't match the code. – Olaf Dietsche Jul 07 '21 at 09:34
  • sorry, this is my second day learning C++ but i thought it had something to do with calling sort() because using `std::` with `cout` works but not with sort – SILO Jul 07 '21 at 09:35
  • 6
    std::sort(words.begin(), words.end()) – Meowster Jul 07 '21 at 09:35
  • `std::sort(words.begin(), words.end());` got rid of the error message, thanks! I wish it was included in the book though – SILO Jul 07 '21 at 09:37
  • 3
    @SILO If your book actually instructed you to call `std::sort(words)`, get a better book. ;-) – DevSolar Jul 07 '21 at 09:44
  • @SILO please next time use [godbolt link](https://godbolt.org/z/fjPfTq8M8) which will reproduce your issue. Note that you made other errors when posting a question (see comments above). – Marek R Jul 07 '21 at 09:45
  • 1
    "std::sort(words.begin(), words.end()); got rid of the error message," ? do you mean the other error, the one you didnt include in the question? The one you included is not from the code you posted – 463035818_is_not_an_ai Jul 07 '21 at 09:46
  • i replaced the error message with the one I got here: https://godbolt.org/z/fsq1oM5dd. – 463035818_is_not_an_ai Jul 07 '21 at 09:48
  • @SILO you need to read [`std::sort` documentation](https://en.cppreference.com/w/cpp/algorithm/sort). Which book are you using? Try the good ones in [The Definitive C++ Book Guide and List](https://stackoverflow.com/q/388242/995714) – phuclv Jul 07 '21 at 09:49
  • You claim you copied that code from _Programming Principles and Practice using C++_ by Stroustrup? I don't believe you, but maybe you misunderstood something. – Jabberwocky Jul 07 '21 at 09:52
  • @DevSolar The book OP appears to be using is written by Bjarne Stroustrup. OP’s error is not mentioned [in the errata](https://www.stroustrup.com/PPPslides/PPP2errata.html) so I think it’s more likely that OP is mistaken. – Konrad Rudolph Jul 07 '21 at 09:53

2 Answers2

3

There are two problems in your code:

  1. your usage of sort is wrong, it's std::sort(words.begin(), words.end()). This should be in your book or in your learning material.

  2. in if (i == 0; words[i-1]!=words[i]) during the first iteration i is 0 and therefore you are accessing words[-1] which is out of bounds and which will at best trigger some error message. Also the ; makes no sense here.

You probably want this:

  for (size_t i = 0; i < words.size() - 1; i++) {
    if (words[i] != words[i + 1]) {
      std::cout << words[i] << "\n";
    }
  }
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2

sort function accepts either 2 or 3 variables. first 2 are for starting and ending indexes. In this case, they are words.begin() and words.end()

So, your 13th line (which has the error) should be:

std::sort(words.begin(),words.end());

The 3rd argument can be given to specify type of sort. For example,

std::sort(words.begin(),words.end(),greater<int>());

will sort the vector in reverse order.

Shubham Garg
  • 459
  • 3
  • 10