How do I make multi dimensional in C++? - c++

I have this array
int sequence[2][3][2][2][50][2] = {
{
{1},
{{
{2, 4},
{3, 5}
},
{255,0,0}
}
},
{
{2},
{{
{3, 4},
{2, 6}
},
{0,0,255}
}
}
};
Whenever I try to index the first multi dimensional array, using
int frame[2] = {sequence[1]}
I get this error "invalid conversion for 'int (*)[2][2][50][2]' to 'int' [-fpermissive]
What am I doing wrong?

Whenever I try to index the first multi dimensional array, using
int frame[2] = {sequence[1]}
Let's simplify the syntax you use:
int frame[2] = {/*list of ints*/}
This initializes a one dimensional array of 2 int, using a brace-enclosed list of integers.
An element of your outermost multi-dimensional array with arity k, is also a multi-dimensional array (with arity k-1). It is not an int.
So, there is nothing wrong with how you index the multi-dimensional array. What is wrong, is trying to initialize an array if int with a multi-dimensional array as the first value.
How should I initialize it then?
It is impossible to answer because it is not clear which of the integer values within the multi-dimensional array you want to use to initialize.
Here is a syntactically correct way to initialize frame:
int frame[2] = {sequence[0][0][0][0][0][0], sequence[0][0][0][0][0][1]};
It uses the values in the first subarray of the first subarray of the first subarray of the first subarray of the first subarray of the outermost array.

Related

Create a vector in C++ with constant values

I want to create a vector by determining the size and the constant value which the vector has, e.g. a vector of the size 5 and only 3 as values.
vector = {3, 3, 3, 3, 3}
I only know how to create a vector with zero as value with std::vector<int> vec(5);
You can use a constructor that accepts a count and a value.
std::vector<int> v (5,3);
Will result in having a vector of a size 5 and 3 as a values {3,3,3,3,3}.

Memory allocation in C++ STL for dynamic containers

When you declare 2D arrays, they are stored in contiguous memory locations and this is easy as the number of rows is fixed while declaring them.
Whereas when we declare a 2D vector vector<vector<int>> v, how does it work. As the number of rows is not fixed at all. My first guess was the new vector which you push_back into it are randomly allocated but then even that wont work as these vectors of int are randomly accessible.
My first guess is to allocate vectors of int randomly in the memory and store their address in another vector of addresses.
eg
vector<vector<int>> vmain;
vector<int> a = {1, 2, 3};
vector<int> b = {1, 2, 3};
vector<int> c = {1, 2, 3};
vmain.push_back(a);
vmain.push_back(b);
vmain.push_back(c);
is stored something similar to
vector<&vector<int>> vmain; //vector of pointer to vector
vector<int> a = {1, 2, 3};
vector<int> b = {1, 2, 3};
vector<int> c = {1, 2, 3};
vmain.push_back(&a);
vmain.push_back(&b);
vmain.push_back(&c);
Please tell me if this is the correct way.
And also for vector of maps or sets vector<map<int, int>> v1 and vector<set<int>> v2. As size of maps and sets is not fixed.
The vector object doesn't store the elements. It stores a pointer to a contiguous chunk of memory containing the elements. When you have std::vector<std::vector<int>> the outer vector contains a pointer to a contiguous chunk of memory containing vector objects, each of which have a pointer to a contiguous chunk of memory containing the ints.
std::map and std::set also don't store the elements in the object itself. Each object contains a pointer to a BST containing the elements.

Static Multi Dimensional Arrays ( C/C++)

Is memory allocated for multidimensional arrays in C or C++ always contiguous, or is the storage dependent on the compiler? If it is guaranteed to be contiguous is there a standard on it somewhere for reference? For example
int x[2][2] = { { 1 , 2 } , { 5 , 10 } } ;
Are the integers 1, 2, 5, 10 in sequence in memory ?
Arrays are guaranteed contiguous. What we have here is an array of arrays - each layer of which is contiguous. The inner most arrays we know must be {1, 2} and {5, 10}, and the outer most array must also be contiguous. Therefore, {{1,2},{5,10}} must be 1, 2, 5, 10 sequentially in memory.
Yes. Arrays are always allocated in contiguous memory location. It doesn't matter whether its a single or multi dimensional array.

Returning unique() function in C++

I came across the following function, which sorts an array passed down by main(), removes duplicates, and returns the number of unique elements. It's the last bit I'm having a hard time wrapping my head around.
int reduce(long ar[], int n) {
sort(ar, ar + n);
return unique(ar, ar + n) - ar; // ???
}
To my understanding unique() returns a pointer to the end of the segment that stores the unique values in the array. But I don't see why subtracting the array name from the iterator results in an int that equals the number of unique elements, or why unique(ar, ar+n) can't be typecasted to int to achieve the same result.
why unique(ar, ar+n) can't be typecasted to int to achieve the same result.
Because, as you said, unique returns a pointer. A pointer is a memory address, not an index. So casting a pointer to an int is meaningless.
why subtracting the array name from the iterator results in an int that equals the number of unique elements
Subtracting two pointers (into the same array) evaluates to the number of elements between them.*
* As pointed out by #Nawaz in comments below, this result is signed. So (p1 - p2) == -(p2 - p1).
Say you have an array like this:
{1, 2, 2, 3, 4, 4, 5}
After calling std::unique, you'll probably end up with this (thank-you, Nawaz), with the elements past the new end left as they used to be before the call:
{1, 2, 3, 4, 5, 4, 5}
^
std::unique returns an iterator to the new end of the array, so where the arrow is. From there it makes logical sense that subtracting the beginning of the array would return the number of unique elements. If you want to be a bit more explicit, you can use return std::distance(ar, std::unique(ar, ar + n));, which also works when the iterator doesn't support subtraction.

C++ Initialize array pointer

How do I initialize a pointer to a literal array?
I want *grid to point to the new allocated int array {1, 2, 3}.
int *grid = new int[3];
*grid = {1, 2, 3};
thank you.
You can't initialize a dynamically allocated array that way. Neither you can assign to an array(dynamic or static) in that manner. That syntax is only valid when you initialize a static array, i.e.
int a[4] = {2, 5, 6, 4};
What I mean is that even the following is illegal:
int a[4];
a = {1, 2, 3, 4}; //Error
In your case you can do nothing but copy the velue of each element by hand
for (int i = 1; i<=size; ++i)
{
grid[i-1] = i;
}
You might avoid an explicit loop by using stl algorithms but the idea is the same
Some of this may have become legal in C++0x, I am not sure.
#above grid points to the address location where the first element of the array grid[] is stored. Since in C++ arrays are stored in contiguous memory location, you can walk through your array by just incrementing grid and dereferencing it.
But calling grid an (int*) isnt correct though.
I'm not sure if this is obvious but you can do this in one line.
int *grid = new int[3] {1, 2, 3};
Since this is C++ we are talking about you can also split it into
two files. Where your .h file contains:
int *grid;
And your .cpp file contains:
grid = new int[3] {1, 2, 3};
Use the following code, grid is a pointer, grid[] is an element of that pointer.
int grid[] = {1 , 2 , 3};