Bjarne Stroustrup Eg 4.6.4 A text example

Hello there,
I am a newbie to c++ programming. Lately I am facing an error while doing examples in Bjarne Stroustrup "Programming Principles and Practice Second edition". Now I am sticking here in Vector Examples. Please help me with me . I respect you seniors!

Here my code is


#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;

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

sort(words);

for(int i=0; i<words.size(); i++)
if(i==0||words[i-1]!=words[i])
cout<<words[i]<<'\n';
return 0;

}




Unfortunately, when I try to run it, it did notsucceed. An error message appears like this

"No matching function call to 'sort(std:vector<<std:basic_string<char>>&)' "

Please help me with it. Thanks in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> // "iostream"
#include <algorithm> // "algorithm"
#include <vector> // "vector"
#include <string> // *** added
using namespace std;

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

    // sort(words);
    sort( words.begin(), words.end() ) ; // ***
    // see: http://en.wikipedia.org/wiki/Sort_(C%2B%2B)

    for( /*int*/ size_t i=0; i<words.size(); i++)
       if( i==0 || ( words[i-1]!=words[i] ) ) cout << words[i] << '\n';

    // return 0; /// *** implicit
}
That shows sorting function is not going well. Oh you have saved me from Hell. Thanks a million to "JLBorges".
Topic archived. No new replies allowed.