C++输入输出-OJ

一、一维二维数组

1 输入n,m申请二维动态数组

数组形式:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int **arr = new int*[n];
    for (int i = 0; i< n; i++)
    {
        arr[i] = new int[m];
        for (int j = 0; j < m; j++)
        {
             cin >> arr[i][j];
        }
    }
    for (int i = 0; i< n; i++)
    {
        for (int j = 0; j < m; j++)
        {
             cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

vector形式:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> arr;
    for (int i = 0; i < n; i++)
    {
        vector<int> cur;
        for (int j = 0; j < m; j++)
        {
            int cur_val;
            cin >> cur_val;
            cur.push_back(cur_val);
        }
        arr.push_back(cur);
    }
    for (int i = 0; i< n; i++)
    {
        for (int j = 0; j < m; j++)
        {
             cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

2 打印二维数组

    for (int i = 0; i< n; i++)
    {
        for (int j = 0; j < m; j++)
        {
             cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    //STL
    for (const auto i : arr)
    {
		for (auto j : i)
		{
			cout << j << " ";
		}
		cout << endl;
	}

3 一维数组n个元素

vector:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n ;
    vector<int> arr;
    for (int i = 0; i < n; i++)
    {

        int cur_val;
        cin >> cur_val;
        arr.push_back(cur_val);
    }
    for (const auto i : arr)
    {
        cout << i << " ";
    }
    return 0;
}

动态数组版:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n ;
    int *arr = new int[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}

4 打印一维数组

	//stl和数组均可
    for (int i = 0; i< n; i++)
    {
        cout << arr[i] << " ";
    }
    //STL才行
    for (const auto i : arr)
    {
        cout << i << " ";
    }

5 以逗号相隔的数组,以回车为结束符号(不定长度)

在这里插入图片描述

输入1,2,6,7,4
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int word = 0;
    vector<int> money;
	while (cin >> word)
	{
		money.push_back(word);
		if (cin.get() == '\n')
			break;
	}
    for (int i = 0; i < money.size(); i++)
    {
        cout << money[i] << " ";
    }
    return 0;
}

6 不定长度的输入数组,回车结束

输入12345,输出12345
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int word = 0;
    vector<int> money;
	while (cin >> word)
	{
		money.push_back(word);
		if (cin.get() == '\n')
			break;
	}
    for (int i = 0; i < money.size(); i++)
    {
        cout << money[i] << " ";
    }
    return 0;
}

二、字符串

1 输入单字符串(遇空格或回车就结束了)

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:

	static int check(string s1)
	{
		//
	}
};
int main()
{
	string input1;
	cin >> input1;
	cout << input1;
	return 0;
}

2 输入回车才结束的单字符串(会读取a b之间的空格)

2.1 单字符,只能输入一个串

输入带空格的字符串,输出正常在这里插入图片描述

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	string input;
	getline(cin, input);
    cout << input;
	return 0;
}

2.2 垃圾方法

只能输入一个,以回车结束!!!,
如果输入多个,就会把下一个的第一个输入吞了!!!!
如:
{【】}
【】
输出
{【】}

以后每个字符串都没有第一个?

把cin.get换成getchar也一样

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
	string input;
	string res;
	vector<string> text;
	int sum = 0;
	while (getline(cin, input))
	{
		text.push_back(input);
		if (cin.get() == '\n')   
		{
			break;
		}
	}
	for (int i = 0; i < text.size(); i++)
	{
		cout << text[i];
	}
	return 0;
}

在这里插入图片描述

3 输入多个字符串,每个串之间以空格作为结束;最终结束是回车

举例:
输入ssws(空格)wwww(空格)aaa(回车)
将三个字符串存入vector< string >
在这里插入图片描述

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	string tempStr;
	vector<string> Array;
	while (cin >> tempStr)
	{
		Array.push_back(tempStr);
		if (getchar() == '\n')  break;  //此处语句只能放在录入之后,否则会少收录一个
	}
	for (int i=0;i<Array.size();i++)
		cout << Array[i] << endl;
	system("pause");
 	return 0;
}

4 输入多个字符串,以回车结尾(目前的唯一方法)

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    vector<vector<string>> input;
    while (getline(cin, s))
    {
      if (s.size() == 0) break;
      stringstream ss(s);
      string cur;
      vector<string> v;
      while (ss >> cur)
      {
        v.push_back(cur);
      }
      input.push_back(v);
      v.clear();
    }
    vector<string> res;
    for (int i = 0; i < input.size(); i++)
    {
      string sss;
      for (int j = 0; j < input[i].size(); j++)
      {
        sss += input[i][j];
      }
      res.push_back(sss);
    }
    for (auto i : res)
    {
      cout << i << endl;
    }
    return 0;
}

5 输入不定矩阵,以回车结尾

vector版:在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    vector<vector<string>> input;
    while (getline(cin, s))
    {
      if (s.size() == 0) break;
      stringstream ss(s);
      string cur;
      vector<string> v;
      while (ss >> cur)
      {
        v.push_back(cur);
      }
      input.push_back(v);
      v.clear();
    }
    vector<vector<int>> res;
    for (int i = 0; i < input.size(); i++)
    {
      vector<int> cur; 
      for (int j = 0; j < input[i].size(); j++)
      {
         cur.push_back(stoi(input[i][j]));
      }
      res.push_back(cur);
    }
    for (const auto i : res)
    {
      for (auto j : i)
        cout << j << " ";
      cout << endl;
    }
    return 0;
}

二维动态数组:输入矩阵,以空格相隔,以回车结束
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    vector<vector<string>> input;
    while (getline(cin, s))
    {
      if (s.size() == 0) break;
      stringstream ss(s);
      string cur;
      vector<string> v;
      while (ss >> cur)
      {
        v.push_back(cur);
      }
      input.push_back(v);
      v.clear();
    }

    int n  = input.size();
    int m = input[0].size();
    int **arr = new int*[n];
    for (int i = 0; i < n; i++)
    {
        arr[i] = new int[m];
        for (int j = 0; j < m; j++)
        {
            arr[i][j] = stoi(input[i][j]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

6 输入指定数量n的string,存入vector < string >(输入可能带有空格需要存储)

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
    cin >> n;
    cin.get();
    vector<string> io;
    for (int i = 0; i < n ; i++)
    {
        string cur;
        getline(cin, cur);
        io.push_back(cur);
    }
    for (auto i : io)
    {
        cout << i << endl;
    }
    return 0;
}

7 输入指定数量n的string,存入vector < string >(输入没有空格)

在这里插入图片描述

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
    cin >> n;
    cin.get();
    vector<string> io;
    for (int i = 0; i < n ; i++)
    {
        string cur;
        cin >> cur;
        io.push_back(cur);
    }
    for (auto i : io)
    {
        cout << i << endl;
    }
    return 0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值