Search code examples
c++integersizeunsignedsigned

warning: comparison between signed and unsigned integer expressions..how to solve it?


i am getting a comparison between signed and unsigned integer expression in my code:

    vector<long int> row;
    long n,m;
    long int pro=1;
    cin>>n;
    for(long i=0;i<n;i++)
    {
        long int temp;
        for(long j=0;j<n;j++)
        {
            cin >> temp;
            row.push_back(temp);
        }
    }

    cin >> m;
    for(long i=0;i<row.size();i++)
        pro = pro * pow(row[i],m);

    long int mod = 1000000007;
    cout<< (long int)pro%mod;

At the line: for(long i=0;i<row.size();i++)

How can I fix this warning?


Solution

  • std::vector::size returns a value of size_type, which is Unsigned integral type (usually std::size_t).

    Your loop count variable is of type long which is a signed type. So in the loop condition you are comparing a signed and an unsigned type.

    The solution is simple: Use std::vector<long int>::size_type (or maybe even size_t) instead of long.