array of vectors

how to create an array of arrays

Page 1 of 1

3 Replies - 34120 Views - Last Post: 07 July 2009 - 08:04 AM Rate Topic: -----

#1 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • 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.

Thank you for your help in advance,

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  Icon User is offline

  • member icon

Reputation: 269
  • View blog
  • Posts: 3,067
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 machoolah  Icon User is offline

  • D.I.C Head

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

Re: array of vectors

Posted 07 July 2009 - 07:20 AM

View PostGWatt, 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 machoolah  Icon User is offline

  • D.I.C Head

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

Re: array of vectors

Posted 07 July 2009 - 08:04 AM

View Postmachoolah, on 7 Jul, 2009 - 06:20 AM, said:

View PostGWatt, 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);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1