0

I'm having trouble outputting my vector of lists:

class index_table {

public:
    index_table() { table.resize(128);}
    void insert(string &, int );

private:
    class entry
    {
        public:
        string word;
        vector <int> line;
    };

    vector< list <entry> > table;
};

I've got it so that it will fill up:

int main ()
{
index_table table;
string word,
int num = 5; //this is going to a random number. 5 is a temp. place holder.

while (cin >> word)
    table.insert(word, num);

}

but how to output it? I've tried many different approaches, but a lot of them are giving me errors.
Do I have to overload the operator? I'm not entirely sure how I will be able to do it.

4

3 回答 3

3

假设您确实有充分的理由使用std::vector< std::list<entry> >,那么根据给定的结构,单词的打印可能如下所示:

class index_table {
public:
    void print() {
        for (size_t i = 0; i < table.size(); ++i) {
            std::list<entry>::iterator li;
            for (li = table[i].begin(); li != table[i].end(); ++li)
                std::cout << li->word << " ";
        }
    }
    ...

private:
    std::vector< std::list<entry> > table;
    ...
};
于 2013-10-14T21:16:11.097 回答
1

如果您的编译器支持 C++11,则可以使用两个基于范围的嵌套 for 循环。看功能void index_table::dump()

// Output function
void index_table::dump() {
    for (list<entry> &le : table) {
        for (entry &e : le) {
            e.dump();
        }
    }
}

我还在dump()入口类中创建了一个函数,它输出两个变量的内容,现在已设为私有。

class index_table {
    public:
    index_table() {
        table.resize(128);
    }

    void insert (int,string&,int);
    void dump();

    private:
    class entry {
        private:
        string word;
        int value;

        public:
        entry (string word, int value) {
            this->word = word;
            this->value = value;
        }

        void dump() {
            cout << "Word/value is: " << word << "/" << value << endl;
        }
    };

    vector< list <entry> > table;
};

void index_table::insert(int c, string &key, int value) {
//void index_table::insert(string &key, int value) {
    entry obj(key, value);

    table[c].push_back(obj);
}

// Output function
void index_table::dump() {
    for (list<entry> &le : table) {
        for (entry &e : le) {
            e.dump();
        }
    }
}

int main (int argc, char **argv) {
    index_table mytable;

    string a = "String 0-A";
    string b = "String 0-B";
    string c = "String 1-A";
    string d = "String 1-B";
    string e = "String 6-A";
    string f = "String 6-B";

    mytable.insert(0, a, 1);
    mytable.insert(0, b, 2);
    mytable.insert(1, c, 3);
    mytable.insert(1, d, 4);
    mytable.insert(6, e, 3);
    mytable.insert(6, f, 4);

    mytable.dump();
}

程序输出:

Word/value is: String 0-A/1
Word/value is: String 0-B/2
Word/value is: String 1-A/3
Word/value is: String 1-B/4
Word/value is: String 6-A/3
Word/value is: String 6-B/4

PS:我还稍微更改了您的代码,以使其在我的测试中更容易运行。

于 2013-10-14T21:38:21.653 回答
0
//This is the simple solution for outputting vector of lists.
#include <bits/stdc++.h>
using namespace std;
main()
{
    vector<list<int>> my_vector; //creating vector of lists;
    list<int> my_list;

    for(int i = 0; i < 5; i++)
        my_list.push_back(i);

    my_vector.push_back(my_list); // pushing a list y to a vector

    for (vector<list<int>>::iterator it = my_vector.begin(); it != my_vector.end(); ++it)
        for (list<int>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
            cout << *it2 << ", ";
}
于 2020-05-21T11:30:58.363 回答