<我在哪里转换?>警告C4244:'initializing':从'__int64'转换为'int',可能会丢失数据 [英] <where do I convert?> warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
问题描述
以下代码在第174行给出了以下警告:
The code below gives me following warning in line 174:
警告C4244:'initializing':从'__int64'转换为'int',可能丢失数据
warning C4244: 'initializing' : conversion from '__int64' to 'int', possible loss of data
但我真的不明白为什么。我有 s 的
__ int64
的,但是大小是整数, k
也是整数,所以一切都应该很好。或者 k:s
与我想象的不同?
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?
#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;
}
推荐答案
for(int k:s)
遍历 s
的所有元素,以及 s
__ int64
因此 k
的类型需要 __ int64
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
请参阅: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
这篇关于<我在哪里转换?>警告C4244:'initializing':从'__int64'转换为'int',可能会丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!