声明一个指向字符串向量的指针向量
Declaring a vector of pointers to vector of strings
我有一个2D的字符串表(使用STL向量),我试图修改,使表是指向字符串向量的指针向量。我知道这将需要更改构造函数,以便动态创建行,并将指向行的指针插入表中,但我不确定如何首先创建这个表。
在我的。h文件中:class StringTable
{
public:
StringTable(ifstream & infile);
// 'rows' returns the number of rows
int rows() const;
// operator [] returns row at index 'i';
const vector<string> & operator[](int i) const;
private:
vector<vector<string> > table;
};
在我的。cpp文件中:
StringTable::StringTable(ifstream & infile)
{
string s;
vector<string> row;
while (readMultiWord(s, infile)) // not end of file
{
row.clear();
do
{
row.push_back(s);
}
while (readMultiWord(s, infile));
table.push_back(row);
}
}
int StringTable::rows() const
{
return table.size();
}
const vector<string> & StringTable::operator[](int i) const
{
return table[i];
}
我觉得这可能是一个相当容易的转换,但我没有很多使用向量的经验,我不知道从哪里开始。任何指导是非常感激的!
看起来您正在尝试创建某种形式的多维向量。你考虑过使用boost吗?http://www.boost.org/doc/libs/1_47_0/libs/multi_array/doc/user.html
最简单的方法是使用typedef。此外,你似乎在你的头文件中使用'using'子句-你永远不应该这样做。
class StringTable
{
public:
typedef std::vector<std::string> Strings_t;
std::vector<Strings_t *> table;
};
不要忘记,当添加一个现在,你将需要分配内存:
StringTable tbl;
StringTable::Strings_t *data_ptr=new StringTable::Strings_t;
data_ptr->push_back("foo");
data_ptr->push_back("bar");
tbl.table.push_back(data_ptr);
(纠正)相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>