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

Bjo*_*orn 1 c++ printf vector

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

vector<long long> 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)"
Run Code Online (Sandbox Code Playgroud)

如果我遍历向量通过

if(i=0;i<sieve.size();i++) 
Run Code Online (Sandbox Code Playgroud)

我得到了正确的迭代次数.

我做错了什么或者什么是printf?size()返回一个int对吧?


这是我的整个小脚本:

#include <iostream>
#include <vector>
#include <stack>
#include <math.h>

int main (int argc, char * const argv[]) {
    unsigned long long answer = 0;
    unsigned long long cur = 2;
    std::vector<long long> 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<sieve.size();i++){
        if(!(cur % sieve[i])){
          isPrime = false;
          break;
        }
      }
      if(isPrime){  
        if(!(value % cur)){
          std::printf("Is prime factor: %d\n", cur);
          sieve.push_back(cur);
          answer = sieve[sieve.size() - 1];
          size = sieve.size();
          std::printf("current last: %d sieve size: %ld\n", answer, size);
          for(i=0; i<sieve.size();i++){
            std::printf("sieve iter: %d sieve val: %d\n", i, sieve[i]);
            std::cout << size;
            std::cout << " wtf\n";
          }
        }
      }
    }
    answer = sieve[sieve.size() - 1];
    size = sieve.size();
    std::printf("Limit: %d Answer: %d sieve size: %ld\n", limit, answer, size);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jul*_*ano 6

现在,有了完整的资料来源,很明显.

你宣布:

int size;
Run Code Online (Sandbox Code Playgroud)

然后你用过:

std::printf("current last: %d sieve size: %ld\n", answer, size);
std::printf("Limit: %d Answer: %d sieve size: %ld\n", limit, answer, size);
Run Code Online (Sandbox Code Playgroud)

如果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’
Run Code Online (Sandbox Code Playgroud)

这说明了很多.

您应该将大小声明为:

std::vector<long long>::size_type size;
Run Code Online (Sandbox Code Playgroud)

然后你应该用它作为:

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);
Run Code Online (Sandbox Code Playgroud)

当然,使用iostream可以避免这些问题,特别是printf()中的丑陋转换,可以将大小转换为printf已知的类型.


Tod*_*lin 5

这搞砸了,因为你有:

unsigned long long answer = 0;
int size;
Run Code Online (Sandbox Code Playgroud)

你打电话printf给:

std::printf("current last: %d sieve size: %ld\n", answer, size);
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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