为什么存储在整数中的向量值会产生不同的结果?

时间:2016-05-26 18:04:15

标签: c++11 vector

我最近开始在C ++中使用向量。我尝试编写的程序的目的是确定第一个缺失的正数。给定输入数组A : [ -9, 12, -1, 0, 1 ],预期输出为2。我把它编码了 -

int Solution::firstMissingPositive(vector<int> &A)
{
    std::sort(A.begin(),A.end());    //sorting vector

    bool Negative=false;
    int flag;
    int i=0;

    for(i=0;i<A.size();++i)
    {
        if(A[i]<=0)
        {
            Negative=true;
            std::cout<<"\nwe found a -ve number or 0";
        }
        else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
        {
            Negative=false;
            std::cout<<"\ncomparing @ ="<<i<<" which = "<<A[i];
            std::cout<<"\ncomparing @ ="<<i+1<<" which = "<<A[i+1];

            flag=A[i]+1; //The faulty statement

            std::cout<<"\n\n missing number(A[i]+1) @ ="<<A[i]+1;
            std::cout<<"\n\n missing number(flag) @ ="<<flag;
            break;
        }

    }
//do something more
}

使用此输出 -

-9 -1 0 1 12 
we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing @ =3 which = 1
comparing @ =4 which = 12

missing number(A[i]+1) @ =2

missing number(flag) @ =20

我发现这很有趣,因为对我来说,看起来我不能使用整数来存储向量的值。

  • 尝试调试它我发现将标记分配更改为flag = A[i]+2会使结果打印30
  • 我已经阅读了有关SO的其他问题,建议使用vector.at(i)代替[]运算符作为更好的做法。更改此内容并不反映我的代码有任何更改。
  • flag更改为vector<int>会给我一个肮脏的错误,我不确定。

A[i]+1语法上不等于整数值吗?如果是,为什么我不能存储它?

1 个答案:

答案 0 :(得分:3)

将代码缩减为MCVE会很快证明问题出在输出格式上,因此将“\ n”放在语句的开头是一种不常见的做法,正是出于这个原因。你没有看到20,你看到的是旗帜中的“2”,其后是其他地方的零。

#include <algorithm>
#include <iostream>
#include <vector>

void solve(std::vector<int>& A)
{
    std::sort(A.begin(),A.end());    //sorting vector

    bool Negative=false;
    int flag;
    int i=0;

    for(i=0;i<A.size();++i)
    {
        if(A[i]<=0)
        {
            Negative=true;
            std::cout<<"we found a -ve number or 0\n";
        }
        else if( (A[i]>0) && (A[i+1]!=A[i]+1) )
        {
            Negative=false;
            std::cout<<"comparing @ ="<<i<<" which = "<<A[i]<<'\n';
            std::cout<<"comparing @ ="<<i+1<<" which = "<<A[i+1]<<'\n';

            flag=A[i]+1; //The faulty statement

            std::cout<<"missing number(A[i]+1) @ ="<<A[i]+1<<'\n';
            std::cout<<"missing number(flag) @ ="<<flag<<'\n';
            break;
        }

    }
//do something more
}

int main() {
    std::vector<int> A { -9, 12, -1, 0, 1 };
    solve(A);
    std::cout << "and we're done\n";
}

输出(见http://ideone.com/zb9fNX

we found a -ve number or 0
we found a -ve number or 0
we found a -ve number or 0
comparing @ =3 which = 1
comparing @ =4 which = 12
missing number(A[i]+1) @ =2
missing number(flag) @ =2
and we're done

我还应该指出,如果它试图读取数组中的最后一个元素,那么对“A [i + 1]”的测试将导致越界数组访问。你应该改变

for(i=0;i<A.size();++i)

for(i=0;i<A.size() - 1;++i)

或更常见,

for(i=1;i<A.size();++i)

并使用“A [i-1]”代替“A [i + 1]”来获取之前的值。

例如:

#include <algorithm>
#include <iostream>
#include <vector>

void solve(std::vector<int>& A)
{
    std::sort(A.begin(),A.end());    //sorting vector

    int expected = 0;
    int missing = 0;

    for(int i = 1; i < A.size(); ++i)
    {
        if (A[i] <= 0) {
            std::cout << "we found " << A[i] << "\n";
            continue;
        }

        if (A[i-1] <= 0) {
            expected = 1;
        } else {
            expected = A[i-1] + 1;
        }
        if (A[i] == expected)
            continue;

        std::cout << "A[" << i-1 << "] = " << A[i-1] << '\n';
        std::cout << "expecting " << expected << '\n';
        std::cout << "A[" << i << "] = " << A[i] << '\n';

        missing = expected;         
        std::cout << "missing number: " << expected << '\n';

        break;
    }
//do something more
}

int main() {
    std::vector<int> A { -9, 12, -1, 0, 1 };
    solve(A);
    std::cout << "and we're done\n";
}

http://ideone.com/AIw4oU