3D vector

Can I use vector like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
vector< vector< vector<int> > > M;  //declare a vector of vectors of vectors.

if(M.size()<a){                     //resize when it's not large enough
  M.resize(a+1);
  if(M.at(a).size()<b){
    M.at(a).resize(b+1);
    if(M.at(a).at(b).size()<c){
      M.at(a).at(b).size(c+1,0);
    }
  else{
    if(M.at(a).at(b).size()<c){
      M.at(a).at(b).size(c+1,0);
    }
  }
else{
  if(M.at(a).size()<b){
    M.at(a).resize(b+1);
    if(M.at(a).at(b).size()<c){
      M.at(a).at(b).size(c+1,0);
    }
  else{
    if(M.at(a).at(b).size()<c){
      M.at(a).at(b).size(c+1,0);
    }
  }
M.at(a).at(b).at(c)=d;              //input d at M[a][b][c] 


I'm not sure if this is right. I want some more information if I get anything wrong here. Thanks!
Last edited on
Hey man, I think its a lot of work to change size of your vectors like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

template<typename T>                                                      //default argument
std::vector<std::vector<std::vector<T>>> make_3d_vector(int z, int y, int x, T value = T{})
{
    return std::vector<std::vector<std::vector<T>>>(z, std::vector<std::vector<T>>(y, std::vector<T>(x, value)));
}

int main()
{
    //specify dimensions and last arg is value to initialize elements if you don't like default 0
    auto 3d_vec = make_3d_vector<int>(10, 10, 10, 20); 
    std::cout << 3d_vec [5][5][5]; //20
}


This is also more efficient if all you want to do is to create specific size 3d vector
So it would be easier to understand this is how you create 1d and 2d vectors with specific size

1D
std::vector<int> vec(size, value);

2D
std::vector<std::vector<int>> vec(size, std::vector<int>(size2, value));

and so on. If you leave out value part the default value will be used (in case of int its 0)
Last edited on
Well... I have a "very large" file as the data of the input which I don't know the content yet, so I can't really declare it with size, can I?
@tomtran have you thought about using other stl structures? Stacks, Queues, Lists, Maps, Sets, and or combining them with tuples or pairs? I mean you can probably use vectors all the way, but it might be easier to wrap your head around your data struct if you were using different abstractions.
Last edited on
I've learn stacks, queues... Lists, Maps, Sets I've not yet learned, but I've read about Set on the web. I think Vectors suit my prob best.
@TheIdeasMan:
Sorry for my disturbance.
@keskiverto:
1. Each line contains 4 integers, I'm sure, cuz the spec says so.
2. time won't be wrong cuz I've double check it with my friends about the range.

And I see where I have the problem I mention.
1
2
if(M.at(a).at(b).size()<c)
  M.at(a).at(b).resize(c+1);

M.at(a).at(b).at(c)=d;

This hold true until the next line of the integer c is actually c itself. For example: If the size of c is 10 and c in the next line is 10. Then at(10) will be the problem. I get it!!
Hence, instead of M.size()<a. I should implement M.size()<=a
@tomtran3110

No worries, it's not a big drama :+)

Can I ask what is your real life problem you are trying to solve? As mentioned by several of us, a different approach might be better.
My prof. took a task similar to KDDCUP2012 track1 for assignment. I handed it in already. But I kinda feel like to find out any faster way to approach.
Data set format:
(UserId)\t(ItemId)\t(Result)\t(Unix-timestamp)

Result: values are 1 or -1, where 1 represents the user UserId accepts the recommendation of item ItemId, and -1 represents the user rejects the recommended item.

5 actions to perform:
accept(u,i,t): outputs the (Result) when user u is given item i at time t.

items(u1, u2): outputs the sorted (ItemId), line by line, that are recommended to both user u1 and user u2.

users(i1,i2,t1,t2): outputs the sorted (UserId), line by line, which corresponds to users who are given both item i1 and item i2 between the time interval [t1,t2].

ratio(i,threshold): outputs the acceptance rate of item i among the users who have been given items for more than threshold times. The format of your output is (#accept)/(#total) .
For instance, ratio(10, 78) = 14/101 means there are 101 users who are given any items by the system for more than 78 times. Among them, 14 users (remove duplication) have accepted item 10.

findtime item(i, Us): outputs the sorted list of timestamps (remove duplication), line by line, that correspond to when item i is given to any member from a group of users Us. You can choose your implementation of Us.

And by the way, what is tuple? As Bdanielz suggest, I would like to know how to use it? Any link that has asked this question is fine.
Topic archived. No new replies allowed.