简体   繁体   中英

Data representation for multiple vectors

I have two items directory and movies. I need to represent a single directory with particular set of movies (only 4 per dir). Lets say we have 2 directories and 8 movies. 1 directory will contain 4 movies and another will contain 4. Programatically its like 0th directory contains: 0 to 3 movie ID's and 1st directory contains: 4 to 7 movie ID's. I've written the following code, but could not able to achieve what I need as shown in the expected output vs. original output with the help of vectors.

#include <iostream>
#include <vector>

using namespace std;

const int noofmovies = 4;

class Myclass{
private:
    int directory;
    vector<int> directories;
    vector<int> movies;
public:
    void read();
    void display() const;
};
void Myclass::read(){
    cout << "Enter number of directories: " << endl;
    cin >> directory;
    for(int i=0;i<directory;i++){
        directories.push_back(i);
    }
    for(int j=0;j<(directory*noofmovies);j++){
        movies.push_back(j);
    }
}
void Myclass::display() const{
    for(int i=0;i<directories.size();i++){
        cout << "directory[" << i << "]" << " contains " << endl;
    }
    for(int j=0;j<movies.size();j++){
        cout << "movies[" << j <<"]" <<endl;
    }
}
int main(){
    Myclass my;
    my.read();
    my.display();
}

original output:

Enter number of directories:
2
directory[0] contains
directory[1] contains
movies[0]
movies[1]
movies[2]
movies[3]
movies[4]
movies[5]
movies[6]
movies[7]

expected output:

Enter number of directories:
2
directory[0] contains
movies[0]
movies[1]
movies[2]
movies[3]
directory[1] contains
movies[4]
movies[5]
movies[6]
movies[7]

Well I tried modifying the loops as below but the result is not what I wanted:

    void Myclass::read(){
    cout << "Enter number of directories: " << endl;
    cin >> directory;

    for(int i=0;i<directory;i++){
        directories.push_back(i);
        for(int j=0;j<noofmovies;j++){
            movies.push_back(j);
        }
    }
}
void Myclass::display() const{

    for(int i=0;i<directories.size();i++){
        cout << "directory[" << i << "]" << " contains " << endl;
        for(int j=0;j<movies.size();j++){
            cout << "movies[" << j <<"]" <<endl;
        }
    }

}

The problem is in Myclass::display() const member function. Both implementations of your other function are fine.

Your last attempt is the closest. Your only problem is the loop bounds of the nested for loop.

Currently you loop all movies , ie

for (int j=0 ; j < movies.size() ; j++)

This is incorrect, because it makes it look as if each directory owns all movies. However, directory i owns movies from i*noofmovies , inclusive, to (i+1)*noofmovies , exclusive. In other words, changing the inner loop to

for (int j=i*noofmovies ; j < (i+1)*noofmovies ; j++)

will fix your program.

You can achieve this by the following code

void Myclass::display() const{
    int j = 0;    

    for(int i=0;i<movies.size();i++){
        if(i % noofmovies == 0){
            cout << "directory[" << j++ << "]" << " contains " << endl;
        }

        cout << "movies[" << i <<"]" <<endl;
    }
}

But you are not holding an actual link between directories and movies. In a real world scenario I think it would be better if Directory was a class that has a property vector<int> movies . And then you can iterate the vector<Directory> directories and for each directory to iterate its movies .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM