当我使用printf和%d来获取向量的大小时,我不明白为什么我得到0:
vectorsieve; int size; ... //add stuff to vector ... size = sieve.size(); printf("printf sieve size: %d \n", size); //prints "printf sieve size: 0" std::cout << "cout sieve size: "; std::cout << size; std::cout << " \n "; //prints "cout sieve size: 5 (or whatever the correct sieve size is)"
如果我遍历向量通过
if(i=0;i我得到了正确的迭代次数.
我做错了什么或者什么是printf?size()返回一个int对吧?
这是我的整个小脚本:
#include#include #include #include int main (int argc, char * const argv[]) { unsigned long long answer = 0; unsigned long long cur = 2; std::vector sieve; unsigned long long limit; unsigned long long value; unsigned int i; int size; bool isPrime; std::cout << "Provide a value to find its largest prime factor: "; std::cin >> value; limit = ceil(sqrt(value)); sieve.push_back(2); while(cur++ < limit){ isPrime = true; sieve.begin(); for(i=0; i
Juliano.. 6
现在,有了完整的资料来源,很明显.
你宣布:
int size;然后你用过:
std::printf("current last: %d sieve size: %ld\n", answer, size); std::printf("Limit: %d Answer: %d sieve size: %ld\n", limit, answer, size);如果size为int,则应使用"%d",而不是"%ld".一个好的编译器会警告你这件事.GCC为您的原始版本提供以下警告:
test.cpp: In function ‘int main(int, char* const*)’: test.cpp:17: warning: converting to ‘long long unsigned int’ from ‘double’ test.cpp:30: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘int’ test.cpp:36: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%ld’ expects type ‘long int’, but argument 4 has type ‘int’这说明了很多.
您应该将大小声明为:
std::vector::size_type size; 然后你应该用它作为:
std::printf("current last: %llu sieve size: %llu\n", (unsigned long long) answer, (unsigned long long) size); std::printf("Limit: %llu Answer: %llu sieve size: %llu\n", (unsigned long long) limit, (unsigned long long) answer, (unsigned long long) size);当然,使用iostream可以避免这些问题,特别是printf()中的丑陋转换,可以将大小转换为printf已知的类型.
1> Juliano..:现在,有了完整的资料来源,很明显.
你宣布:
int size;然后你用过:
std::printf("current last: %d sieve size: %ld\n", answer, size); std::printf("Limit: %d Answer: %d sieve size: %ld\n", limit, answer, size);如果size为int,则应使用"%d",而不是"%ld".一个好的编译器会警告你这件事.GCC为您的原始版本提供以下警告:
test.cpp: In function ‘int main(int, char* const*)’: test.cpp:17: warning: converting to ‘long long unsigned int’ from ‘double’ test.cpp:30: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘int’ test.cpp:36: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%ld’ expects type ‘long int’, but argument 4 has type ‘int’这说明了很多.
您应该将大小声明为:
std::vector::size_type size; 然后你应该用它作为:
std::printf("current last: %llu sieve size: %llu\n", (unsigned long long) answer, (unsigned long long) size); std::printf("Limit: %llu Answer: %llu sieve size: %llu\n", (unsigned long long) limit, (unsigned long long) answer, (unsigned long long) size);当然,使用iostream可以避免这些问题,特别是printf()中的丑陋转换,可以将大小转换为printf已知的类型.
2> Todd Gamblin..:这搞砸了,因为你有:
unsigned long long answer = 0; int size;你打电话
printf
给:std::printf("current last: %d sieve size: %ld\n", answer, size);你的两个格式字符串都是错的:
你传递
answer
到printf
和格式化%d
,但它应该是%lld
,因为它的声明unsigned long long
.你通过大小
%d
而不是%ld
.既然size
是int
,它应该是%d
.当这些ARGS得到传递给printf,它的印刷的第32位,
answer
第一个%d
和第二个32位(或更多,过去的结束!)的answer
对%ld
.这不是你想要的.如果您使用
-Wall
编译器进行编译,则应警告您这类事情.密切关注警告!