# array of vectors

Page 1 of 1

## 3 Replies - 42738 Views - Last Post: 07 July 2009 - 08:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=113360&amp;s=63665933481ef54e17928a03ad7697f8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 machoolah

Reputation: -6
• Posts: 87
• Joined: 17-May 09

# array of vectors

Posted 06 July 2009 - 09:09 AM

Hello everyone,
So I need a container which is similar to an array of arrays. I don't know beforehand what the size of each element would be so I was thinking of an array of vectors. But I couldn't implement it. I would really appreciate if somebody tells me how to do that or if anyone has an alternative idea. Considering each of the arrays a level, I may need to remove some elements at each level at a later time.

Some thing that I ran up to somewhere else is:
```vector<string> container[5];

```

however, there are two problems with that which are first I don't know how to use container(i.e. add, remove items, etc) and second, I want a dynamic size.

This post has been edited by machoolah: 07 July 2009 - 07:26 AM

Is This A Good Question/Topic? 0

## Replies To: array of vectors

### #2 GWatt

Reputation: 292
• Posts: 3,092
• Joined: 01-December 05

## Re: array of vectors

Posted 06 July 2009 - 09:33 AM

To create a vector of vectors goes something like this
vector <vector <string> > vec;
You can use the resize function to allocate space to the vector(s)
```vec.resize(10);
for (int i = 0; i < vec.size(); i++)
vec[i].resize(10);

```

and now the vector of vectors can hold 10 vectors of strings and each vector of strings can hold 10 strings.

### #3 machoolah

Reputation: -6
• Posts: 87
• Joined: 17-May 09

## Re: array of vectors

Posted 07 July 2009 - 07:20 AM

GWatt, on 6 Jul, 2009 - 08:33 AM, said:

To create a vector of vectors goes something like this
vector <vector <string> > vec;
You can use the resize function to allocate space to the vector(s)
```vec.resize(10);
for (int i = 0; i < vec.size(); i++)
vec[i].resize(10);

```

and now the vector of vectors can hold 10 vectors of strings and each vector of strings can hold 10 strings.

How do you remove a single item from a multi-dimensional vector esp. a 3-D one?
what is wrong with the following code? I get an error when I want to remove an item.
```#include <iostream>
#include <vector>

using namespace std;

int main()
{
int size = 5;
vector < vector < vector <unsigned long int >>> reducedTree;
reducedTree.resize(size);
for (int i = 0; i<reducedTree.size(); i++)
{
reducedTree[i].resize(size);
}
reducedTree[0][0].push_back(5);
reducedTree[0][0].push_back(10);
cout << reducedTree[0][0][0] << endl;
cout << reducedTree[0][0][1] << endl;
reducedTree[0][0].erase(reducedTree.begin()+1);

return EXIT_SUCCESS;
}

```

### #4 machoolah

Reputation: -6
• Posts: 87
• Joined: 17-May 09

## Re: array of vectors

Posted 07 July 2009 - 08:04 AM

machoolah, on 7 Jul, 2009 - 06:20 AM, said:

GWatt, on 6 Jul, 2009 - 08:33 AM, said:

To create a vector of vectors goes something like this
vector <vector <string> > vec;
You can use the resize function to allocate space to the vector(s)
```vec.resize(10);
for (int i = 0; i < vec.size(); i++)
vec[i].resize(10);

```

and now the vector of vectors can hold 10 vectors of strings and each vector of strings can hold 10 strings.

How do you remove a single item from a multi-dimensional vector esp. a 3-D one?
what is wrong with the following code? I get an error when I want to remove an item.
```#include <iostream>
#include <vector>

using namespace std;

int main()
{
int size = 5;
vector < vector < vector <unsigned long int >>> reducedTree;
reducedTree.resize(size);
for (int i = 0; i<reducedTree.size(); i++)
{
reducedTree[i].resize(size);
}
reducedTree[0][0].push_back(5);
reducedTree[0][0].push_back(10);
cout << reducedTree[0][0][0] << endl;
cout << reducedTree[0][0][1] << endl;
reducedTree[0][0].erase(reducedTree.begin()+1);

return EXIT_SUCCESS;
}

```

nevermind, I got an answer, I should've used
```reducedTree[0][0].erase(reducedTree[0][0].begin()+1);

```