警告C4244:“初始化”:从“__int64”转换为“int”,可能丢失数据。

时间:2022-12-27 16:10:32

The code below gives me following warning in line 174:

下面的代码在第174行警告我:

warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data

警告C4244:“初始化”:从“__int64”转换为“int”,可能丢失数据。

but I don't really understand why. I have vector s of __int64's, but i'ts size is integer, k is also integer, so everything should be fine. Or maybe k:s works different than I thought?

但我真的不明白为什么。我有__int64的向量,但我的大小是整数,k也是整数,所以一切都应该没问题。或者k:s和我想的不同?

#define _USE_MATH_DEFINES

#include <iostream>
#include <vector>
#include <cmath>


using namespace std;

__int64 count( vector<__int64> s, int n )
{
    vector<__int64> table(n+1,0);   // or: 0LL instead 0
    table[0] = 1;                   // or: 1LL instead 1
    for ( int k : s )    /* 174 */  // or:  for ( auto& k : s )`
        for(int j=k; j<=n; ++j)
            table[j] += table[j-k];
     return table[n];
}


int main()
{
// some code here
    cin.get();
    return 0;
}

2 个解决方案

#1


2  

for ( int k : s ) iterates over all the elements of s, and the template type of s is __int64 so the type of k needs to be __int64

for (int k: s)遍历s的所有元素,而s的模板类型是__int64,所以k的类型需要是__int64。

See: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html

参见:http://www.cprogramming.com/c + + 11 / c++ 11-ranged-for-loop.html

#2


2  

change for ( int k : s ) to for ( auto& k : s ), everything will be ok.

改变(int k: s)到for (auto&k: s),一切都会好起来的。

#1


2  

for ( int k : s ) iterates over all the elements of s, and the template type of s is __int64 so the type of k needs to be __int64

for (int k: s)遍历s的所有元素,而s的模板类型是__int64,所以k的类型需要是__int64。

See: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html

参见:http://www.cprogramming.com/c + + 11 / c++ 11-ranged-for-loop.html

#2


2  

change for ( int k : s ) to for ( auto& k : s ), everything will be ok.

改变(int k: s)到for (auto&k: s),一切都会好起来的。