用C ++输出列表向量

时间:2013-10-14 21:10:58

标签: c++ vector io stdvector stdlist

我无法输出列表矢量:

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

我已经知道它会填满:

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

}

但如何输出呢?我尝试了很多不同的方法,但很多方法都给了我错误 我是否必须超载操作员?我不完全确定我将如何做到这一点。

3 个答案:

答案 0 :(得分: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;
    ...
};

答案 1 :(得分: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:我也改变了你的代码,使其更容易运行我的测试。

答案 2 :(得分: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 << ", ";
}