#include <iostream>
#include <vector>
#include <memory.h>
#include <stdio.h>
int main() {
int size_v = 10;
std::vector<int> tmp(5, 0);
int * tmp_pt = tmp.data();
memset(tmp_pt, 0, 5 * 4);
std::vector<int> myvector(5);
int* p = myvector.data();
std::vector<bool> b_vec(5,true);
for(auto t : b_vec) {
std::cout<<std::boolalpha <<t<<std::endl;
}
for (auto b : b_vec) {
b = false;
}
for (auto const & b : b_vec)
{
std::cout << std::boolalpha << b << std::endl;
}
std::vector<int> int_vec(5,0);
for(auto & t : int_vec) {
std::cout<<t<<' ';
}
std::cout<<std::endl;
for (auto &t : int_vec) {
t = 1;
}
for (auto const & t : int_vec) {
std::cout<< t<<' ';
}
std::cout<< std::endl;
return 0;
}
如代码所示,非引用的遍历bool vector也会改变其值,在使用auto的情况下,必须显式的进行类型转换,使用bool来避免.