简体   繁体   English

多个向量的数据表示

[英]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). 我需要用一组特定的电影来代表一个目录(每个目录仅4个)。 Lets say we have 2 directories and 8 movies. 可以说我们有2个目录和8个电影。 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. 1个目录将包含4个电影,另一个目录将包含4个。从编程上来说,它的第0个目录包含:0到3个电影ID,第1个目录包含:4到7个电影ID。 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. 问题出在Myclass::display() const成员函数中。 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. 唯一的问题是嵌套的for循环的循环边界。

Currently you loop all movies , ie 当前,您循环播放所有movies ,即

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. 但是,目录i拥有从i*noofmovies (含)到(i+1)*noofmovies (不包含(i+1)*noofmovies 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 . 在现实世界中,我认为Directory是具有属性vector<int> moviesclass会更好。 And then you can iterate the vector<Directory> directories and for each directory to iterate its movies . 然后,您可以迭代vector<Directory> directories并为每个directory迭代其movies

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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