How to make a 2d array of different types?

The question wording isn't very helpful, but basically I want to have a 2d container of some sort, and one dimension will be a list of class objects and the second, inner dimension will be a list of integers associated with that object.
This code is incorrect, but it would look like so:
 
  std::vector<Class std::vector<int>> 2dVector;

How would I achieve this?
Would it not be feasible to contain the list of integers within the object?

If not, you could have a map or a vector of pairs.
Would it not be feasible to contain the list of integers within the object?
Well, I'm not sure, it might be. I'm working with a large amount of Text class objects and I'm trying to find the most systematic way to create and store them all. I haven't thought about a vector of pairs though. Would that be compatible if one of the pairs was an array?
Hope this can help. I would create a vector instead. Hope this is what you need.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// CPLUSPLUSFORUM_HELP1.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <string>

using namespace std; 

class Persons
{
public:
	Persons() : name(""), age(0) {}
	Persons(const string &_name, int _age) : name(_name), age(_age) {}
	Persons(const Persons &rhs) : name(rhs.name), age(rhs.age) {}
	Persons operator=(const Persons &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		age = rhs.age;
		return *this;
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _age)
	{
		name = _name;
		age = _age;
	}
	
	const string &Name() const { return name; }
	int Age() const { return age; }
	
private:
	string name;
	int age;
};


int main()
{
	//Create a list with persons and fill it:
	  
	vector<Persons> myList;
	myList.push_back(Persons("Charles", 22));
	myList.push_back(Persons("Darwin", 21));
	myList.push_back(Persons("Phytagoras", 75));
	myList.push_back(Persons("Michelle", 65));
	myList.push_back(Persons("Carl", 76));
	myList.push_back(Persons("Cathrine", 54));
	myList.push_back(Persons("Marielle", 25));
	myList.push_back(Persons("Patrick", 85));
	myList.push_back(Persons("Oliver", 48));
	myList.push_back(Persons("Elinette", 52));
	
}



Maybe you can use a Multiimensional Array ;)
Would that be compatible if one of the pairs was an array?

Yes, but not plain arrays.

std::vector< std::pair< Text, std::vector<int> > > foo;
@patriic48, yes a multidimensional array is much more down my alley.
myList.push_back(Persons("Charles", 22));
myList.push_back(Persons("Darwin", 21));
myList.push_back(Persons("Phytagoras", 75));
myList.push_back(Persons("Michelle", 65));
...
I'm trying to avoid exactly this so I won't be typing out a list of hundreds of definitions.
Going off the idea of multidimensional arrays and taking keskiverto's suggestion
std::vector< std::pair< Text, std::vector<int> > > foo;
Here is what I have in mind:
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<std::pair<Text, std::vector<int>>> textVals_;

//truncated array for example purposes
int vals[4][3] = {
		{0, 0, 0},
		{0, 0, 0},
		{0, 0, 0},
		{0, 0, 0}
}
for(int i - 0; i < 4; i++){
textVals_.push_back(std::make_pair(text, vals[i]));
}

I'm having trouble because in the push_back() statement, vals[i] throws an error that textVals_ doesn't take an int* as an argument. I want to create a multidimensional array of each Text object's values that way I can have it all stored in one place, but I also need to store that info in a pair with its respective text object. As I'm typing this out, I realize how ridiculous this is to do. What's a better method to store objects and their info?
Got it! This is great because I've been freaking out for a while about how to efficiently create objects, and I'd say this is good enough for me. So here it is:
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
static int objCounter = 0;
Class::Class(){
//new object = +1 for the static object counter
objCounter++;
}
const static int arr[4][3] = {
//insert actual values here, each new row is a new object's values
//this is the only hardcoded part there is, which annoys me
		{0, 0, 0},
		{0, 0, 0},
		{0, 0, 0},
		{0, 0, 0}
}
//a very efficient way that I found to assign object values for every object created, as long as the array still has room of course
void Text::AssignVals(){
	for(int i = 0; i < 20; i++){
//if the number of created objects is equal to the selected row in array
		if(objCounter == i){
//any function goes here, just make sure to use the array's dimensions to assign values
			text.setPosition(arr[i - 1][0], arr[i - 1][1]);
			text.setCharacterSize(arr[i - 1][2]);
			text.setRotation(arr[i][3]);
		}
	}
}
Topic archived. No new replies allowed.