I have a function declaration as follows
void set_values(float values[4][4]);
If I call the function like this everything is OK.
float values[4][4] = {
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
};
mat1.set_values(values);
However i thought that I could take the array declared in the curly braces and pass it directly into the function like this:
mat1.set_values({
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 },
{ 1, 2, 3, 4 }
});
But this gives me a compile error too many initializer values
Why does the first code work but not the second one?
Your function expects an array object, not an initialization list for such an object.
Related
I am trying to store a cell-like structure in C++, where its elements can have arrays of different lengths like the following example:
myMultiSizedArray = {
{ 1, 2, 4 },
{ 3, 5, 6, 7 },
{ 7, 8, 9, 10 },
{ 1, 3 },
{ 4, 5, 8 },
{ 9, 10 }
{ 5 } }
I am thinking of using a vector in a struct such as the following:
struct f
{
std::vector<int> elements;
};
std::vector<f> myMultiSizedArray;
I would appreciate it if the community could give me their feedback. Are there better, more efficient approaches? Does C++ provide a means for this? Thank you
As mentioned by other users as comment, you could use a vector inside another vector as in piece of code below:
using namespace std;
vector<vector<int>> myMultiSizedArray;
myMultiSizedArray.push_back({ 1, 2, 3, 4 });
myMultiSizedArray.push_back({ 6, 5, 200, 3, 2, 1 });
use "vector< vector< int > >" is better
I am early in c++ programming. I want to send a 3D array to a function.same:
const int MaxNumberOfLegs=function1();
double D_and_L_Of_Legs[4][2][MaxNumberOfLegs] = { { {1, 2, 3.4, 4} , {1, 2, 3, 4} },
{ {1, 2, 3, 4.5} , {1, 2, 3} },
{ {1.8, 2, 3, 4} , {1, 2, 3, 4} },
{ {} , {} }
};
function2(D_and_L_Of_Legs);
and function2 is same:
void ProcessImage(double D_and_L_Of_Legs[4][2][MaxNumberOfLegs]){
}
in my code, MaxNumberOfLegs variable calculate with function1 and not a const variable. and in c++ when we want to send an array we must write dimention of array.
Now my question is, How can i send 3d array when third dimention is not const??
This function will do:
void ProcessImage(double D_and_L_Of_Legs[][][], int MaxNumberOfLegs){
}
And when you want to call, do this:
const int MaxNumberOfLegs=function1();
double D_and_L_Of_Legs[4][2][MaxNumberOfLegs] = {
{ {1, 2, 3.4, 4} ,{1, 2, 3, 4} },
{ {1, 2, 3, 4.5} , {1, 2, 3} },
{ {1.8, 2, 3, 4} , {1, 2, 3, 4} },
{ {} , {} }
};
function2(D_and_L_Of_Legs,MaxNumberOfLegs);
Alternative method:
Try to declare MaxNumberOfLegs out side the function as a global variable and keep all the code same. ie int MaxNumberOfLegs=function1(); put this on the top of the code. and inside the function make MaxNumberOfLegs=function1();.
Im using VS2013 along with the SystemC library from Allegro. I was trying to initialize two arrays as follows:
int pathObs1[19] = {10,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,10};
int Map[10][4] = {
{ 0, 3, 1, 4 }, //Grid 1
{ 1, 3, 2, 4 }, //Grid 2
{ 2, 3, 3, 4 }, //Grid 3
{ 3, 3, 4, 4 }, //Grid 4
{ 4, 3, 5, 4 }, //Grid 5
{ 5, 3, 6, 4 }, //Grid 6
{ 6, 3, 7, 4 }, //Grid 7
{ 6, 2, 7, 3 }, //Grid 8
{ 6, 1, 7, 2 }, //Grid 9
{ 6, 0, 7, 1 } //Grid 10
};
However i received the error the above error. I saw some questions on SO which had the same issue, however I dont think they were dealing with SystemC. Any easy workaround for this in SystemC since im trying to initialize inside my SC_MODULE header/constructor?
Edit: I had a typo in my array initialization. Still get the same error.
2dArray[m][n] means m rows n columns so you can keep n values in each row but in your code you defined matrix which had 3 columns but still you are assigning 4 values.
You can use a loop for filling the array:
#include <iostream>
#include <stdlib>
int main()
{
srand(time(null));
int map[10][4];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 4; j++)
{
map[i][j] = rand(); // you can write smth like rand() % 5 to make a limit of the values
}
}
return 0;
}
So I have a pointer to a 2D array like so:
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
int* ptr[sizeof(board[0]) / sizeof(board[0][0])] = board;
I'm trying to follow this example. But for some reason I'm getting the error:
IntelliSense: initialization with '{...}' expected for aggregate
object
Any idea what the problem is?
Assign pointer to the first element of the array like below
int (*ptr)[5] = board;
Note: Column size [5] in the pointer declaration should be equal to the original 2 dimension array column size [5].
Declaring row size [3] is optional.
int main() {
int board[3][5] = { 3, 5, 2, 2, 1, 3, 4, 34, 2, 2, 3, 4, 3, 223, 923 };
/*
// 3 Rows 5 Columns Matrix
int board[3][5] = { {3, 5, 2, 2, 1 },
{3, 4, 34, 2, 2 },
{3, 4, 3, 223, 923}
};
*/
// Assign pointer to the first element of the array
int (*ptr)[5] = board;
for(int i=0; i< (3*5); i++) {
std::cout<<(*ptr)[i]<<std::endl;
}
return 0;
}
A 2D array is not the same as an array of pointers. You cannot directly convert one to the other.
I just needed to put () around the *ptr. I have no idea how this fixes it but now I can do ptr[1][2].
I have the following matrix:
unsigned wins[8][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } };
how to convert it into a std::vector?
You can use the two iterator constructor to instantiate a vector with a copy of of the data in wins:
unsigned* start = &wins[0][0];
std::vector<unsigned> vwins(start, start + (8 * 3));
This relies on pointer arithmetic, the fact that pointers are iterators, and the fact that 2D arrays are contiguous blocks, essentially 1D arrays with clever indexing.
Since I don't know whether you want a 2D vector or not, I'll handle the 2D case since juanchopanza handled the 1D case. :) If you're using C++11, then you can just do this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<int>> wins = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } };
for(vector<int> & row : wins) {
for(int &col : row) {
cout << col << " ";
}
cout << endl;
}
return 0;
}
This example uses C++11 initializer lists to create an analogous structure, also called wins. I also wrote a little code to show how you could loop through it to print it out in a sensical order.
Hope this helps! :)