技术交流QQ群:1027579432,欢迎你的加入!

  • 从键盘直接输入类似数组[2,7,11,-14]
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;

// 字符串分割函数
void split(const string& s, vector<int>& sv, const char flag = ' ') {
    sv.clear();
    istringstream iss(s);
    string temp;

    while (getline(iss, temp, flag)) {
        sv.push_back(stoi(temp));
    }
    return;
}



int main(){
    int start, end;
    vector<int> vec;
    string s, str;
    getline(cin, s);
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '[') start = s.find_first_of('[');
        if(s[i] == ']') end = s.find_last_of(']');
    }
    for(int i = start + 1; i < end; i++) str += s[i];

    split(str, vec, ',');

    cout << "验证输入\n";
    for(auto x : vec) cout << x << ' ';
    return 0;
}