zoukankan      html  css  js  c++  java
  • vector<vector<int>> 简单知识介绍

     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 bool cmp(const vector<int> &a, const vector<int> &b) {
     6     return a[1] < b[1];
     7 }
     8 int main()
     9 {
    10     vector<vector<int>> vet;
    11     int RR, CC;
    12     cin >> RR >> CC;
    13     for (int i = 0; i<RR; i++)        //输入数据
    14     {
    15         vector<int> myvector;
    16         for (int j = 0; j<CC; j++)
    17         {
    18             int tempVal = 0;
    19             cin >> tempVal;
    20             myvector.push_back(tempVal);
    21         }
    22         vet.push_back(myvector);
    23     }
    24     cout << vet.size() << endl;    //计算行数
    25     cout << vet[0].size() << endl; //计算列数
    26     sort(vet.begin(), vet.end());  //仅对第一列进行排序
    27     for (int i = 0; i < RR; i++)
    28     {
    29 
    30         for (int j = 0; j < CC; j++)
    31         {
    32             cout << vet[i][j] << " ";
    33         }
    34         cout << endl;
    35     }
    36     sort(vet.begin(), vet.end(),cmp);// 利用cmp可以根据其他列排序。
    37     for (int i = 0; i < RR; i++)
    38     {
    39 
    40         for (int j = 0; j < CC; j++)
    41         {
    42             cout << vet[i][j] << " ";
    43         }
    44         cout << endl;
    45     }
    46     system("pause");
    47     return 0;
    48 }

     方法二:插入的不同方法

    #include<iostream>
    #include<vector>
    #include<set>
    #include<algorithm>
    using namespace std;
    int main()
    {
    	int r = 2;
    	int c = 3;
    	vector<vector<int>> vet(r,vector<int>(c));
    	
    	
    	for (int n = 0; n<r*c; n++) 
    	{
    		vet[n / c][n%c] = 45;
    	}
    	for (int n = 0; n<r*c; n++)
    	{
    		cout << vet[n / c][n%c] << endl;
    	}
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    unix中的rm,rmdir的使用
    jQuery的学习笔记4
    jQuery的学习笔记2
    outlook 2016 for windows 每次刷新发送接收邮件会弹出登陆界面
    Azure SQL Data Warehouse
    Hadoop---Google MapReduce(转)
    Java 1.8特性
    SQL——Mysql数据库介绍
    接口和简单工厂设计模式
    自定义异常
  • 原文地址:https://www.cnblogs.com/wujufengyun/p/6760664.html
Copyright © 2011-2022 走看看