Array made of string and int?

Hello there,

so for a project im working on I need to be able to construct an array that saves a [club Name] and different [int] values, thus I guess I need to use two different data types, right?

Might then look something like this:
1
2
3
4
5
6
7
8
9
10
 
 array[club][victories]
 array[club][defeats]
 array[club][points]
 array[club][rank]

 array[club2][victories]
 array[club2][defeats]
 array[club2][points]
 array[club2][rank]


So that I can store them in a fixed order and have the output be sorted by using the last [rank] that gets modified in a sort-function i will still have to think of.

Question is, is there any way of creating an array in C++ that stores [string] and [int] like that?

No structs/classes please :P

An array can only store elements of the same type. If you don't want to use structs or classes you need to have two arrays - one for the strings and one for the ints.

Why don't you want to use structs or classes?
You could also hideously use a std::map<std::string, std::vector<int>> where the list of integers at the end of the map implicitly have some order that represents those values.

It's hideous and I'd also be inclined to ask why you're not using classes or structs.
Well, I'm no allowed to.
I want to use structs because im sure it's easier, but for this project (didn't mention it's a school project) we are only allowed to use anything we've talked about so far, that excludes structs, classes, objects, maps and a lot more.

I got it though, as I intend to only output/save the data via "i", i just took 2 arrays
1
2
3
4

 string VereinName[18];
 int VereinWerte[18][6];


so when i refer to VereinWerte[i][2] i also refer to VereinWerte[i] and thus have my two arrays "attached" to each other.

Do you think, for creating a list and sorting that list by
VereinWerte[i][0] > VereinWerte[i][1] > VereinWerte[i][2]>VereinWerte[i][3]> VereinWerte[i][4]

and having VereinWerte[i][5] as the Ranking,

this is a clean and smooth solution?

might look like this:

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

  //comparing VereinWerte[i][y] (y < 5);
  //highest rated VereinWerte gets VereinWerte[i][5] = 1;


 k = 0;

do{
				k = k + 1;

				for (i = 0; i < 18; ++i){
					if (VereinWerte[i][5] == k){
						f << setw(2) << VereinWerte[i][5] << ".";
						f << setiosflags(ios::left);
						f << setw(30) << VereinName[i];
						f << resetiosflags(ios::left);
f << setw(3) << VereinWerte[i][0] + VereinWerte[i][1] + VereinWerte[i][2]
							<< setw(4) << VereinWerte[i][0]
							<< setw(3) << VereinWerte[i][1]
							<< setw(3) << VereinWerte[i][2]
							<< setw(4) << VereinWerte[i][3] << ":"
							<< setw(4) << VereinWerte[i][4]
				<< setw(6) << VereinWerte[i][0] * 3 + VereinWerte[i][1]
							<< endl;
						cout << setw(2) << VereinWerte[i][5] << ".";
						cout << setiosflags(ios::left);
						cout << setw(30) << VereinName[i];
						cout << resetiosflags(ios::left);
cout << setw(3) << VereinWerte[i][0] + VereinWerte[i][1] + VereinWerte[i][2]
			                                << setw(4) << VereinWerte[i][0]
							<< setw(3) << VereinWerte[i][1]
							<< setw(3) << VereinWerte[i][2]
							<< setw(4) << VereinWerte[i][3] << ":"
							<< setw(4) << VereinWerte[i][4]
							<< setw(6) << VereinWerte[i][0] * 3 + VereinWerte[i][1]
							<< endl;

					}



				}
			} while (k <= 18);


don't know if just that function is enough of a snippet to understand what I'm going for here :/
Why are we using the wrong tool for the job? If you really don't want class/struct then you need separate arrays.

std::string club_names[MAX];
int club_info[MAX][4]; //victories, defeats, points, rank

Then you just manually keep them in sync while working on them... or better yet, have an array of indices that changes during sorting and never actually change/move any of the data in the arrays.

int club_sorted[MAX]; //Array of indices into the original arrays, but can be sorted in any order.
Topic archived. No new replies allowed.