实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)
stl算法中有个copy函数。我们能够轻松的写出这种代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double darray[10]={1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9};
vector<double> vdouble(10);
vector<double>::iterator outputIterator=vdouble.begin();
copy(darray,darray+10,outputIterator);
while(outputIterator!=vdouble.end())
{
cout<<*outputIterator<<endl;
outputIterator++;
}
getchar();
return 0;
}
于是你想使用copy来set to vector,于是你这样写道:
#include<iostream>
#include<vector>
#include<set>
int main()
{
std::set <double> input;
input.insert(5);
input.insert(6);
std::vector <double> output;
std::copy(input.begin(), input.end(), output.begin());
return 0;
}
编译通过,执行的时候出现错误。
why?
方法一
假设你在定义output的时候,指定其大小,错误就会消失:
#include<iostream>
#include<vector>
#include<set>
int main()
{
std::set <double> input;
input.insert(5);
input.insert(6);
std::vector <double> output(2);//指明大小
std::copy(input.begin(), input.end(), output.begin());
std::cout << output.size() << std::endl;
return 0;
}
方法二:使用back_inserter
back_inserter 是iterator适配器,它使得元素被插入到作为实參的某种容器的尾部
#include<iostream>
#include<vector>
#include<set>
#include<iterator>
int main()
{
std::set <double> input;
input.insert(5);
input.insert(6);
std::vector <double> output;
std::copy(input.begin(), input.end(), std::back_inserter(output));
std::cout << output.size() << std::endl;
return 0;
}
再继续vetor to vector:
#include<iostream>
#include<vector>
#include<set>
#include<iterator>
int main()
{
std::vector<std::string> v, orig;
orig.push_back("first");
orig.push_back("second");
v = orig;
v.insert(v.end(), v.begin(), v.end());
// Now v contains: { "first", "second", "", "" }
v = orig;
std::copy(v.begin(), v.end(), std::back_inserter(v));
v = orig;
v.reserve(v.size() * 2);
v.insert(v.end(), v.begin(), v.end());
// Now v contains: { "first", "second", "first", "second" }
v = orig;
v.reserve(v.size() * 2);
std::copy(v.begin(), v.end(), std::back_inserter(v));
// Now v contains: { "first", "second", "first", "second" }
// GOOD (best):
v = orig;
v.insert(v.end(), orig.begin(), orig.end()); // note: we use different vectors here
// Now v contains: { "first", "second", "first", "second" }
return 0;
}
再看这个:假设不resize,就会崩溃,把resize换成reserve也会崩溃,和解?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int myints[] = { 10, 20, 30, 40, 50, 60, 70 };
vector<int> myvector;
vector<int>::iterator it;
//myvector.resize(7); // 为容器myvector分配空间 ,不resize会崩溃
copy(myints, myints + 7, myvector.begin());
cout << "myvector contains: ";
for (it = myvector.begin(); it != myvector.end(); ++it)
{
cout << " " << *it;
}
cout << endl;
return 0;
}
================================================================
vector有reserve何resize。二者何为不同?
vector 的reserve增加了vector的capacity,可是它的size没有改变!而resize改变了vector的capacity同一时候也增加了它的size。
原因例如以下:
reserve是容器预留空间,但在空间内不真正创建元素对象,所以在没有增加新的对象之前,不能引用容器内的元素。
增加新的元素时,要调用push_back()/insert()函数。
resize是改变容器的大小。且在创建对象,因此,调用这个函数之后。就能够引用容器内的对象了,因此当增加新的元素时。用operator[]操作符,或者用迭代器来引用元素对象。此时再调用push_back()函数,是加在这个新的空间后面的。
两个函数的參数形式也有差别的,reserve函数之后一个參数。即须要预留的容器的空间;resize函数能够有两个參数,第一个參数是容器新的大小, 第二个參数是要增加容器中的新元素,假设这个參数被省略,那么就调用元素对象的默认构造函数。
看看实例:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> vector_for_reserve;
vector<int> vector_for_resize;
vector_for_reserve.reserve(4);
vector_for_resize.resize(4);
//size:0 capacity:4
cout << vector_for_reserve.size() << " " << vector_for_reserve.capacity() << endl;
//size :4 capacity:4
cout << vector_for_resize.size() << " " << vector_for_resize.capacity() << endl;
vector_for_reserve[0] = 1;//错误
vector_for_resize[0] = 1;
return 0;
}
实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)的更多相关文章
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ...
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
- 实战c++中的string系列--不要使用memset初始化string(一定别这么干)
參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ...
- 实战c++中的string系列--指定浮点数有效数字并转为string
上一篇博客讲了好几种方法进行number到string的转换,这里再单独说一下float或是double到string的转换. 还是处于控件显示的原因.比方说要显示文件的大小,我们从server能够获 ...
- 实战c++中的string系列--CDuiString和string的转换(duilib中的cduistring)
使用所duilib的人定会知道cduistring类型,先看看这个类是怎么定义的: class UILIB_API CDuiString { public: enum { MAX_LOCAL_STRI ...
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)
之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...
随机推荐
- 从终端运行python程序
终端窗口运行.py程序 首先你要安装python,命令行输入 python 有python提示符 >>> 出现说明安装成功 程序第一行应该是 #! python3 #! python ...
- CSU 1374 Restore Calculation 数位DP
题意: 给你三个数A, B, C(没有前导0),但是其中某些位不知道. 问A+B=C成立有多少种情况. 思路: 从最后一位往前推,枚举A, B的每一种情况,考虑进位和不进位两种情况. 代码: #inc ...
- Spring Cloud学习笔记【十】配置中心(消息驱动刷新配置)
上一篇中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用 Webhook 的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案 ...
- ArcGIS api for javascript——显示多个查询结果
描述 本例展示了在重叠的多边形处理查询的一种方式.单击一个石油和天然气的字段来查看地图上的高亮显示.如果仅仅点击一个要素,能够在单击一次来查看包含一些属性的InfoWindow.如果偶然单击到重叠的要 ...
- awk技巧
1通过awk脚本执行awk程序:awk-f program_file_name input_files #!/bin/awk -f BEGIN { print "What is your n ...
- apiCloud如何打开新页面的同时,关掉当前页
方法很多,只要不同时open.close都可行.给你一个简单的方式: api.addEventListener({ name:'viewdisappear' },function(){ api.clo ...
- thinkphp5项目--企业单车网站(五)
thinkphp5项目--企业单车网站(五) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...
- Ubuntu16.04安装官方Firefox 火狐浏览器 延长支持版(Extended Support Release, 简称“ESR”)
Ubuntu16.04安装官方Firefox 火狐浏览器 延长支持版(Extended Support Release, 简称“ESR”) 延长支持版本(Extended Support Releas ...
- vue-cli · Failed to download repo vuejs-templates/webpack-simple: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:8086 && vue init webpack-simple xxx
vue init webpack-simple mywork报错如下: vue-cli · Failed to download repo vuejs-templates/webpack-simple ...
- VUE里子组件获取父组件动态变化的值
在VUE里父组件给子组件间使用props方式传递数据,但是希望父组件的一个状态值改变然后子组件也能监听到这个数据的改变来更新子组件的状态. 场景:子组件通过props获取父组件传过来的数据,子组件存在 ...