0

why won't the sort() function work although I've included algorithm header

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std ; int main(){ vector<string>words ; for(string word;cin>>word;){ words.push_back(word); } for(string x:words) cout<<x<<","; sort(words); for(string x: words) cout<<x<<", "; } hello I'm new to this app and to programming, I'm using cxxdroid to code c++ however the sort() will give an error with the compiler such as <stdin>:13:1: error: no matching function for call to 'sort' sort(words); ^~~~ /data/user/0/ru.iiec.cxxdroid/files/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../include/c++/4.9.x/algorithm:4122:1: note: candidate function template not viable: requires 3 arguments, but 1 was provided

19th Oct 2022, 12:05 AM
Abdullah Othman
Abdullah Othman - avatar
2 Answers
0
sort(words.begin(),words.end());
19th Oct 2022, 1:58 AM
Bob_Li
Bob_Li - avatar
0
/* bonus: sort with lambda expression, reverse. */ #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<string> words{ "park","zoo","arcade"}; cout<<"words:\n"; for(auto i: words) cout<<i<<' '; cout<<'\n'; cout<<"\nsort:\n"; sort(words.begin(),words.end()); for(auto i: words) cout<<i<<' '; cout<<'\n'; cout<<"\nsort descending with lambda expression:\n"; sort(words.begin(),words.end(),[](const string &a, const string &b){return a>b;}); for(auto i: words) cout<<i<<' '; cout<<'\n'; cout<<"\nreverse:\n"; reverse(words.begin(),words.end()); for(auto i: words) cout<<i<<' '; cout<<'\n'; }
19th Oct 2022, 2:13 AM
Bob_Li
Bob_Li - avatar