1

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?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
swarnava112
  • 421
  • 8
  • 19
  • 1
    In case one does *not* feel like counting. `pro = pro * pow(row[i],m);` is row 27. Please mark problematic lines with comments denoting line number, such as `// line 27 HERE`. Thanks. – WhozCraig Aug 08 '15 at 04:21
  • 4
    I'm betting that in **the real code**, `for(long i=0;i – Drew Dormann Aug 08 '15 at 04:21
  • @DrewDormann I'd back you on that bet, not that you need it. – WhozCraig Aug 08 '15 at 04:23
  • 1
    Why are you using _both_ `long` and `long int`? They're the same type. – Emil Laine Aug 08 '15 at 09:41
  • Possible duplicate of [C: how can I fix warnings like: "comparison between signed and unsigned"](http://stackoverflow.com/questions/859943/c-how-can-i-fix-warnings-like-comparison-between-signed-and-unsigned) – phuclv Mar 24 '17 at 10:52
  • did you ever copy the warning and google it? results will come immediately and there are tons of duplicates on SO – phuclv Mar 24 '17 at 10:54

3 Answers3

4

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Or `decltype(row)::size_type` so you don't have to change it if the element type of `row` happened to change. – Emil Laine Aug 09 '15 at 09:23
2

vector::size returns a size_type which is an unsigned integral value.

You can fix this one of two ways:

  1. Use an unsigned iterator in your for-loop: for(auto i = 0U; i < row.size(); ++i)
  2. Cast the return of vector::size to a signed integer: for(auto i = 0; i < static_cast<int>(row.size()); ++i)
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

C++ has a thing called the range-based for loop, which relieves you from the burden of dealing with index variables. It also solves your mismatched signedness problem:

for(long r : row)
    pro = pro * pow(r,m);

Just use it.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157