The structure is defined as
struct state{
string node_name;
int node_no;
int node_val;
int occupant;
vector<int>node_con;
};
state s[100][100]
I want to send it to a function along with i and j values , where s[i][j] , (i->rows , j-> columns) . How will the struct be sent with both i and j ?
This way
void f(StructName (*a)[100], int i, int j) {
}
Please read about two dimensional arrays and pointer to arrays. Alternatively in C++ you can pass it by a reference, which will make it not decay to its first element
void f(StructName (&a)[100][100], int i, int j) {
}
in C there is no way (AFAIK)
in C++ you could do this
template <class T, int N, int M>
void f(T (&a)[N][M])
{
//...
}
Alternatively, you could pass the dimensions manually, or hard-code them
In C, you can wrap it up in another structure :-)
I see stuff that doesn't look like C in your code ...
struct state {
string node_name;
int node_no;
int node_val;
int occupant;
vector<int>node_con;
};
struct wrap {
int i;
int j;
struct state (*ps)[];
};
int main(void) {
struct state s[100][100];
struct wrap x;
x.i = 100;
x.j = 100;
x.ps = s;
fx(x);
return 0;
}
You mean passing an array of structures. I think it should be this:
struct state{
string node_name;
int node_no;
int node_val;
int occupant;
vector<int>node_con;
};
state s[100][100];
void doSomething(state theState[][100], int i, int j)
{
cout << theState[i][j].node_name << endl;
}
int main()
{
s[0][1].node_name = "s[0][1]";
doSomething(s, 0, 1);
}
Related
I have the following code:
template <int Size>
class A
{
public:
static int size;
static int myArray[Size];
};
The size variable I can set by:
template <int Size>
int A<Size>::size=SomeQuantity;
I would like to initialize the array from 0...Size in steps of 1. E.g, if Size=10, myArray =[0,1,2,....,9]
Can the initialization of a static array be assigned to a function? Is there any C++ built in way of doing this?
Edit
I could define inside the class:
static int initArray()
{
for( int i = 0; i<sizeof(myArray)/sizeof(myArray[0]); i++)
{
myArray[i]=i;
}
return 0;
}
And afterwards initialize this as:
template <int Size>
int A<Size>::myArray[Size]={initArray()};
Kind regards
Write your own function, eg
int* MakeArray(const int size){
int* res = new int[size];
//stl generate transform or just a loop
for (auto i=0;i<size;i++) res[i]=i;
return res;
}
I wrote a simple class that performs basic arithmetic operations using a method that receives an index and two values to compute.
The index indicates which operation to perform in a table that contains pointers to functions.
Here is my code:
#include <iostream>
using namespace std;
class TArith
{
public:
static const int DIV_FACTOR = 1000;
typedef int (TArith::*TArithActionFunc)(int,int);
struct TAction
{
enum Values
{
Add,
Sub,
count,
};
};
int action(TAction::Values a_actionIdx, int a_A, int a_B)
{
return ( this->*m_actionFcns[a_actionIdx] )(a_A,a_B);
}
private:
int add(int a_A, int a_B)
{
return a_A + a_B ;
}
int sub(int a_A, int a_B)
{
return a_A - a_B ;
}
static TArithActionFunc m_actionFcns[TAction::count];
int m_a;
int m_b;
};
TArith:: TArithActionFunc TArith:: m_actionFcns[TAction::count] = {
TArith::add,
TArith::sub
};
void main(void)
{
TArith arithObj;
int a=100;
int b=50;
for(int i = 0 ; i <TArith::TAction::count ; ++i)
{
cout<<arithObj.action( (TArith::TAction::Values)i,a,b )<<endl;
}
cout<<endl;
}
Compiler says:
'TArith::add': function call missing argument list; use '&TArith::add' to create a pointer to member
'TArith::sub': function call missing argument list; use '&TArith::sub' to create a pointer to member
why do I need to use the & symbol?
TArith:: TArithActionFunc TArith:: m_actionFcns[TAction::count] = {
TArith::add,
TArith::sub,
TArith::mul,
TArith::div
};
Correct syntax for a pointer to a member function f of a class C is &C::f. You're missing the leading &.
Try:
TArith:: TArithActionFunc TArith:: m_actionFcns[TAction::count] = {
&TArith::add,
&TArith::sub,
&TArith::mul,
&TArith::div
};
I would like to use template to specify sizes in functions. Let me give you an example.
I can do this:
int sub (int a[2][2]) {
}
int main () {
int a[2][2];
sub(a);
}
I can do this:
template<int size2>
int sub (int a[][size2]) {
}
int main () {
int a[2][2];
sub(a);
}
But what I would like is this:
template<int size1, int size2>
int sub (int a[size1][size2]) {
}
int main () {
int a[2][2];
sub(a);
}
How could I?
The better option is probably to use std::array, if you have C++11 support, or std::vector, if not. If you really want to use C-style arrays, you can pass them by reference, using the following syntax.
template<size_t size1, size_t size2>
int sub (int (&a)[size1][size2]) {
// ...
}
int main() {
int a[2][2];
sub(a);
}
As Brian Bi already wrote, the best way to go is to use std::array.
But to answer your question, it is halfway possible to do what you want.
When you use a C-style 2D array as an argument to a function, it decays to a pointer to an array of the second dimension. For example int a[6][6] decays to int (*a)[6], so the first dimension is hidden and means nothing to the compiler. The second dimension however, can be used as template parameters and can be deduced.
For example:
template<int S>
void func(int (*a)[S])
{
cout << S << endl;
}
and then:
int a[66][67];
func(a);
prints "67".
Here is another way (sub<2,2>:
template<int size1, int size2>
int sub (int a[size1][size2]) {
}
void main(int argc, char **argv)
{
int a[2][2];
sub<2,2>(a);
}
I've just tried:
class Test
{
public:
int iArray[][];
}
...is this not possible? Do I have to set a constant value?
like:
class Test
{
public:
const int iArray[5][4];
}
I want to define [x][y] later, just have the placements there. Else it wouldn't be "dynamic" and I don't want to use a vector because I want to be able to access the values by "X" and "Y".
I think better way to achieve this is to use pointers. You can do like this.
#include <cstdlib>
#include <iostream>
using namespace std;
class PointerTest {
private:
int** array;
int x, y;
public :
void setValue(int row, int col,int value);
int getValue(int row, int col);
PointerTest(int row, int col);
~PointerTest() {
for(int i=0;i<x;i++) {
delete array[y];
}
}
};
PointerTest::PointerTest(int row, int col) {
x=row, y=col;
for(int i=0;i<row;i++) {
*array=new int[col];
}
}
void PointerTest::setValue(int row, int col, int value) {
*(array[row])=value;
}
int PointerTest::getValue(int row, int col) {
return *(array[row]);
}
int main(int argc, char *argv[])
{
PointerTest* t=new PointerTest(4,5);
t->setValue(0,0,464);
cout<<"The value in array: "<<t->getValue(0,0)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
What about
tempalte <int N1, int N2> class Test
{
public:
int iArray[N1][N2];
};
?
What about putting a std::vector in a vector?
std::vector< std::vector< const int > > iArray;
There aren't many reason to use "plain" arrays in C++.
If you want to decide int iArray[][]; size later then you can use vector< vector<int> > iArray;.
The other way is to use nested new[], which would be little complex.
No this is not possible. But you can have a pointer in your class like
int **ptr;
and then in the constructor or where ever allocate the memory for your array with
ptr = (int **)malloc( the size you want );
or with the "new[]"-operator in C++.
but if you are using C++ .. the best way is to use:
std::vector< std::vector< int >> array;
class Test
{
public:
Test()
{
iArray = new int*[5];
for(int i = 0; i < 5; i++)
iArray[i] = new int[4];
}
~Test()
{
for(int i = 0; i < 5; i++)
delete[] iArray[i];
delete[] iArray;
}
int** iArray;
};
Will allow you to allocate a 2d int array at runtime (in this example it is a 5x4), but in all honestly I would use vectors as pointed out by some other posters, you don't need to worry about freeing the memory afterwards like you do with the use of new.
I need to create a function that has a parameter which is a multi-dimensional array with two dimensions being user-specified, e.g.
int function(int a, int b, int array[a][b])
{
...
}
How would I do that in C++ ?
Are the dimensions known at compile-time? In that case, turn them into template parameters and pass the array by reference:
template<int a, int b>
int function(int(&array)[a][b])
{
...
}
Example client code:
int x[3][7];
function(x);
int y[6][2];
function(y);
Assuming the dimensions are not known at compile time, you emulate a two dimensional array with a one dimensional array:
int& getat(int x, int y, int r, int c, int *array) {return array[y*c+x];}
int function(int a, int b, int *array) {
getat(4, 2, a, b, array) = 32; //array[4,2] = 32
}
or, for safety, wrap it all in a class:
template <class T>
class array2d {
std::vector<T> data;
unsigned cols, rows;
public:
array2d() : data(), cols(0), rows(0) {}
array2d(unsigned c, unsigned r) : data(c*r), cols(c), rows(r) {}
T& operator()(unsigned c, unsigned r) {
assert(c<cols&&r<rows);
return data[r*cols+c];
}
};
or, best yet, use Boost's Multidimensional Array, which will be better than anything mere mortals could write.
I'm not sure if this work, because your question and code are not the same, according to your code the function can have 3 parameters, so this would work:
int function(int a, int b, int** &array)
{
array = new int*[a];
for (int i =0;i<a;i++)
array[i] = new int[b];
// I don't know why you are returning int, probably doing something here....
}
However your question says that your function can take only one parameter, so:
if the dimensions are known at compile time, then Fred's Answer is the best (it charmed me in fact! :) ).
if not, I can't see any possible solution that allows passing more than one user-specified value other than encapsulating all these values in one object.
Like this:
class Foo {
public:
Foo(int d1, int d2)
{ a = d1; b = d2; }
int a,b;
int** array;
};
int function(Foo &f)
{
f.array = new int*[f.a];
for (int i = 0;i<f.a;i++)
f.array[i] = new int[f.b];
// I don't know why you are returning int, probably doing something here....
}
Though I find it a bad idea, in fact the function could be a parameterless method instead:
class Foo {
public:
Foo(int d1, int d2)
{ a = d1; b = d2; }
void Create() // Or could do this right in the Constructor
{
array = new int*[a];
for (int i = 0;i<a;i++)
array[i] = new int[b];
}
private:
int a,b;
int** array;
};
Still this is a bad idea, because you are reinventing the wheel, as there are a perfect class in the STL to do all the work for you:
vector< vector<int> > v; // Now v is a 2D array