Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.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 - Fatal编程技术网

C++ 在双数组中查找第一个数字

C++ 在双数组中查找第一个数字,c++,arrays,C++,Arrays,我有这个阵列: 000000000 000000000 00-2000000 000000000 000000000 000000000 000000000 00000-2000 000000000 我想做的是从左到右,在每一行中找到 '-2'. 我不想再印其他的了 我试过这样做,但效果不好 while ((g[p1][p2] != -2) && (p1 < 9) && (p2 < 9)) { if (p2 = 9) p1++; el

我有这个阵列:

000000000
000000000
00-2000000
000000000
000000000
000000000
000000000
00000-2000
000000000
我想做的是从左到右,在每一行中找到 '-2'. 我不想再印其他的了

我试过这样做,但效果不好

while ((g[p1][p2] != -2) && (p1 < 9) && (p2 < 9))
{
    if (p2 = 9) p1++;
    else p2++;
}
while((g[p1][p2]!=-2)&(p1<9)&(p2<9))
{
如果(p2=9)p1++;
else-p2++;
}
这是一个9x9数组,我正在搜索其中的第一个-2

while ((g[p1][p2] != -2) && (p1 < 9) && (p2 < 9))
{
    if (p2 == 8) {
        p1++;
        p2=0;
    }
    else p2++;
}
while((g[p1][p2]!=-2)&(p1<9)&(p2<9))
{
如果(p2==8){
p1++;
p2=0;
}
else-p2++;
}

p2的最后一个位置应该是8,如果它以0开头。

以下是正确的方法:

int p1, p2; //must be declared outside loop, otherwise they get deleted when it ends.
for(p1 = 0; p1 < 9; p1++)
    for(p2 = 0; p2 < 9; p2++)
        if(g[p1][p2] == -2) goto doneSearching;
doneSearching:

<>既然你贴了C++,为什么不做一些类似的事情:< /P>
#include <iostream>
#include <string>
#include <vector>

int main(int argc, const char * argv[])
{
    std::vector<std::string> rowStr;

    // Import your data however you need to, below is just for example
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("00-2000000");
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("00000-2000");
    rowStr.push_back("000000000");

    std::string subString("-2");

    for (size_t r = 0; r < rowStr.size(); r++) {
        std::string binaryStr = rowStr.at(r);
        if (binaryStr.find(subString) != std::string::npos) {
            size_t c = binaryStr.find(subString);
            std::cout << "row = " << r << "; col = " << c << std::endl;
        }
    }

    return 0;
}
while((g[p1][p2]!=-2)&(p1<9)&(p2<9))
{
如果(p2==8){
p1++;
}
else-p2++;
}
首先,您的代码是错误的,因为您使用的是
=
运算符。=运算符用于为变量赋值,在本例中应使用的运算符为
==
,表示同情


也要检查到8,因为C++中的数组从0开始。

到目前为止你尝试了什么?所以,你想要第一个实例,以行的主要顺序搜索吗?是的,是第一个例子。你必须显示你尝试过的最小可编译的解决方案。我做了,但是很难。我希望它在找到元素时停止,直到数组结束时才循环。它仍然没有找到第一个数字。@GeorgeIrimiciuc更新了它,p2必须在8结束,我猜,当它到达末尾时,你也必须重置它。如果我这样做会怎么样。。int gasitnr=0;对于(int aa=0;aa)不要使用
@georgeirimicuc No,那会更糟。如果它的意思是相同的,为什么我要这样做?但基本上我要做的是循环所有的内容,但将位置保存在新变量中。
#include <iostream>
#include <string>
#include <vector>

int main(int argc, const char * argv[])
{
    std::vector<std::string> rowStr;

    // Import your data however you need to, below is just for example
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("00-2000000");
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("000000000");
    rowStr.push_back("00000-2000");
    rowStr.push_back("000000000");

    std::string subString("-2");

    for (size_t r = 0; r < rowStr.size(); r++) {
        std::string binaryStr = rowStr.at(r);
        if (binaryStr.find(subString) != std::string::npos) {
            size_t c = binaryStr.find(subString);
            std::cout << "row = " << r << "; col = " << c << std::endl;
        }
    }

    return 0;
}
row = 2; col = 2
row = 6; col = 5
while ((g[p1][p2] != -2) && (p1 < 9) && (p2 < 9))
{
    if (p2 == 8){
       p1++;
    }
    else p2++;
}