Hi, all
its me again!
Still stuck with the STL function usage.
but this time face the sort function problems!
here is my class:

class A
{
public:
    A( const string& filename )
    {
       ifstream file(filename.c_str()) ;
        string word;
       while( file >> word)
       {
	  _word_list.push_back(word);
        }
    }
    StringIntVector topNWords(size_t n) const;
     bool isnotpuct() const;
    // this function is to judge weather the text inside the file
    // is punctuations.
private:
    vector<string> _word_list;

};

My topnwords function implementation-- this function is used for return find the top n times words occurrence

//stringvector is the type--
//typedef vector< pair < string, int > > StringIntVector;

StringIntVector TextUtil::topNWords(size_t n) const
{
    map<string, int> freq;
   StringIntVector vRet;
    for(size_t i=0; i<_word_list.size(); i++)
  {
     if(isnotpuct())
    {
      string words = _word_list[i];
      size_t topn = wordCount(words);
      freq[words] = topn;
    }
   }
    sort(freq.begin(),freq.end());
    map<string, int>::const_iterator iter;
    for (iter=freq.begin(); iter != freq.end(); iter++)
    {
        vRet.push_back(*iter);
    }
      return vRet;
 
}

But this one got bunch of errors on vc++. The error always point me to the algorithm file which i could not understand.

Error	2	error C2784: 'reverse_iterator<_RanIt>::difference_type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_Tree<_Traits>::iterator'

Seems like my sort function usage is wrong. But how i could change my code to use it??
Many thanks.

The translation of your error is that std::map doesn't support random access iterators, and random access iterators are required for std::sort to work. However, std::map is already sorted on the key, so there's no need to explicitly sort anyway.

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.