A compound vector question

Apr 12, 2013 at 2:00pm
Hi Everyone,

I'm writing a bit of code that uses a multidimensional vector like this:

std::vector< std::vector< std::vector<data> > > data_vector

The reason I'm doing this is that I need to store multiple points of data in an n by n grid.

What I've tried doing is:

x = data_vector[n][n].size();

But doing this returns a segmentation fault.

Is it possible to find the size of a specific element like this? If not, what is the best way of doing it?

Thanks,

Kit



Last edited on Apr 12, 2013 at 2:10pm
Apr 12, 2013 at 2:02pm
The segmantation fault means that you have no element data_vector[n][n].
Apr 12, 2013 at 2:11pm
Thanks for the reply vlad.

I'll have a look at the section of code where I build the vector and see if there's something I'm missing there.

Kit
Apr 12, 2013 at 2:13pm
Is it possible to find the size of a specific element in this way?

Only if your "data" has a public size() member function.

If so, what is the best way of doing it?


First find the size of the outer vector:

size_t out_size = data_vector.size(); // Find the size of the outer vector.

Then if 'n' is within range you can find the size of the inner vector like:

size_t in_size = data_vector[n].size(); // Find the size of the inner vector.

Now if your "data" has a size() member function:

x = data_vector[n][n].size();

Would give you the size reported by your data.size() member function as long as 'n' is within the range of both your vectors.

Apr 12, 2013 at 2:25pm
jlb (410)
Only if your "data" has a public size() member function.



His "data" is std::vector<data> that obviously has public method size because he defined data_vector as

std::vector< std::vector< std::vector<data> > > data_vector
Last edited on Apr 12, 2013 at 2:36pm
Apr 12, 2013 at 2:43pm
Why is it obvious? data is not a vector, it is some kind of user defined class, data_vector is the vector<vector>> instance.

std::vector< std::vector< std::vector<data> > > data_vector

data_vector is the vector. Since this is a vector<vector<data>> using two sets of brackets means that the data type must have a size member function.

data_vector.size(); // The size of the outer vector.
data_vector[0].size(); // The size inner vector of the first outer element.
data_vector[0][0].size(); // The size reported by the data class size() member function.
Apr 12, 2013 at 2:53pm
@jlb
Why is it obvious?


It is obvious who understand the record

std::vector< std::vector< std::vector<data> > > data_vector

So data_vector[n] has type std::vector< std::vector<data>>

data_vector[n][n] has type std::vector<data>

std::vector<data> has public method size().
Apr 12, 2013 at 2:58pm
So then I take it you don't understand the record?

Try changing data to an int.

Apr 12, 2013 at 3:06pm
jlb (413)


I of course understand that such advanced "programmers" as you will not use a compiler to prove own statement. So I did it using MS VC++ instead of you

1
2
3
std::vector<std::vector<std::vector<int>>> v;

std::cout << typeid( v[0][0] ).name() << std::endl;


The output is

class std::vector<int,class std::allocator<int> >
Apr 12, 2013 at 7:49pm
Your absolutely correct, the compiler doesn't lie:

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

using namespace std;


int main()
{
   vector<vector<int>> data;
   vector<int> stuff(3);
   data.push_back(stuff);
   cout << data.size() << endl;
   cout << data[0].size() << endl;
}


No compiler errors and the output is:
1
2
1
3



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

using namespace std;


int main()
{
   vector<vector<int>> data;
   vector<int> stuff(3);
   data.push_back(stuff);
   cout << data[0][0].size();

}


Output: COMPILER ERROR:

1
2
3
main.cpp||In function ‘int main()’:|
main.cpp|14|error: request for member ‘size’ in ‘(& data.std::vector<_Tp, _Alloc>::operator[]<std::vector<int>, std::allocator<std::vector<int> > >(0u))->std::vector<_Tp, _Alloc>::operator[]<int, std::allocator<int> >(0u)’, which is of non-class type ‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’|
||=== Build finished: 1 errors, 0 warnings ===|


As you should be able to see an int doesn't have a size() member function.

Apr 12, 2013 at 8:00pm
@jlb
I think you missed that the OP is using a 3D vector.
Apr 12, 2013 at 11:41pm
@jlb
As you should be able to see an int doesn't have a size() member function.


Yes the int type does not have the size() member but std::vector does have.

The problem that it is you who are unable to see what the declaration

std::vector< std::vector< std::vector<data> > > data_vector


means.

I do not know how many times I should you point out this declaration. It looks like you are unequal.
Last edited on Apr 12, 2013 at 11:41pm
Apr 15, 2013 at 8:32am
Hi Guys,

Thanks for your answers! vlad was right, I needed to build my 3D vector properly before I tried to manipulate it.

The problem was that I had only declared it and hadn't built the thing properly before I started trying to use it in my program.

I got round this by pushing empty elements into a 1D vector, pushing that vector into a 2D vector and then taking that vector and pushing it into my original declaration.

Thanks!
Topic archived. No new replies allowed.