在 C++ 中解析输入以进行竞争性编程
parsing input in c++ for competitive programming
如何解析输入,例如:
[[1,3,5,7],[10,11,16,20],[23,30,34,60]]
对于 m x n
大小的二维向量。我试过了
char x;
vector<int> v;
vector<vector<int>> v_v;
vector<int> temp;
int br_op_cl = 0;
int row = 0;
while (cin >> x) {
// cout << x << endl;
if (x == '[' || x == '{') {
// cout << "inside [" << endl;
br_op_cl++;
cout << "inside [ " << br_op_cl << endl;
} else if (x == ']' || x == '}') {
cout << "inside ] " << x << endl;
br_op_cl--;
} else if (x >= 0 && x != ',') {
cout << "inside 0-9 " << x << endl;
temp.push_back(x);
if (br_op_cl % 2 != 0) {
cout << br_op_cl << " inside br_op_cl " << '\n';
v_v.push_back(temp);
}
}
}
输出为
49 51 53 55 49 48 49 49 49 54 50 48 50 51 51 48 51 52 54 48
这是每个数字的ascii值。
关于如何一起读取 chars 和 int 以及 c++ 中的解析技术的任何帮助
将 [1,3,5,7]
视为单行。使用 stringstream
读取此行。然后用另一个stringstream
读取这一行的内容
getline
将读取每一行直到达到 ]
,另一个 getline
将读取每一列直到达到 ]
。
将出现的 {
替换为 [
,以使解析更容易。
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
int main()
{
std::string str = "[[1,3,5,7],[10,11,16,20],[23,30,34,60]]";
replace(str.begin(), str.end(), '{', '[');
replace(str.begin(), str.end(), '}', ']');
std::stringstream ss(str);
std::vector<std::vector<int>> res;
if (ss.get() != '[')
return 0;
char c;
while (ss >> c && c != ']') {
if (c == '[') {
getline(ss, str, ']');
std::stringstream scol(str);
std::vector<int> vec;
while (getline(scol, str, ','))
vec.push_back(std::stoi(str));
res.push_back(vec);
}
}
for (auto& row : res) {
for (auto& col : row) std::cout << col << ",";
std::cout << "\n";
}
return 0;
}
如何解析输入,例如:
[[1,3,5,7],[10,11,16,20],[23,30,34,60]]
对于 m x n
大小的二维向量。我试过了
char x;
vector<int> v;
vector<vector<int>> v_v;
vector<int> temp;
int br_op_cl = 0;
int row = 0;
while (cin >> x) {
// cout << x << endl;
if (x == '[' || x == '{') {
// cout << "inside [" << endl;
br_op_cl++;
cout << "inside [ " << br_op_cl << endl;
} else if (x == ']' || x == '}') {
cout << "inside ] " << x << endl;
br_op_cl--;
} else if (x >= 0 && x != ',') {
cout << "inside 0-9 " << x << endl;
temp.push_back(x);
if (br_op_cl % 2 != 0) {
cout << br_op_cl << " inside br_op_cl " << '\n';
v_v.push_back(temp);
}
}
}
输出为
49 51 53 55 49 48 49 49 49 54 50 48 50 51 51 48 51 52 54 48
这是每个数字的ascii值。 关于如何一起读取 chars 和 int 以及 c++ 中的解析技术的任何帮助
将 [1,3,5,7]
视为单行。使用 stringstream
读取此行。然后用另一个stringstream
读取这一行的内容
getline
将读取每一行直到达到 ]
,另一个 getline
将读取每一列直到达到 ]
。
将出现的 {
替换为 [
,以使解析更容易。
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
int main()
{
std::string str = "[[1,3,5,7],[10,11,16,20],[23,30,34,60]]";
replace(str.begin(), str.end(), '{', '[');
replace(str.begin(), str.end(), '}', ']');
std::stringstream ss(str);
std::vector<std::vector<int>> res;
if (ss.get() != '[')
return 0;
char c;
while (ss >> c && c != ']') {
if (c == '[') {
getline(ss, str, ']');
std::stringstream scol(str);
std::vector<int> vec;
while (getline(scol, str, ','))
vec.push_back(std::stoi(str));
res.push_back(vec);
}
}
for (auto& row : res) {
for (auto& col : row) std::cout << col << ",";
std::cout << "\n";
}
return 0;
}