vector创建2维数组
2024-08-25 18:34:01
以前我要建立一个二维数组,总是使用
int N=5, M=6;
vector<vector<int> > Matrix(N);
for(int i =0; i< Matrix.size(); i++){
Matrix[i].resize(M);
}
上面多写了三行,就是为了指定 Matrix 的烈数。
今天才发现,可以用更简单的方法完成上面的工作
下面的一行就可以代替上面的四行
vector<vector<int> > Matrix(N, vector<int>(M));
#include <iostream>
using namespace std; #include <vector> int main(void)
{
vector <vector <int>> array(,vector<int>()); // vector<vector <int>>array(3);
// for (int i = 0; i <3; i++)
// array[i].resize(3);//设置数组的大小3X3
//现在你可以和使用数组一样使用这个vector for (int i = ; i <; i++)
for (int j = ; j <; j++)
array[i][j] = (i*j);
//输出
for (int i = ; i <; i++)
{
for (int j = ; j <; j++)
cout <<array[i][j] << " ";
cout <<endl;
} cout << "-------------" << endl;
array[].swap(array[]); //数组交互 for (int i = ; i <; i++)
{
for (int j = ; j <; j++)
cout << array[i][j] << " ";
cout << endl;
}
}
最新文章
- session 和 cookie区别
- WIN 下的超动态菜单(二)用法
- Spring 通过maven pom文件配置初始化
- python实现批量ping IP,并将结果写入
- ubuntu切换root
- Eclipse+GitHub
- Delphi:窗体自适应屏幕分辨率的改进
- TestNG基本注解(注释)
- Struts2+Spring+Ibatis集成合并
- NOIP前刷水行动
- 在java中的Try Catch块-------------异常处理(2)
- SQL-Oracle游标
- Struts2 之 Action 类访问 WEB 资源
- [C++ Primer Plus] 第11章、使用类(一)程序清单——重载 P408
- Android Studio指定引用jnilibs 特定CPU架构的so库文件
- 题解——HDU 2089 不要62(数位DP)
- POJ2182 Lost Cows
- 启动servlet报错:The servlets named [DemoServlet] and [main.java.com.wlf.demo.servlet.DemoServlet] are both mapped to the url-pattern [/hello] which is not permitted
- <;构建之法>;前三章读后感—软件工程
- Python 的 if __name__ == &#39;__main__&#39;