我正试图解决这个problem。我写了这个代码:不能在我的代码中找到错误:C++
#include <iostream>
#include <vector>
using namespace std;
int findIndex(x, lastAns, N) {
return ((x == !lastAns) % N);
}
void query_1(int x, int y, int N, int lastAns, std::vector< std::vector<int> >& v) {
v[findIndex(x, lastAns, N)].push_back(y);
}
void query_2(int x, int y, int N, int* lastAns, std::vector< std::vector<int> >& v) {
*lastAns = y % (v[findIndex(x, *lastAns, N)].size());
cout << *lastAns << endl;
}
int main(int argc, char const *argv[])
{
int N, Q;
cin >> N >> Q;
std::vector< std::vector<int> > v;
std::vector<int> buff;
int queryType;
int lastAns = 0;
int x, y;
for(int i=0; i<N; ++i) {
for(int j=0; j<N-1; ++i) {
buff.push_back(0);
}
v.push_back(buff);
}
for(int i=0; i<Q; ++i) {
cin >> queryType;
cin >> x >> y;
if(queryType == 1) {
query_1(x, y, N, lastAns, v);
}
else if (queryType == 2) {
query_2(x, y, N, &lastAns, v);
}
else continue;
}
return 0;
}
我得到的错误是:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
我还没有遇到过这种错误。当我通过gdb运行代码时,得到:
Program received signal SIGABRT, Aborted.
0x00007ffff74ab418 in __GI_raise ([email protected]=6)
at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
我的代码中存在什么问题?
有人会建议你迟早使用debbuger ... – xinaiz
有没有特别的原因使用丑陋的宏而不是普通或模板函数? – Slava
没有理由。我只是想一个足够简单的计算,一个宏就足够了。 –