打印矢量元素:当printf工作时,cout不起作用

时间:2017-08-29 13:42:03

标签: c++ c++11 printf stdvector cout

我正在尝试使用这个简单的例子来访问向量的元素:

#include <iostream>
#include <vector>

typedef std::vector<uint8_t> uint8_vec_t;

int main(void) {
    uint8_vec_t v = {1, 2, 3, 4};
    uint8_vec_t::iterator itr;
    std::cout << "v.size() = " << v.size() << std::endl;
    int i = 0;
    for ( itr = v.begin(); itr != v.end(); ++itr, i++) {
        std::cout << "std::cout: v[" << i << "] = " << v[i] << std::endl;
        std::cout << "std::cout: *itr =" << *itr << std::endl;
        printf("printf: v[%d] = %d\n", i, v[i]);
        printf("printf: *itr = %d\n", *itr);
    }
    //
    return 0;
}

我得到以下输出:

v.size() = 4
std::cout: v[0] = 
std::cout: *itr =
printf: v[0] = 1
printf: *itr = 1
std::cout: v[1] = 
std::cout: *itr =
printf: v[1] = 2
printf: *itr = 2
std::cout: v[2] = 
std::cout: *itr =
printf: v[2] = 3
printf: *itr = 3
std::cout: v[3] = 
std::cout: *itr =
printf: v[3] = 4
printf: *itr = 4

我向coutprintf提供相同的值,但只有printf将值打印到终端。 在使用cout

时我没有问题,为什么printf无法打印值?

感谢您的帮助。

0 个答案:

没有答案