C++ vector的访问(resize,pu_back与下标访问的区别)
来源:互联网 发布:滴滴企业版软件下载 编辑:程序博客网 时间:2024/05/23 10:30
在编写代码时发现vector的一个现象
(1)
std::vector<std::string> str_vector;
str_vector.resize(3);
str_vector.push_back("name_1");
str_vector.push_back("name_2");
然后进行访问逐一打印发现:
for (std::vector<std::string>::iterator iter = str_vector.begin(); iter != str_vector.end(); ++iter)
{
std::cout<<*iter<<", ";
}
其值为:"","","","name_1","name_2",
此时发现resize是开辟了三个string空间,而后push_back是接着前面开辟的空间而往里push.
(2)
std::vector<std::string> str_vector;
str_vector.push_back("name_1");
str_vector.push_back("name_2");
其值为:"name_1","name_2",
(3)
std::vector<std::string> str_vector;
str_vector[0] = "name_1"; //wrong,错误,vector大小未知,且没有元素.
// 下标只能用于获取已存在的元素
总结:
(1) 若想对vector进行下面访问,则必须空间已开辟,可以用:
std::vector<std::string> str_vector;
str_vector.resize(3);
str_vector[0] = "name_1";
str_vector[1] = "name_2";
str_vector[2] = "name_3";
也可用:
std::vector<std::string> str_vector;
str_vector.push_back("name_1");
str_vector.push_back("name_2");
for(int i=0; i< str_vector.size(); i++)
{
std::cout<<str_vector[i]<<std::endl;
}
(1)
std::vector<std::string> str_vector;
str_vector.resize(3);
str_vector.push_back("name_1");
str_vector.push_back("name_2");
然后进行访问逐一打印发现:
for (std::vector<std::string>::iterator iter = str_vector.begin(); iter != str_vector.end(); ++iter)
{
std::cout<<*iter<<", ";
}
其值为:"","","","name_1","name_2",
此时发现resize是开辟了三个string空间,而后push_back是接着前面开辟的空间而往里push.
(2)
std::vector<std::string> str_vector;
str_vector.push_back("name_1");
str_vector.push_back("name_2");
其值为:"name_1","name_2",
(3)
std::vector<std::string> str_vector;
str_vector[0] = "name_1"; //wrong,错误,vector大小未知,且没有元素.
// 下标只能用于获取已存在的元素
总结:
(1) 若想对vector进行下面访问,则必须空间已开辟,可以用:
std::vector<std::string> str_vector;
str_vector.resize(3);
str_vector[0] = "name_1";
str_vector[1] = "name_2";
str_vector[2] = "name_3";
也可用:
std::vector<std::string> str_vector;
str_vector.push_back("name_1");
str_vector.push_back("name_2");
for(int i=0; i< str_vector.size(); i++)
{
std::cout<<str_vector[i]<<std::endl;
}
(2) 请注意resize()与push_back的同时使用,其空间是resize的空间+push_back的空间,否则达不到预设目标.
欢迎大家批评,指正,交流!
联系方式:
emai: tongzhuodenilove@163.com 0 0
- C++ vector的访问(resize,pu_back与下标访问的区别)
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- vector.resize 与 vector.reserve的区别
- Vector用数组下标访问的条件
- audio to text
- 转载Xen Network各种架构
- ExtJs中getCmp、getDom、Get的区别
- 虚函数和纯虚函数及虚函数表
- Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法
- C++ vector的访问(resize,pu_back与下标访问的区别)
- office的vb6ext.olb加载不了怎么办
- Android注意问题
- zepto tap事件的一个bug
- 设计模式(九)之责任链模式
- Java 阿冬的swing学习之路----阿冬专栏!!!!
- Java_本地方法_Java程序中调用c函数
- spring学习笔记(11)——使用FactoryBean创建bean
- 腾讯2014校园招聘笔试题