我正在尝试使用这个简单的例子来访问向量的元素:
#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
我向cout
和printf
提供相同的值,但只有printf
将值打印到终端。
在使用cout
printf
无法打印值?
感谢您的帮助。