我正在尝试读取输入的第二行和第三行,其中这两行的第一个数字是数组的长度(该行中的数字数目(不包括空格和第一个数字))。我想将数组的长度存储在一个单独的数组中,并将这些行上的其余数字打印到STDOUT。我目前有以下代码,但收到与stoi函数有关的错误。
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
int main() {
int numarr;
int numqueries;
string line;
char chars;
scanf("%d %d",&numarr,&numqueries);
vector<string> lengths = vector<string>(numarr);
vector<int> arrays = vector<int>(numarr);
vector<int> queries = vector<int>(numqueries);
int lq = 0;
while(getline(cin,line) && lq <= numarr){
stringstream iss(line);
string wd;
string dd;
int cnt = 1;
while(cnt == 1 || (iss >> wd && cnt < stoi(lengths[cnt-1]))){
if(cnt == 1){
lengths[cnt-1] = wd;
cout << "first";
cnt++;
}else{
cout << wd;
cnt++;
}
}
lq += 1;
}
return 0;
}
错误输出:
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
Reading symbols from Solution...done.
[New LWP 499362]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGABRT, Aborted.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
To enable execution of this file add
add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
info "(gdb)Auto-loading safe path"
示例输入:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3