How to return a multidimensional array

I am attempting to return a multidimensional array from within a function of a class. Is this possible?

I can only get the data by making the array public and using user.line[a][b] to get the data. Is it possible to get the data from line[a][b] from int main()?

Here is my code:

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
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

class File_input {
	private:
	int a;
	int b;
	string input;
	public:
	string line[16][2];
	string *getfile(string cfgfile) {
		ifstream config(cfgfile.c_str());
		a=0;
		while(getline(config,input)) {
			if(!input.find_first_of("@")) {
				a=a++;
				b=1;
				line[a][b]=input;
			}
			else {
				a=a++;
				b=2;
				line[a][b]=input;
			}
		}
		return *line;
	}
};

int main() {
	File_input user;
	int a;
	int b;
	string *line=user.getfile("file.txt");
	b=1;
	for(a=1;a<7;a++) {
		cout<<"["<<a<<"]["<<b<<"] "<<user.line[a][b]<<endl;
	}
	b=2;
	for(a=1;a<7;a++) {
		cout<<"["<<a<<"]["<<b<<"] "<<user.line[a][b]<<endl;
	}
	return 0;
}


file.txt contains the following:

1
2
3
4
5
6
Line A
@Line B
Line C
@Line D
Line E
@Line F


Thanks.
a = a++; Either undefined behaviour or statement has no effect.
You could threat that pointer as an unidimensional representation of a matrix. However I think it would be better to define string File_input::operator()(int row, int column)
Last edited on
If I do this, should I remove the *getfile and replace with getfile (same for return line)? I'm just unsure the best/correct method for getting back the multidimensional array data from within a class function.

Thanks for your reply!
To return a multidimensional array, that is, not a matrix object, declare it like this:
int f(int x)[10][10];//f takes an integer and returns a pointer to a 10 by 10 array of ints.
This is because function calls and array indices have equal precedence, but they are left associative.

Whoops, nevermind, you can't. It's syntactically valid, but undefined semantically. Instead just return a matrix or a vector or something.
Actually you can, never mind again, here is the syntax step by step:
1
2
3
4
5
6
int i;//an integer
int i[10];//an array of integers
int i[10][10];//an array of arrays of ints
int (*i)[10][10];//a pointer to an array of arrays of ints. * has lower precedence than [].
int (*f(int))[10][10];/* a function returning a 
pointer to an array of arrays of ints. () has higher precedence than *.*/

Now do you see why C++ users call arrays 'evil'?
Last edited on
You could technically return a pointer to such a thing couldn't you? If you return a pointer to the first element you would have to do the pointer math yourself (which isn't that hard for a 2d array).

You could also use the array definitions the above poster used, though since he used a one letter function name and only gave a definition you might like to see what the fleshed out example would look like

int (*retarray(/*any argument variables would go in here*/))[10][10]{
...
return &var//assuming var is an int[10][10]
}

in case you don't know the /**/ i used above is a comment that blocks all contained text between the /* and the */ from being compiled as part of the code
Last edited on
Ok, I thought to try this using a vector, but I'm running into another problem. If this is a bad idea, I'll stick with my original. The problem I'm seeing is error: expected '{' before '(' token. I'm not sure where I'm going wrong here with vectors inside of a class as a function. Here is the class code changed to vector.

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
class File_input {
	private:
	int a;
	int b;
	string input;
	vector< vector<string> > line;
	public:
	File_input() : line(6, vector<string>(2))(string cfgfile) {
		ifstream config(cfgfile.c_str());
		a=0;
		while(getline(config,input)) {
			if(!input.find_first_of("@")) {
				a=a++;
				b=1;
				line[a][b]=input;
			}
			else {
				a=a++;
				b=2;
				line[a][b]=input;
			}
		}
		return line;
	}
};
What is this line supposed to do?
File_input() : line(6, vector<string>(2))(string cfgfile) {
I want it to be a vector function within my class. I believe that line is incorrect, I just need to create a function with a multidimensional vector with everything below that line. I'm quite sure I'm going about this wrong...
Last edited on
I just created a normal vector function that will do this without having to be in a class. However, it would be nice to be able to add this to a class correctly. Anyone know of any links that explain using classes and vectors together?

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
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>

using namespace std;

vector< vector<string> > getfile(string cfgfile) {
	vector< vector<string> > line;
	int a=2;
	int b=0;
	char c='@';
	string input;
	
	line.resize(a);

	ifstream config(cfgfile.c_str());
	while(getline(config,input)) {
		if(input.find_first_of(c)) {
			line[b].push_back(input);
		}
		else {
			line[b+1].push_back(input);
		}
	}

	return line;
}

int main() {
	int a;	// [ ][x]
	int b;	// [x][ ]

	for(b=0;b<getfile("file.txt").size();b++) {
		for(a=0;a<getfile("file.txt")[b].size();a++) {
			cout<<"["<<b<<"]["<<a<<"] "<<getfile("file.txt")[b][a]<<endl;
		}
	}

	return 0;
}
Aren't you worried about the time complexity?
1
2
for(b=0;b<getfile("file.txt").size();b++) {
		for(a=0;a<getfile("file.txt")[b].size();a++) {
O(n4)
Last edited on
That last part was mainly for me to see the short output. I'm learning this in my spare time and always open for better ways to do things.
Topic archived. No new replies allowed.