想知道如何定义动态的一维二维数组?
一维二维的int数组如何定义?
一维二维的char数组如何定义?
比如:一个类的构造函数有x,y两个int参数,如何根据这两个参数动态构成数组
cls1::cls1(int x,int y)
{
int/char mp[x][y];
...
}
一维二维的int数组如何定义?
一维二维的char数组如何定义?
比如:一个类的构造函数有x,y两个int参数,如何根据这两个参数动态构成数组
cls1::cls1(int x,int y)
{
int/char mp[x][y];
...
}
解决方案 »
- 控件焦点自动切换的问题
- LINK : fatal error LNK1104: cannot open file "ToolkitPro1501vc60D.lib"
- 消息钩子实现改键 求助!~
- MDI下,怎么在一个子窗口显示MS grid控件
- 请问DICOM格式转化成BMP怎么办?
- 求救!关于析构函数的问题,在线等!
- InternetGetCookie函数的疑惑,高手解答
- 如何在 View 类中编写press any key to continue功能模块
- vfw win7-64bit vc2013 这种环境下,谁用成功了vfw?
- 帮帮忙吧,各位大侠!!!!我只有这么多分了:(
- 如何获得新创建的对话框的句柄
- 关于SetCapture
{
int/char *m_pBuf=new int/char [x*y];
int m_x=x;
int m_y=y;
...
}
访问的时候需要自己转换一下下标
query(int x,int y){
return m_pBuf[x*m_y+y];
}
大概就是这样子吧
{
CArray<int,int> iArray1;
CArray<int,int>* GetArrPoint();
}
Class CInt2
{
CArray<CInt1,CInt1> iArray2;
}
#include <vector>
using namespace std;void main()
{
vector<char> ca;
vector<int> ia;
for(int i = 0; i < 10; i++)
{
ca.push_back(rand());
ia.push_back(rand()); } for(int j = 0; j < 10; j++)
cout << ca[j] << ',' << ia[j] << endl;
}
c2a[0].push_back('a');
cout << c2a[0][0];vector<vector<int> > i2a;
同上
注意vector<vector<int> 和>之间有空格
vector<vector<int> >
再自己作一些两维或多维的[]操作重载思路你自己可以想得到的
class dynamic_array
{
public:
dynamic_array(){};
dynamic_array(int rows, int cols)
{
for(int i=0; i<rows; ++i)
{
data_.push_back(std::vector<T>(cols));
}
}
// other ctors ....
std::vector<T> & operator[](int i) { return data_[i]; } const std::vector<T> & operator[] (int i) const { return data_[i]; } // other accessors, like at() ... // other member functions, like reserve()....private:
std::vector<std::vector<T> > data_;
};
int main()
{
dynamic_array<int> a(3, 3);
a[1][1] = 2;
int x = a[1][1];
return 0;
}////////////////////////////////
另,我自己以前也使用 <vector<vector<T> > ,也不错。