文章目录
1 数字
一行数字之间空格隔开
cin 输入会阻止在空格处,下次循环读入
cin.sync(); 可以用来清空缓冲区
void t_01(){
/*
* Start 输入
* 11 33 55 回车
* End 打印结果
* */
vector<int> ans;
int num;
while(cin >> num){
ans.push_back(num);
if(cin.get() == '\n') break;
}
for(auto e: ans) cout << e << "->";
}
两行数字之间空格隔开
void t_01(){
/*
* Start 输入
* 11 33 55 回车
* 7 回车
* End 打印结果
* */
vector<vector<int>> ans;
vector<int> single;
int num;
while(cin >> num){
single.push_back(num);
// 第一行退出
if(cin.get() == '\n'){
ans.push_back(move(single));
break;
}
}
while(cin >> num){
single.push_back(num);
// 第一行退出
if(cin.get() == '\n'){
ans.push_back(move(single));
break;
}
}
for(auto vec: ans){
for(auto e:vec){
cout << e << " ";
}
cout << endl;
}
}
多行最后以回车空行结束
void vStrToVec2i(const vector<string> &vecS, vector<vector<int>> &vec2i){
for(auto str: vecS){
// 11 22 33
int posStart = 0;
bool isEnd = false;
vector<int> veci;
while(true){
string subStr;
int nextSpace = str.find(" ", posStart);
// [2] exit
if(isEnd) break;
// [1] first to end
if(nextSpace == -1){
isEnd = true;
}
if(nextSpace == -1){
subStr = str.substr(posStart); // get str_end num
}else{
subStr = str.substr(posStart, nextSpace-posStart); // get str num
}
veci.push_back(stoi(subStr)); // conver to num and push
posStart = nextSpace + 1;
}
vec2i.push_back(veci);
}
}
void t_05(){
/*
* Start 输入
* 11 33 55 回车
* 22 44 66 回车
* 7 回车
* 回车
*
* End 打印结果 ans = {{11, 33, 55}, {22, 44, 66}, {7}}
* */
string inputStr;
vector<string> vecS;
vector<vector<int>> vec2i;
while (true){
// [1] 获取一行
getline(cin, inputStr);
// 回车退出
if(inputStr.empty()) break;
// [2] Push
vecS.push_back(move(inputStr));
}
// [3] vecS -> vec2i
vStrToVec2i(vecS, vec2i);
for(auto row: vec2i){
for(auto e: row) { cout << e << " "; }
cout << endl;
}
}
2 字符串
一行字符串之间空格隔开(获取一行)
一行字符串之间空格隔开(获取一个一个)
void t_02(){
/*
* input : hello xhh mcy
* 打印全部 获取 {hello, xhh, mcy}
* 打印每个字符串段
* */
string inputStr;
vector<string> ans;
while (cin >> inputStr){
ans.push_back(move(inputStr));
// 回车结束
if(cin.get() == '\n') break;
}
for(const auto &e: ans){
cout << e << "-";
}
}
子串 substr和find
void t_02(){
/*
* input : hello xhh mcy
* 打印全部 获取 获取子串"xhh"
* */
string str = "hello xhh mcy xhh";
// [1] idx从6开始得到3个字符的子串
string getXhh = str.substr(6, 3);
// [2] idx从6开始到最后的字串
string getXhhToEnd = str.substr(6);
cout << "getXhh.size(): " << getXhh.size() << "\tgetXhh: " << getXhh << endl;
cout << "getXhhToEnd.size(): " << getXhhToEnd.size() << "\tgetXhhToEnd: " << getXhhToEnd << endl;
// [3] 查找字符串"xhh"起始位的idx,失败返回-1
// str.rfind("h") 倒着寻找
int xhh_pos = str.find("xhh");
cout << "xhh_pos: " << xhh_pos << endl;
int p2 = str.find("xhh", xhh_pos+1); // 查找下一个"xhh"
// [4] 从pos开始,n个字符,替换为str
str.replace(xhh_pos, 3, "zph"); // hello zph mcy
str.replace(xhh_pos, 3, "zphzphzph"); // hello zphzphzph mcy
cout << "alter replace: " << str << endl;
}
str 的erase功能
void t_03(){
string str = "xhh_abc";
cout << "len: " << str.size() << " str: " << str << endl;
// [1] 删除 _a
str.erase(3, 2);
cout << "len: " << str.size() << " str: " << str << endl;
// [2] b到最后
str.erase(3);
cout << "len: " << str.size() << " str: " << str << endl;
}