abcd
abcd

Reputation: 1

Data structure for a group of grouped data in C++

I need to create a group of grouped data. the size of each internal group is not same. i should be able to access any member of the any group using indices. Which data structure can I use in C++?

Upvotes: 0

Views: 1760

Answers (2)

BlackJack
BlackJack

Reputation: 2876

You need to add more information. How are the groups stored? If each group is an array, you can use an array of arrays i.e. (type)**AoA and each index of AoA would point to an array that contains the underlying group. For example:

#include <stdlib.h>

int main()
{
   int group1[3] = {1,2,3};
   int group2[3] = {4,5,6};

   int** groupArray = (int**)malloc(sizeof(int*) *2);
   groupArray[1] = group1;
   groupArray[2] = group2;

   return 0;
}

Since it's C++, a vector of vectors would be a better choice, but the concept is the same. Also, read up on "B-Trees" - it's a good way to store what you're asking for, but that depends on how the groups are themselves stored.

Upvotes: 0

Nicola Musatti
Nicola Musatti

Reputation: 18218

If all your data are of the same type you can use a vector of vectors, e.g.

std::vector< std::vector<YourDataType> >

Upvotes: 1

Related Questions