operator []还是什么? [英] operator [] or what ?
问题描述
我写二维数组的包装类。
我如何通过运算符[]访问一个元素?
我需要类似的东西这个:
CMyArray< int> A(4,4); // array" int A [4] [4]";
int value = A [2] [3];
// - -----------------------
template< class TType>
class CMyArray >
{
public:
CMyArray(int x,int y);
.....
私人:
TType **数据;
....
};
数组的创建:
data = new TType * [x];
for(int i = 0; i< x; i ++)data [i] =新TType [y];
我需要这个类操作符[] - 我怎么写它?
PS我知道关于STL,矢量等但是我想尝试通过
我自己手动完成。
(我的英文为sory)
I write 2-dimensional array''s wrapper class.
And how I can access to the one element through the operator [] ?
I need something like this:
CMyArray<int> A(4, 4); // array "int A[4][4]";
int value = A[2][3];
//------------------------
template <class TType>
class CMyArray
{
public:
CMyArray(int x, int y);
.....
private:
TType **data;
....
};
Creation of array:
data = new TType*[x];
for (int i=0; i<x; i++) data[i] = new TType[y];
And I need for this class operator [] - how I can write it ?
P.S. I know about STL, vector etc. but I want to try to do it manually by
myself.
(sory for my english)
推荐答案
Marchello写道:
Marchello wrote:
我写的是二维数组'的包装类。
以及如何我可以通过运算符[]访问一个元素?
我需要这样的东西:
CMyArray< int> A(4,4); // array" int A [4] [4]" ;;
int value = A [2] [3];
// --------- ---------------
模板<类TType>
类CMyArray
公共:
CMyArray(int x ,int y);
....
私人:
TType **数据;
...
};
创作数组:
data = new TType * [x];
for(int i = 0; i< x; i ++)data [i] = new TType [y];
I write 2-dimensional array''s wrapper class.
And how I can access to the one element through the operator [] ?
I need something like this:
CMyArray<int> A(4, 4); // array "int A[4][4]";
int value = A[2][3];
//------------------------
template <class TType>
class CMyArray
{
public:
CMyArray(int x, int y);
....
private:
TType **data;
...
};
Creation of array:
data = new TType*[x];
for (int i=0; i<x; i++) data[i] = new TType[y];
And I need for this class operator [] - how I can write it ?
1.你可以编写operator []来返回TType *
TType * CMyArrayoperator [](int x)
{
返回数据[x];
}
2.或者,您也可以使用operator()。我已经修改了代码
,使用模板参数x和y,而不是在
构造函数中给出x和y。当然,这需要在编译时
编译时知道x和y的值。
模板<类TType,int x,int y>
类CMyArray
{
公开:
CMyArray()
{
data [1] [2] = 100;
}
TType operator()(int m,int n)
{
返回数据[m] [n];
}
私人:
TType数据[x] [y];
};
用法:
CMyArray< int, 4,5 GT; arr;
int p = arr(4,5)//与arr相同[4] [5]
HTH。
1. You can write operator[] to return TType*
TType* CMyArrayoperator[](int x)
{
return data[x];
}
2. Alternatively, you can also use operator(). I have modified the code
a bit to use template parameters x and y instead of giving x and y in
constructors. Of course this needs the values of x and y to be known at
compile time.
template <class TType, int x, int y>
class CMyArray
{
public:
CMyArray()
{
data[1][2] = 100;
}
TType operator()(int m, int n)
{
return data[m][n];
}
private:
TType data[x][y];
};
Usage :
CMyArray<int, 4,5> arr;
int p = arr(4,5) // same as arr[4][5]
HTH.
Marchello写道:
Marchello wrote:
我写的是二维数组'的包装类。
以及如何我可以通过运算符[]访问一个元素?
我需要这样的东西:
CMyArray< int> A(4,4); // array" int A [4] [4]" ;;
int value = A [2] [3];
// --------- ---------------
模板<类TType>
类CMyArray
公共:
CMyArray(int x ,int y);
....
私人:
TType **数据;
...
};
创作数组:
data = new TType * [x];
for(int i = 0; i< x; i ++)data [i] = new TType [y];
PS我知道关于STL,矢量等但我想尝试自己手动完成它。
(我的英文为sory)
I write 2-dimensional array''s wrapper class.
And how I can access to the one element through the operator [] ?
I need something like this:
CMyArray<int> A(4, 4); // array "int A[4][4]";
int value = A[2][3];
//------------------------
template <class TType>
class CMyArray
{
public:
CMyArray(int x, int y);
....
private:
TType **data;
...
};
Creation of array:
data = new TType*[x];
for (int i=0; i<x; i++) data[i] = new TType[y];
And I need for this class operator [] - how I can write it ?
P.S. I know about STL, vector etc. but I want to try to do it manually by
myself.
(sory for my english)
就是这样写的:
TType operator [](int a)
{
返回数据[a];
}
没问题
just write like this:
TType operator[](int a)
{
return data[a];
}
is ok
Neelesh Bodas写道:
Neelesh Bodas wrote:
Marchello写道:
Marchello wrote:
我编写了二维数组的包装类。
我如何通过运算符[]访问一个元素?
我需要这样的东西:
CMyArray< int> A(4,4); // array" int A [4] [4]" ;;
int value = A [2] [3];
// --------- ---------------
模板<类TType>
类CMyArray
公共:
CMyArray(int x ,int y);
....
私人:
TType **数据;
...
};
创作数组:
data = new TType * [x];
for(int i = 0; i< x; i ++)data [i] = new TType [y];
I write 2-dimensional array''s wrapper class.
And how I can access to the one element through the operator [] ?
I need something like this:
CMyArray<int> A(4, 4); // array "int A[4][4]";
int value = A[2][3];
//------------------------
template <class TType>
class CMyArray
{
public:
CMyArray(int x, int y);
....
private:
TType **data;
...
};
Creation of array:
data = new TType*[x];
for (int i=0; i<x; i++) data[i] = new TType[y];
And I need for this class operator [] - how I can write it ?
1.你可以编写operator []来返回TType *
TType * CMyArrayoperator [](int x)
{
返回数据[x];
}
2.或者,您也可以使用operator()。我已经修改了一些代码来使用模板参数x和y而不是在
构造函数中给出x和y。当然,这需要在编译时知道x和y的值。
模板<类TType,int x,int y>
类CMyArray > {
公众:
CMyArray()
{
数据[1] [2] = 100;
}
TType运算符( )(int m,int n)
{
返回数据[m] [n];
}
私有:
TType数据[x] [y];
};
用法:
CMyArray< int,4,5> arr;
int p = arr(4,5)//与arr相同[4] [5]
HTH。
1. You can write operator[] to return TType*
TType* CMyArrayoperator[](int x)
{
return data[x];
}
2. Alternatively, you can also use operator(). I have modified the code
a bit to use template parameters x and y instead of giving x and y in
constructors. Of course this needs the values of x and y to be known at
compile time.
template <class TType, int x, int y>
class CMyArray
{
public:
CMyArray()
{
data[1][2] = 100;
}
TType operator()(int m, int n)
{
return data[m][n];
}
private:
TType data[x][y];
};
Usage :
CMyArray<int, 4,5> arr;
int p = arr(4,5) // same as arr[4][5]
HTH.
不需要像这样写,arr [4] [5]可以调用两次operator []而不是。
no need write like this,arr[4][5] can be call twice operator[] instead.
这篇关于operator []还是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!