实战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中的元素还会怎样? 看看之前写过的程序: ...
随机推荐
- HDU 4939 Stupid Tower Defense 简单DP
题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...
- TortoiseGit 弹出 git@xxx.com's password 对话框
安装完 tortoise git,用它克隆项目的时候,一直弹出git@xxx.com's password 对话框 解决的办法是,将ssh客户端默认的路径,换为git 安装目录下ssh.exe的路径就 ...
- POJ 1258 Agri-Net (最小生成树+Prim)
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39820 Accepted: 16192 Descri ...
- Windows安装两个mysql数据库步骤
因为新旧项目数据库版本号差距太大.编码格式不同.引擎也不同,所以仅仅好装两个数据库. 本次安装两个mysql数据库.版本号各自是4.0.18,5.5.36.都是可运行文件直接安装. 本机上之前已经安装 ...
- centos7 安装好python3 yum报错
解决方法: 修改两个地方 vi /usr/bin/yum 将最前面的改为#! /usr/bin/python2 vi /usr/libexec/urlgrabber-ext-down #! /usr/ ...
- Android App中使用Gallery制作幻灯片播放效果
http://www.jb51.net/article/83313.htm 我们有时候在iPhone手机上或者Windows上面看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的图片滚动效果 ...
- IntelliJ IDEA springmvc demo
construction pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- lsblk---列出所有可用块设备的信息,
lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...
- gcc---C/C++ 编译器
gcc命令使用GNU推出的基于C/C++的编译器,是开放源代码领域应用最广泛的编译器,具有功能强大,编译代码支持性能优化等特点.现在很多程序员都应用GCC,怎样才能更好的应用GCC.目前,GCC可以用 ...
- 【CS Round #37 (Div. 2 only) A】Boring Number
[Link]:https://csacademy.com/contest/round-37/task/boring-number/ [Description] 让你找离平均数最近的一个数的下标; [S ...