Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 指针到指针二维数组中的迭代_C++_Arrays_Pointers_Dynamic Arrays - Fatal编程技术网

C++ 指针到指针二维数组中的迭代

C++ 指针到指针二维数组中的迭代,c++,arrays,pointers,dynamic-arrays,C++,Arrays,Pointers,Dynamic Arrays,我在Hackerrank上做这个挑战,我已经尝试了所有我能做的,但是没有做对。 挑战详情如下: 我的代码是: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { /* Enter your code here. Read

我在Hackerrank上做这个挑战,我已经尝试了所有我能做的,但是没有做对。 挑战详情如下:

我的代码是:

 #include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int noOfSequence, noOfQuery;
    int lastAnswer = 0, j=0;
    int query, x, y;

    cin>>noOfSequence>>noOfQuery;

    int *seqLength = new int[noOfSequence];
    for(int i=0; i<noOfSequence; i++){
        seqLength[i] = 0;
    }

    int **seqList = new int*[noOfSequence];
    for(int i=0; i<noOfSequence; i++){
        seqList[i] = new int [noOfSequence];
    }

    for(int i=0; i<noOfQuery; i++){
        cin>>query>>x>>y;
        switch(query){
            case 1: {
                        int seqListIndex = (x ^ lastAnswer) % noOfSequence;
                        *(seqList[seqListIndex] ++) = y;
                        seqLength[seqListIndex] += 1;
                        break;
                    }
            case 2: {
                        int seqListIndex = (x ^ lastAnswer) % noOfSequence;
                        lastAnswer = seqList[seqListIndex][y%seqLength[seqListIndex]];
                        cout<<lastAnswer;
                        break;
                    }
            default: cout<<"default"<<endl;
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
/*在此处输入代码。从标准输入读取输入。将输出打印到标准输出*/
int noOfSequence,noOfQuery;
int lastAnswer=0,j=0;
int查询,x,y;
cin>>noOfSequence>>noOfQuery;
int*seqLength=新的int[noOfSequence];
对于(inti=0;i>x>>y;
开关(查询){
案例1:{
int seqListIndex=(x^lastAnswer)%noOfSequence;
*(seqList[seqListIndex]+)=y;
seqLength[seqListIndex]+=1;
打破
}
案例2:{
int seqListIndex=(x^lastAnswer)%noOfSequence;
lastAnswer=seqList[seqListIndex][y%seqLength[seqListIndex]];

cout问题是您将
seqList[seqListIndex]
视为指向“下一个元素”的指针,但
y%seqLength[seqListIndex]
是其原始值的偏移量

“下一个元素”的索引是
seqLength[seqListIndex]
,因此替换

*(seqList[seqListIndex] ++) = y;


(如果使用
std::vector
,代码可能会简化很多)

但是没有正确。你为什么说正确?为什么不使用向量?你知道代码> >代码>是位运算符吗?嗯…自动生成器包括<代码> <代码>,对于你在C++中应该使用什么来做一个不太微妙的提示。此外,这可能是问题的根源,当你做<代码> SEQ时。List[seqListIndex]++
您丢失了原始指针。感谢您提供的解决方案。我理解它为什么有效。但不理解“偏移”的含义。
seqList[seqListIndex][seqLength[seqListIndex]] = y;