vector使用练习题

题目描述

给定一个二维矩阵,有数个询问,要求输出第 i 行,第 j 列的元素。

输入

第一行两个整数 n,m 表示矩阵大小

接下来 n 行,每行 m 列,描述该矩阵

第 n+1行一个整数 q 表示询问数量

接下来 q 行,每行两个整数 i,j,表示查询矩阵中第 i 行第 j 列的元素是什么

n,m,q ≤ 100000

n⋅m ≤ 100000

矩阵中任一个元素在 int 范围内

输出

q 行,每行一个整数表示矩阵中第 i 行第 j 列的元素是什么

样例输入
2 3
1 2 5
3 6 4
3
1 1
2 1
2 3

样例输出
1
3
4

#include<iostream>
#include<vector>
#define max 100001
using namespace std;

int main()
{
	int m,n;
	vector<vector<int> > array;
	vector<int> v;
	cin>>m>>n;
	int a[max];
	
	int t=0;
    for(int i=0;i<m;i++)
    {
    	v.clear();
    	for(int j=0;j<n;j++)
    	{
    		cin>>t;
    		v.push_back(t);
		}
		array.push_back(v);
	}
	int q,k1,k2,k;
	cin>>q;
	for(k=0;k<q;k++)
	{
		cin>>k1>>k2;
	    a[k]=array[k1-1][k2-1];//注意不是array[k1][k2]
	}
	
		for(int p=0;p<q;p++)
	{
		cout<<a[p];
		cout<<endl;
	}

    return 0;
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C++中,结构体排序练习通常涉及到如何使用标准库中的算法对包含自定义数据类型的结构体数组或容器进排序。这里我们可以举一个简单的例子,假设有一个名为`Student`的结构体,包含`name`和`age`两个成员: ```cpp struct Student { std::string name; int age; }; ``` 你可以用以下几种方法对`Student`结构体数组进排序: 1. **直接排序:**如果年龄是排序的主要依据,你可以定义一个比较函数(`compare`),然后使用`std::sort`函数: ```cpp bool compareStudents(const Student& s1, const Student& s2) { return s1.age < s2.age; } int main() { Student students[] = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}}; std::sort(students, students + sizeof(students) / sizeof(students), compareStudents); // 现在students数组按年龄升序排 } ``` 2. **使用STL算法:**如果你的结构体已经实现了`<`运算符,那么可以直接使用`std::stable_sort`: ```cpp bool studentLess(const Student& s1, const Student& s2) { return s1.age < s2.age; } int main() { std::vector<Student> students = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}}; std::stable_sort(students.begin(), students.end(), studentLess); } ``` 3. **自定义比较器(C++11及以上):**也可以使用lambda表达式来创建一个可传递的比较器: ```cpp int main() { std::vector<Student> students = {{"Alice", 20}, {"Bob", 18}, {"Charlie", 22}}; std::sort(students.begin(), students.end(), [](const Student& s1, const Student& s2) { return s1.age < s2.age; }); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值