列表的矢量
A vector of lists
本文关键字:列表 更新时间:2023-10-16
我第一次尝试实现一个链式哈希表。我想创建一个列表向量。所以我声明了一个向量列表为私有。
class HashTable{
public:
HashTable( int ) ;
void add( int k ) ;
int remove( int k ) ;
int find( int k ) ;
private:
vector< list > t ;
int n ;
int hash( int ) ;
};
它显示以下错误:
\"template class std::vector"的模板参数列表中的参数1处的HashTable.cpp[Error]类型/值不匹配
基本上我的问题是列出一个类型,所以如果我们可以声明int的向量,为什么不能声明List的向量?
您忘记了list的模板参数。它不能只是list
,它必须是类似std::list<int>
的列表(其中int可以是任何其他类型,但由于所有其他成员函数都使用int,我在示例中使用了int)
下面的代码说明了这个问题:
#include <vector>
#include <list>
int main(int argc, char* argv[])
{
std::vector< std::list<int> > v; // this compiles
std::vector<std::list> v2; // and this doesn't
return 0;
}
如下所示,所有编译错误都来自第7行(列表中缺少模板参数的行)。这是在ideone 上编译的
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:7:26: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
prog.cpp:7:26: error: expected a type, got ‘list’
prog.cpp:7:26: error: template argument 2 is invalid
prog.cpp:7:30: error: invalid type in declaration before ‘;’ token
prog.cpp:7:28: warning: unused variable ‘v2’ [-Wunused-variable]
您必须指定列表应包含的元素类型。你不能只说"一个列表",你必须说"某种类型的t的列表"。列表模板采用一个参数,就像vector一样,所以您必须说一些类似std::vector<std::list<char*> >
的内容。如果您不使用C++11,请注意在第一个>
之后留一个空格,否则它将无法工作,因为它被解析为>>
运算符。
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有为自己的结构调用列表推回方法
- 使用简单类型列表实现的指数编译时间.为什么
- 一对向量构造函数:初始值设定项列表与显式构造
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过for循环使用用户输入填充列表
- C++:如何使函数只返回作为列表一部分的字符串
- 概念中的cv限定符需要表达式参数列表
- 下面是我为检测链接列表中的循环而制作的代码
- 建议在运行时将带有类实例的列表从c++导入qml
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在卡萨布兰卡形成编码参数的列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题