输入2

作者: lxr_ | 来源:发表于2022-09-10 10:53 被阅读0次

/*
有些输入可能是,输入一个矩阵:
[ [3,2,3],
[1,6,5],
[7,8,9] ]
对于这种没有给定矩阵行列数的输入,而且还包含中括号和逗号的输入,我们也是只能按照字符串拆分来进行。
*/

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

#define _CRT_SECURE_NO_WARNINGS

using namespace std;

int main(int argc, char** argv)
{
    vector<vector<int>> arr;

    string input;
    char* tok;
    while (getline(cin, input))     //读取一行到input中
    {
        if (!input.empty())
        {
            vector<int> a;

            int num;
            tok = strtok((char*)input.c_str(), " ,[]");     //使用“空格 逗号 中括号”作为分隔符分割字符串
            while (tok)
            {
                num = atoi(tok);                            //stoi也可以,但是会做范围检查,不能超过int范围
                a.push_back(num);
                tok = strtok(NULL, " ,[]");
            }
            arr.push_back(a);
        }

    }

    return 0;
}

相关文章

网友评论

      本文标题:输入2

      本文链接:https://www.haomeiwen.com/subject/xaltortx.html