C++ common input and output [machine trial]

0. Commonly used header files

#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include<sstream>
#include <unordered_map>

using namespace std;

1. Reading the string

Enter a string per line

	int n;
	cin >> n;	
	vector<string> A;
	for (int i = 0; i < n; i++)
	{
    
    
		string str;
		cin >> str;
		A.push_back(str);
	}

1.1 String reading, with "," as the interval

Example:
a,c,bb
f,dddd
nowcoder

here use the substr function to split:

string s;
	vector<string> A;
	
	vector<vector<string>> ans;

	while (cin >> s)
	{
    
    
		A.push_back(s);  //  读取一行 字符串
	}
	
	for (int i = 0; i < A.size(); i++)
	{
    
    
		int pos=0;
		vector<string> res;
		while (pos != -1)  //  能找到就一直迭代
		{
    
    
			pos = A[i].find(',');  //  找到“,”
			string temp = A[i].substr(0, pos); 分割 , 前面的字符串
			res.push_back(temp);
			A[i] = A[i].substr(pos + 1);  // A[i] 继续迭代为,后面的字符串
		}
		ans.push_back(res);
	}

2. One-dimensional vector input

The length N of the array is input in the first line, and the value of the vector is input in the second line.

	int n = 0;
	cout << "输入数组的行数:"<<endl;
	cin >> n;
	vector<int> A;
	for (size_t i = 0; i < n; i++)
	{
    
    
		int value;
		cin >> value;
		A.push_back(value);
	}

3. Two-dimensional vector input

The lengths N and M of the input array in the first line, and the elements of each row of the input array

N=2, M=2
1 1  // 代表第一行的元素
2 2  // 代表第二行的元素
	int n = 0,m=0;
	cout << "输入数组的行数和列数:"<<endl;
	cin >> n>>m;
	vector<vector<int>> A;
	vector<int> temp;
	int value;
	
	for (size_t i = 0; i < n; i++)
	{
    
    
		temp.clear(); // 用完之后 记得清空
		for (size_t j = 0; j < m; j++)
		{
    
    
			cin >> value;
			temp.push_back(value);
		}
		A.push_back(temp);
	}

4. Input of two-dimensional vector

Enter a row of data, [[5, 4], [6, 3], [6, 7], [6, 6], [4, 6]] A two-dimensional array of this form

	string str;
	getline(cin, str);
	vector<vector<int>> A;  //存放二维数组
	vector<int> tem;
	int value;
	while (str.size() > 2)
	{
    
    
		int pos = str.find(']');
		string temp = str.substr(2, pos - 2);
		str = str.substr(pos + 2);
		pos = temp.find(',');
		int a = stoi(temp.substr(0, pos));
		temp = temp.substr(pos + 1);
		int b = stoi(temp);
		tem.push_back(a);
		tem.push_back(b);
		A.push_back(tem);
		tem.clear();
	}

4. The two-dimensional vector is input in the form of a string [ , there is no space or space after it]

For example: 1,2,34,12,12
2,1,22,23

vector<string> s; // 存放字符串的vector
int N, k;
cin >> N>>k ;
cin.ignore(); // 请空 cin 缓存区的内容,不然会报错
while (N--)
{
    
    
	string str;
	getline(cin,str);  // 取第一行字符串
	// 如果,后有空格 cin>>str 就失效
	s.push_back(str);
}

vector<vector<int>> res;  
for (int i = 0; i < s.size(); i++)
{
    
    
	int pos = 0;
	vector<int> ans;
	while (pos != -1) {
    
    
		pos = s[i].find(',');
		string temp = s[i].substr(0, pos);  // 刚好取到 , 前一个
		int a = stoi(temp);     // 字符串直接转化为 数字
		ans.push_back(a);
		s[i] = s[i].substr(pos + 1);  // s[i] 迭代到 , 的后一个元素之后
	}
	res.push_back(ans);
	}

5. Dynamic programming creates a two-dimensional array

	 int size = str.size();
    vector<vector<bool>> dp(size,vector<bool>(size));  //  初始化二维数组

6. Create a two-dimensional array

The input data includes multiple groups.
Each set of data is one row, and the first integer in each row is the number n of integers (1 <= n <= 100), and the input ends when n is 0.
The next n positive integers, that is, each positive integer that needs to be summed.
Input:
4 1 2 3 4
5 1 2 3 4 5
0

    int a =0;
    int sum =0;
    while(cin>>a && a)
    {
    
    
        sum =0;
        vector<int> A;
        for(int i=0 ; i<a;i++)
        {
    
    
            int value;
            cin >> value;
		    sum += value;
        }
        cout<<sum<<endl;
    }

6. Enter a two-dimensional array of any row and any column

Input:
1 2 3
4 5
0 0 0 0 0

#include<iostream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
    
    
    vector<vector<int>> A;
    vector<int> tmp;
    int value,sum=0;
    while (cin>>value)
    {
    
    
        tmp.push_back(value);
        sum+=value;
        if (getchar() == '\n')
        {
    
    
            A.push_back(tmp);
            tmp.clear();
            cout<<sum<<endl;
            sum=0;
        }
    }
     
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_41623632/article/details/119767402