Accessing array elements of lists of arrays

Started by
4 comments, last by Perost 15 years, 4 months ago
Hi, I'm writing some code in C++. This code uses lists of int* that are new'd into arrays at runtime, ie std::list<int*> intList; int* someInt = new int[someSize]; intList.push_back(); After I've pushed them back, how do I access individual elements of an array? I've tried... for(std::list<int*>::iterator it = intList.begin(); it != intList.end(); ++it) { (*it)[0] = 3; } But no dice. Trying (*it) de-references it to an int*, but I don't know how to then access array elements (I tried jumping along memory using ( + x * sizeof(int)) but that didn't work either). (**it) double de-references it to an int which seems to be ok at compile time, but obviously being able to access only the first element of the array is a problem.
Advertisement
What do you mean "no dice"? Were you expecting there to be dice?
Quote:Original post by Winegums
Hi, I'm writing some code in C++. [...]

Take one step back and explain to us why you think you need a list of dynamically allocated arrays. Maybe then we can help.
Quote:Original post by DevFred
Quote:Original post by Winegums
Hi, I'm writing some code in C++. [...]

Take one step back and explain to us why you think you need a list of dynamically allocated arrays. Maybe then we can help.


Working on a problem posted on project euler. Problem statement is..

Quote:

By starting at the top of the triangle below and moving to
adjacent numbers on the row below,
the maximum total from top to bottom is 23.

3
7 5
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle
below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes,
it is possible to solve this problem by trying every route.
However, Problem 67, is the same challenge with a triangle containing one-hundred rows;
it cannot be solved by brute force, and requires a clever method! ;o)


I thought I'd read the data in by reading the liens in one by one, allocating an array of size (line number) and then adding that to the list. I didn't think an array would really work here, since they're constant in both dimensions.
Yes, a sequence of sequences will work.

Why go half way? Why not std::list<std::list<int> >, or even std::vector<std::vector<int> >? (I would probably typedef these to ease dealing with iterators)
Also, are you trying to solve the problem by bruteforce? It can easily be solved with pen and paper (and perhaps a calculator). A hint: Pick any element in the pyramid, and think about what the maximum total of the path to that element is.

This topic is closed to new replies.

Advertisement