2011-11-16 23 views
0

以下是作業: 實施方法countValue(),該方法計算項目在鏈接列表中出現的次數。請記住使用STL列表。作業:計算項目在鏈接列表中發生的次數

int countValue(list<int> front, const int item); 

生成0到4範圍內的20個隨機數,並在鏈表中插入每個數字。通過使用您將稱爲writeLinkedList的方法輸出列表,您將其添加到ListP.cpp。

在循環中,調用方法countValue(),並在列表中顯示每個值從0到4的出現次數。

記住,所有上述要被包括在所述文件ListP.ccp 執行命令2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2 0 :5,1:1 ,2:5,3:6,4:3

這裏是我到目前爲止有:

#include<iostream> 
#include<list> 
#include<tchar.h> 

int countValue(list<int> front, const int item); 

using namespace std; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    list<int> front; 
    int listCount; 
    cout << "Enter the size of the list: "; 
    cin >> listCount; 
    for (int i = 1; i <= listCount; i++) 
     front.insert(rand()%5); 
    cout << "Original List of Values: " << endl; 
    //writeLinkedList(front, " "); 
    cout << endl; 
    for(int j=0;j<5;++j) 
     cout << countValue (front,j) << endl; 
    cout << endl; 
    return 0; 
} 

int countValue(list<int> front, const int item) 
{ 
    int count0; 
    int count1; 
    int count2; 
    int count3; 
    int count4; 
    list<int> *List; 

    for(list<int>::iterator i = front.begin(); i != front.end(); i++) 
    { 
     if(List->item == 0) 
     { 
      count0++; 
     } 
     if(List->item == 1) 
     { 
      count1++; 
     } 
     if(List->item == 2) 
     { 
      count2++; 
     } 
     if(List->item == 3) 
     { 
      count2++; 
     }if(List->item == 4) 
     { 
      count4++; 
     } 
    } 
} 

這裏是錯誤:

error C2065: 'list' : undeclared identifier line 5 
error C2062: type 'int' unexpected line 5 
error C2661: 'std::list<_Ty>::insert' : no overloaded function takes 1 arguments line 16 
error C3861: 'countValue': identifier not found line 21 
IntelliSense: no instance of overloaded function "std::list<_Ty, _Ax>::insert [with _Ty=int, _Ax=std::allocator<int>]" matches the argument list line 16 
IntelliSense: too few arguments in function call line 16 
error C2039: 'item': is not a member of 'std::list<_Ty>' lines 34, 38, 42, 46, 49 
IntelliSense: declaration is incompatible with "int countValue" (declared at line 5) line 25 
IntelliSense: class "std::list<int, std:: allocator<int>>" has no member "item" lines 34, 38, 42, 46, 49 

我只是想知道我做錯了什麼以及如何解決它也有人可以幫助我弄清楚,如果我正在做countValue函數錯誤或不基於指令,我真的很感激它。我多次閱讀教科書中的章節,在YouTube上查找教程,在代碼中查找Dream,但我仍然無法弄清楚。所有有用的信息表示讚賞!

+0

countValue需要大量的工作。你聲明List,但不要初始化它。你不需要5次,只有一次。它需要返回一些東西。它需要對'item'參數做些什麼。僅舉幾例。 –

回答

3

首先使用'namespace std;'命令行在導入之後,在列表聲明之前,這將清除一些錯誤。

後來想想,你看就在這裏行的內容:
錯誤C2039:「項」:不是的成員「的std ::名單< _Ty>」線34,38,42,46 ,49
很可能你做了一個小錯字,我讓你自己抓住它;)

這是一個開始你