C++输入输出注意事项(创建并输出 一维数组、二维矩阵)

一维数组

(1)如果题目没有指定要输入几个数,那么就需要手动控制结束

例子:输入一行数,存入数组,并打印输出。

	//没有指定要输入几个数,需手动控制
	int n;
	vector<int> res;
	while (cin >> n) {
		res.push_back(n);
		if (cin.get() == '\n') break;//点回车结束
	}
	for (auto i : res) {
		cout << i << " " ;
	}
	cout << endl;
	cout << "-------1----------" << endl;

在这里插入图片描述

如果测试多组数据,加while死循环

	//没有指定要输入几个数,需手动控制
	while (1) {
		int n;
		vector<int> res;
		while (cin >> n) {
			res.push_back(n);
			if (cin.get() == '\n') break;//点回车结束
		}
		for (auto i : res) {
			cout << i << " ";
		}
		cout << endl;
		cout << "-------1----------" << endl;
	}

在这里插入图片描述
————————————————————————————————————

(2)如果题目指定了要输入几个数

例子:输入一行数,存入数组,并打印输出。

	//题目已经指定要输入几个数
	vector<int> res1;
	int a;
	cin >> a;
	while (a--) {
		int x;
		cin >> x;
		res1.push_back(x);
	}
	for (auto i : res1) {
		cout << i << " ";
	}
	cout << endl;
	cout << "----------2-------" << endl;

在这里插入图片描述

如果要测试多组数据,加while

	int a;
	while (cin >> a) {
		vector<int> res1;
		while (a--) {
			int x;
			cin >> x;
			res1.push_back(x);
		}
		for (auto i : res1) {
			cout << i << " ";
		}
		cout << endl;
		cout << "----------2-------" << endl;
	}
或者,加个while(1)死循环
	//题目已经指定要输入几个数
	while (1) {
		int a;
		cin >> a;
		vector<int> res1;
		while (a--) {
			int x;
			cin >> x;
			res1.push_back(x);
		}
		for (auto i : res1) {
			cout << i << " ";
		}
		cout << endl;
		cout << "----------2-------" << endl;
	}

在这里插入图片描述
————————————————————————————————————

(3)指定输入几个数的情况,还可以用for循环存入数组。

例子:输入一行数,存入数组,并打印输出。

**注意for里面要&**
	int m;
	cin >> m;
	vector<int> res2(m);
	for (auto &i : res2) {
		cin >> i;
	}
	for (auto i : res2) {
		cout << i << " ";
	}
	cout << endl;
	cout << "------3-----------" << endl;

在这里插入图片描述

如果要测试多组数据,加while
	int m;
	while (cin >> m) {
		vector<int> res2(m);
		for (auto &i : res2) {
			cin >> i;
		}
		for (auto i : res2) {
			cout << i << " ";

		}
		cout << endl;
		cout << "------3-----------" << endl;
	}

在这里插入图片描述
——————————————————————————————————————————————————————————————————

二维矩阵

(1)普通输入

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//输出函数
void print(vector<vector<int>> arr, int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			//printf("%d ", arr[i][j]);
			cout << arr[i][j]<<" ";
		}
		printf("\n");
	}
}

int main()
{
	//输入操作
	int k;
	vector<vector<int>> matrix;
	int N, M; //分别代表行和列
	while (cin >> N >> M) //多组测试用例
	{
		matrix = vector<vector<int>>(N, vector<int>(M, 0));
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				cin >> k;
				matrix[i][j] = k;
			}
		}
		cout << "------输入完成----------" << endl;

		print(matrix, N, M);
	}
	system("pause");
	return 0;
}

在这里插入图片描述

(感觉比较好)范围for输入,注意取地址符&

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

void print(vector<vector<int>> arr, int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			//printf("%d ", arr[i][j]);
			cout << arr[i][j]<<" ";
		}
		printf("\n");
	}
}

int main()
{
	vector<vector<int>> matrix;
	int N, M; //分别代表行和列
	while (cin >> N >> M) //多组测试用例
	{
		matrix = vector<vector<int>>(N, vector<int>(M, 0));
		for (auto &i : matrix)
			for (auto &j : i)
				cin >> j;      //从开始到这儿是手动输入一个二维矩阵
		//调用输出函数
		print(matrix, N, M);
	}

	system("pause");
	return 0;
}
  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值