作业:计算一个项目在链表中出现的次数
Homework: Counting number of times an item occurs in a linked list
赋值如下:实现一个方法countValue()
,计算一个项目在链表中出现的次数。请记住使用STL列表。
int countValue(list<int> front, const int item);
生成20个0 ~ 4范围内的随机数,将每个数字插入链表。使用一个称为writeLinkedList
的方法输出列表,并将其添加到ListP.cpp。
在循环中,调用countValue()
方法,并显示列表中从0到4的每个值出现的次数。
请记住,上述所有内容都将包含在文件ListP.ccp中输入:2 3 4 0 1 0 2 4 2 3 3 4 3 3 0 0 2 0 2 20: 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上查看了教程,但我仍然不能弄清楚这一点。感谢所有有用的信息!
首先将使用'namespace std;'的行放在导入之后,列表声明之前,这将清除一些错误。
然后想想你在这里看什么:
错误C2039: 'item':不是'std::list<_Ty>'的成员,第34、38、42、46、49行
很可能是你打了个小错别字,我让你自己纠正;)
对你来说是个开始
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- C++ 从链表中删除项目时出现问题
- 双向链表问题 - 包含项目但不显示
- C++ 链表删除了错误的项目
- 将项目添加到双向链表的背面时遇到问题
- 无法在 C++ 中的循环中将项目添加到链表
- 当SLIST_ENTRY不是项目列表的第一个成员时,使用单向链表
- 正在删除单向链表中的项目
- 分段 删除链表的项目时出错
- 如何删除双向链表中与特定值匹配的所有项目
- 将项目添加到链表中,按引用或值传递
- 当列表中有两个以上的项目时,链表的函数 add 会崩溃
- c++如何从链表中随机挑选项目
- 作业:计算一个项目在链表中出现的次数