作业:计算某项在链表中出现的次数
这是任务: 实现一个方法 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 in Code 上查找了教程,但我仍然无法弄清楚这一点。感谢所有有用的信息!
Here's the assignment:
Implement a method countValue()
that counts the number of times an item occurs in a linked list. Remember to use the STL list.
int countValue(list<int> front, const int item);
Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList
which you would add to the ListP.cpp.
In a loop, call the method countValue()
, and display the number of occurrences of each value from 0 to 4 in the list.
Remember that all the above is to be included in the file ListP.ccp
Run: 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
and here is what I have so far:
#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++;
}
}
}
And here are the errors:
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
I just want to know what I've done wrong and how to fix it and also if someone could help me figure out if I'm doing the countValue function wrong or not based on the instructions I would really appreciate it. I've read the chapter in our textbook several times, looked up tutorials on youtube and on Dream in Code, and still I can not figure this out. All helpful information is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使用“namespace std;”放置该行在导入之后、列表声明之前,这将清除一些错误。
然后想想你在这里看到的是什么:
错误 C2039:“item”:不是“std::list<_Ty>”的成员第 34、38、42、46、49 行
您可能犯了一个小错字,我会让您自己发现它;)
这对您来说是一个开始
First put the line using 'namespace std;' after your imports, before your list declaration, that will clean up some errors.
Then think about what you're looking at on the lines in here:
error C2039: 'item': is not a member of 'std::list<_Ty>' lines 34, 38, 42, 46, 49
It's likely you made a small typo, I'll let you catch it yourself ;)
That's a start for you