English 中文(简体)
1. 宣示歌器病媒
原标题:Declaring a vector of pointers to vector of strings

我有2D表(使用STL矢量),并试图加以修改,使表格成为星体的指向者。 我知道,这将要求改变施工者,以便生动地创造行人,把点人放在桌子上,但我并不肯定,首先如何建立这一桌子。

在我的档案中:

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;

};

在我的档案中:

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

我认为,这或许是一个非常容易的开关,但我没有使用病媒的丰富经验,我不敢肯定会从哪里开始。 任何指导都受到高度赞赏。

最佳回答

它希望你努力创造某种形式的多层面病媒。 您是否考虑利用提升?

问题回答

这样做最容易的方法是类型。 同样,你似乎正在使用你档案中的条款——你永远不应该这样做。

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

[更正]





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签