输入输出与结构体
循环 逗号隔开字符串


#include <vector>#include <string>#include <iostream>#include <algorithm>using namespace std;vector<string> my_split(string s, char p){vector<string> res;int i = 0;for(int i = 0; i < s.size(); i ++){string t = "";while(i < s.size() && s[i] != p){t.push_back(s[i]);i ++;}res.push_back(t);}return res;}int main(){vector<string> tmp;string s;while(cin >> s){tmp = my_split(s, ',');sort(tmp.begin(), tmp.end());for(int i = 0; i < tmp.size() - 1; i ++)cout << tmp[i] << ",";cout << tmp[tmp.size() - 1] << endl;tmp.clear();}}
循环 无空格隔开数字
输入:
- 第一行包含一个数t,表示共有t个案例
- 以下重复t次:
a. 第一行包含空格分隔的两个数M和N,代表矩阵高M、宽N
b. 后续M行,每一行都包含一个长度为N,由0/1组成的数组
2
3 4
1011
0000
0010
1 2
01
输出:t个m*n矩阵
1011
0000
0010
01
int t;
cin >> t;
while(t --)
{
//输入
int m, n;
cin >> m >> n;
int a[m][n];
string row;
//每一行用字符串读入,若用int读入,由于没有分隔符,会将一整行读入为一个数,如1011
for(int i = 0; i < m; i ++)
{
cin >> row;
for(int j = 0; j < n; j ++)
{
a[i][j] = row[j] - '0';
}
}
//输出
for(int i = 0; i < m; i ++)
{
for(int j = 0; j < n; j ++)
{
cout << a[i][j];
}
cout << endl;
}
}
数组
//输入数组
vector<int> nums1;
int num;
while(cin>>num)
{
nums1.push_back(num);
if(cin.get() == '\n') break;
}
//输出二维数组[k][2]
vector<vector<int>> res = ksmallproduct(nums1, nums2, k);
for(int i = 0; i < k; i ++)
{
cout << '['<<res[i][0] <<','<< res[i][1]<< ']'<<endl;
}
//输入vector<string>
string s;
while(getline(cin, s))
{
str.push_back(s);
if(s.size() == 0) break;
}
链表
//单链表结构体
struct ListNode{
int val;
ListNode* next;
ListNode():val(0),next(nullptr){}
ListNode(int x):val(x),next(nullptr){}
ListNode(int x, ListNode* next):val(x), next(next){}
};
//输入单链表
int num;
cin >> num;
ListNode* head = new ListNode(num);
ListNode* cur = head;
while(cin >> num)
{
cur->next = new ListNode(num);
cur = cur->next;
if(cin.get() == '\n') break;
}
//输出单链表 双向链表
while(head)
{
cout << head->val << " ";
head = head->next;
}
cout << endl;
树
//层序数组构建二叉树
// https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/zha-zha-fa-xian-de-wen-ti-xi-wang-geng-duo-ren-kan/
TreeNode* deserialize(string data)
{
if(data.size() == 0) return NULL;
int len = data.size();
int i = 0;
vector<TreeNode*> vec;
while(i < len)
{
string str = "";
while(i < len && data[i] != ',')
{
str.push_back(data[i]);
i ++;
}
if(str == "null")
{
TreeNode* temp = NULL;
vec.push_back(temp);
}
else
{
int temp = stoi(str);
TreeNode* cur = new TreeNode(temp);
vec.push_back(cur);
}
i ++;
}
int j = 1;
for(int i = 0; j < vec.size(); i ++)
{
if(vec[i] == NULL) continue;
if(j < vec.size()) vec[i] ->left = vec[j++];
if(j < vec.size()) vec[i] ->right = vec[j++];
}
return vec[0];
}
//输入string转为树
TreeNode *t;
string data;
getline(cin, data);
t = deserialize(data);
补充
int溢出
有符号数 int 4字节 -2,147,483,648 ~ 2,147,483,647
举例:
1)
10110000110101011100010000000101表示负数
2)4bit
无符号 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
有符号 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1循环
对应数如(11,-5)对2^4=16取模相等
long long VS int
int 4字节 -2,147,483,648 ~ 2,147,483,647
超过20亿就用 long long(8字节)
C++ 如何从string中删除一个字符
string::substr(pos, count)
C++ 中的 Lambda 表达式
C++字母大小写转换方法
#include <iostream>
using namespace std;
int main()
{
char a[20];
int i = 0;
cout<<"请输入一串字符:\n";
cin>>a;
for(;a[i];i++)
{
if(a[i] >= 'a'&&a[i] <= 'z')
a[i] -= 32;
else if(a[i] >= 'A'&&a[i] <= 'Z')
a[i] += 32;
}
for(i = 0;a[i];i++)
cout<<a[i];
cout<<endl;
system("pause");
return 0;
}
ASCII码

