auto

std::vector是最常用的容器之一,它是一个大小可动态伸缩的线性存储器。std::vector带有堆内存的管理功能,其内部的实现其实是3个指针。当所分配的堆空间不够用时,std::vector会自动新分配更大的空间,将已有数据移动到新空间,并释放原有的空间。


因此,使用std::vector存储大块的数据是允许的。实际数据都会被保存在堆上,栈中只保存了std::vector内部的3个指针而已。



原文:https://www.cnblogs.com/jins-note/p/9513129.html
// 字符串
std::string str = “hello, world”;
for(auto ch : str) {
std::cout << ch << std::endl;
}
// 数组
int arr[] = {1, 2, 3, 4};
for(auto i : arr) {
std::cout<< i << std::endl;
}

// 列表
std::vector<std::string> str_vec = {“i”, “like”, "google”};
for(auto& it : str_vec) {
it = “c++”;
}

// 容器
std::map<int, std::string> hash_map = {{1, “c++”}, {2, “java”}, {3, “python”}};
for(auto it : hash_map) {
std::cout << it.first << “\t” << it.second << std::endl;
}

  

posted on 2022-03-17 21:08  蜀山菜鸟  阅读(8)  评论(0)    收藏  举报