문제

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.

도움이 되었습니까?

해결책

Assuming you really have a good reason to use std::vector< std::list<entry> >, then based on the given structure, printing of words might look like this:

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

다른 팁

If your compiler supports C++11, you can use two range based nested for loops. Look in the function void index_table::dump().

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

I also created a function dump() in the entry class, which outputs the contents of two variables, which is now made private.

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

Program outputs:

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: I also changed your code a bit to make it run more easily for my test.

//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 << ", ";
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top