以前我要建立一个二维数组,总是使用 
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;
}
}

最新文章

  1. session 和 cookie区别
  2. WIN 下的超动态菜单(二)用法
  3. Spring 通过maven pom文件配置初始化
  4. python实现批量ping IP,并将结果写入
  5. ubuntu切换root
  6. Eclipse+GitHub
  7. Delphi:窗体自适应屏幕分辨率的改进
  8. TestNG基本注解(注释)
  9. Struts2+Spring+Ibatis集成合并
  10. NOIP前刷水行动
  11. 在java中的Try Catch块-------------异常处理(2)
  12. SQL-Oracle游标
  13. Struts2 之 Action 类访问 WEB 资源
  14. [C++ Primer Plus] 第11章、使用类(一)程序清单——重载 P408
  15. Android Studio指定引用jnilibs 特定CPU架构的so库文件
  16. 题解——HDU 2089 不要62(数位DP)
  17. POJ2182 Lost Cows
  18. 启动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
  19. &lt;构建之法&gt;前三章读后感—软件工程
  20. Python 的 if __name__ == &#39;__main__&#39;

热门文章

  1. css文本截字,超出文本省略号显示
  2. js bom和dom
  3. 内存泄漏导致程序killed
  4. linux课后作业1
  5. Linux中的目录功能(Red Hat 7)
  6. Go语言中的HTTP
  7. 【Leetcode】647. Palindromic Substrings
  8. 43-Identity MVC:UI
  9. 笔记-flask-原理及请求处理流程
  10. 今天买了个pro,开始ios开发