如何初始化对象列表的向量.我知道很困惑
How do I Initialize a vector of lists of objects. Confusing I know
我正在创建一种非常粗糙的hashtable
。它将读取一个 ASCII 文本文件,并将所有单词与找到的单词的行号一起存储到链表中。它使用单词第一个字母的数值来查找列表以存储单词,然后在列表中搜索单词的任何条目,如果未找到,则向列表中添加一个新条目,否则它将行号添加到匹配的条目中。
我只是不知道如何初始化vector
.它需要 128 个列表(每个 ASCII 值一个(。
另请注意,我无法使用std::map
我知道使用整数向量,您可以vector<int> vector_name(128,0)
获得 128 个值为 0 的条目。我基本上想这样做,但有 128 个空的entry
列表。
这是我到目前为止的代码
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
class index_table {
public:
void insert(string key, int value);
vector<int>& find(string key);
private:
class entry{
public:
string word;
list<int> line_numbers;
};
vector<list<entry> > table;
};
int main(){
return 0;
}
在你还没有的构造函数中你可以做: table.resize(128);
现在table
是一个由 128 个空std::list
entry
对象的向量。
即
class index_table {
public:
void insert(string key, int value);
vector<int>& find(string key);
//My ctor
index_table(size_t len) : table(len), len(len)
{
//some other stuff someday
}
private:
//not sure why you want to make entry private but ok...
class entry{
public:
string word;
list<int> line_numbers;
};
//some typename convenience for your iterators
typedef entrylist list<entry>;
typedef entrytable vector<entrylist>;
entrytable table;
};
相关文章:
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- 可能我知道为什么这段代码没有给出任何输出吗?
- 为什么我在向量初始化时收到此错误?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 如何知道向量的实际最大大小?(不使用 std::vector::max_size)
- 如果我知道只会使用现有元素,我可以在数组开头之前传递指针吗?
- 防止我的向量在调用它的函数结束时被删除
- 所有shared_ptr都指向我的向量中的同一指针
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- 我需要C++向量的最大大小(我给出的值)
- 在不同条件下无法访问我的向量变量
- 当我使用我的向量名称后跟包含整数变量的括号时,括号是什么意思
- 当我完成向量时,我需要调用 clear() 吗?
- 为什么我的向量函数的返回值总是 1
- 带有钩子的C 二进制树,让我知道何时/何时插入或移动节点
- 如何初始化对象列表的向量.我知道很困惑