Stuck by another problem about the vector, map functions
the topnwords function is to find the top n times words occurrence
Here is my code:

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

StringIntVector TextUtil::topNWords(size_t n) const
{
    map<string, int> freq;
    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++)
    {
        return(iter->first,iter->second);
    }
 
}

So i got this error:

Error	1	error C2664: 'std::vector<_Ty>::vector(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'const int' to 'const std::vector<_Ty> &'

Many thanks.

Recommended Answers

All 3 Replies

What is _word_list ?
What is isnotpuct ?
What is wordCount() ?

Oh, Ok
i am sorry to make the confuse:
here let me post it again and make it more clear:
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 question part:

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

StringIntVector TextUtil::topNWords(size_t n) const
{
    map<string, int> freq;
    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++)
    {
        return(iter->first,iter->second);
    }
 
}

Error part:

Error	1	error C2664: 'std::vector<_Ty>::vector(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'const int' to 'const std::vector<_Ty> &'

Many thanks.

In line 20 of your code you are returning an integer (the second operand of the comma operator), so the compiler complains that it cannot turn an int into your actual return type which is a vector.

Replace lines 18-21 with this:

StringIntVector vRet;
    for (iter=freq.begin(); iter != freq.end(); iter++)
        vRet.push_back(*iter);
    return vRet;

And isnotpuct should probably be called isnotpunct (I didn't know what puct was, but I know what punct is).

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.