gpt4 book ai didi

c++ - 列表 vector

转载 作者:行者123 更新时间:2023-11-30 01:58:24 26 4
gpt4 key购买 nike

我第一次尝试实现链式哈希表。我想创建一个列表 vector 。所以我将列表的 vector 声明为 private 。

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 ) ;
};

它显示以下错误:

\HashTable.cpp [错误] 'template class std::vector' 的模板参数列表中参数 1 的类型/值不匹配

基本上我的问题是 List 是一种类型,所以如果我们可以声明 int vector ,那么为什么不能声明 list vector ?

最佳答案

您忘记了列表的模板参数。它不能只是 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 行(缺少列表模板参数的那一行)。 This was compiled on 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]

关于c++ - 列表 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17504464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com