实战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中的元素还会怎样? 看看之前写过的程序: ...
随机推荐
- 今日SGU 6.6
sgu 177 题意:给你一个一开始全是白色的正方形,边长为n,然后问你经过几次染色之后,最后的矩形里面 还剩多少个白色的块 收获:矩形切割,我们可以这么做,离散处理,对于每次染黑的操作,看看后面有没 ...
- 题解 P3374 【【模板】树状数组 1】
恩,这是AC的第一道树状数组呢. 本蒟蒻以前遇到RMQ问题一般都用线段树或ST表,可惜ST表不支持在线修改,而线段树代码量又太大. 如今终于找到了折中方案:树状数组!!!!代码量小,还支持修改! 树状 ...
- 【SRM 717 div2 B】LexmaxReplace
Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...
- 火狐不支持innerText属性,只支持innerHTML属性
做的一个js的小程序放到火狐上用不了了,原因是innerText不是标准属性,换成innerHTML属性就好,但是可能需要把html标签给去掉
- MethodFilterInterceptor(方法拦截器)配置excludeMethors
由于该类有setExcludeMethods方法,因此在xml中可以配置一个excludeMethods参数 刚开始老是拦截不成功,tomcat显示这个参数没找到,后来终于找到错误:不应该在拦截器栈中 ...
- ArcGIS api for javascript——明确的创建图层列表
描述 本例展示了如何确切地创建一个地图服务里的图层列表.这个列表由HTML checkboxe组成,可用用于开关图层的可见性. 函数updateLayerVisibility()包含开关图层的逻辑.函 ...
- KM HDU 3718
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- JavaScript语言基础3
JavaScript能够处理一些来自于现实世界的数据类型.比如:数字和文本. 同一时候JavaScript中也包括了一些具 有抽象性质的数据类型.比如对象数据类型. JavaScript它是一种弱类 ...
- android YUV Sensor配置Camera应用的flash auto菜单
请在Config.ftbl.flashlight.h (mediatek\custom\common\hal\flashlight\src)中. 将全部的两处凝视掉的code: //CameraPar ...
- centos7 zabbix3.4.6显示中文乱码问题
工具 : winscp (Linux Windows 传输文件工具) 当部署完zabbix然后显示中文会出现如下图 然后此时先去windows的文字目录如 C:\Windows\Fonts 随便托个字 ...