实战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中的元素还会怎样? 看看之前写过的程序: ...
随机推荐
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- HTML学习----------DAY1 第二节
使用 Notepad 或 TextEdit 来编写 HTML 可以使用专业的 HTML 编辑器来编辑 HTML: Adobe Dreamweaver Microsoft Expression Web ...
- strchr函数的实现而不是使用
刚刚在写一个程序的时候突然须要用到定位到一个字符串中第一次出现某个字符的位置,于是就找到了strchr()函数,之前从没实用过的,^_^当然我能够直接调用就可以.可是拥有良好程序素质的洗衣袋决定要想实 ...
- ECharts简单入门
图1和图2是手机上显示的效果, 图3是电脑浏览器显示的效果. 如何使用ECharts? 1.下载echarts.js 2.引入echarts.js <script type="text ...
- 基于SIFT的点云关键点提取
这篇博文主要介绍SIFT算法在提取点云图像关键点时的具体用法. 尺度不变特征转换(Scale-invariant feature transform,SIFT)是David Lowe在1999年发表, ...
- Java读取txt文件和覆盖写入txt文件和追加写入txt
//创建文件 public static void createFile(File filename) { try { if(!filename.exists()) { filename.create ...
- IPv4私有IP地址有哪些!
私有IP地址是一段保留的IP地址.只是使用在局域网中,在Internet上是不使用的. 私有IP地址的范围有: 私网地址分有三类, A类中,第一段为10的都为私网地址,B类中,以172.16--172 ...
- Vue数据驱动表单渲染,轻松搞定form表单
form-create 具有动态渲染.数据收集.校验和提交功能的表单生成器,支持双向数据绑定.事件扩展以及自定义组件,可快速生成包含有省市区三级联动.时间选择.日期选择等17种功能组件. Github ...
- 【Linux下禁用rm命令之建立回收站】
第一步 创建回收站目录 # 根据自己的习惯,找个位置创建一个用作回收文件的目录 # 我们这里将在root目录下面创建一个名为".trash"的隐藏文件 [root@fedora ~ ...
- 小米开源便签Notes-源码研究(1)-导出功能整体思路
NotesListActivity是入口Activity. 响应菜单事件,我的手机是"左键菜单".如果菜单项的ID是"R.id.menu_export_text" ...