当前位置:  开发笔记 > 编程语言 > 正文

当cout显示正确的大小时,为什么printf会为矢量大小显示0?

如何解决《当cout显示正确的大小时,为什么printf会为矢量大小显示0?》经验,为你挑选了2个好方法。

当我使用printf和%d来获取向量的大小时,我不明白为什么我得到0:

vector sieve;
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);

你的两个格式字符串都是错的:

    你传递answerprintf和格式化%d,但它应该是%lld,因为它的声明unsigned long long.

    你通过大小%d而不是%ld.既然sizeint,它应该是%d.

当这些ARGS得到传递给printf,它的印刷的第32位,answer第一个%d和第二个32位(或更多,过去的结束!)的answer%ld.这不是你想要的.

如果您使用-Wall编译器进行编译,则应警告您这类事情.密切关注警告!

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有