/*
* vectorDemo.cpp
*
* Created on: Jul 17, 2014
* Author: lichfeng
*/
#include<vector>
#include <iostream.h>
#include <algorithm>
#include"vectorDemo.H" using namespace std; void vectorDemo() {
/**************************************************************************/
/* constructors functions */
/**************************************************************************/
vector<int> first; // empty vector of ints
vector<int> second(4, 100); // four ints with value 100
vector<int> third(second.begin(), second.end()); // iterating through second
vector<int> fourth(third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = { 16, 2, 77, 29, 8, 3, 24 };
vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int)); cout << "The contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n'; /**************************************************************************/
/* iterator erase (iterator position); */
/* iterator erase (iterator first, iterator last); */
/* erase(): 在vector中移除一个元素(通过position),或者移除一个区间([first, last))内的元素*/
/* 该函数返回值是所删除的元素的下一个元素。 */
/* Note: 由于vector是以数组形式组织的,所以删除中间元素将导致vector中其他元素向前移动。这对vector */
/* 而言是低效的操作,应减少使用。但此操作对list和deque等链表组织的容器很高效. */
/**************************************************************************/
//vector<int>::iterator ii;
fifth.erase(fifth.begin() + 1);
cout << "After erase by position, the contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n'; fifth.erase(fifth.begin(), fifth.begin() + 2);
cout << "After erase by range, the contents of fifth are:";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n'; /***************************************************************************/
/* capacity(): 返回当前vector已分配的存储空间大小(按照所存元素类型),该值大于等于size() */
/* size(): 返回当前vector所实际存储的元素数目 */
/* max_size(): 该vector可以分配的最大内存空间(操作系统限制的) */
/* push_back():在vector末尾增加一个元素,将导致size数加1。当且仅当size数大于当前容量时, */
/* 引起vector内存重新分配(按内存非配器算法增加vector容量 */
/* pop_back(): 移除vector的最后一个元素,引起size减少1 */
/* begin(): 返回指向vector第一个元素的iterator */
/* end(): 返回指向vector最后一个元素的下一个元素的iterator, 不能取值。 */
/* cbegin(): 返回指向vector第一个元素的const iterator,不允许修改vector数据 */
/* cend(): 返回指向vector最后一个元素的下一个元素的const iterator, 不能取值。 */
/* at(): 返回at(position)中position的引用. */
/* front(): 返回vector第一個元素的引用. */
/* back(): 返回vector中最后一个元素的引用. */
/* clear(): 移除vector中所有的元素,将导致vector size为0.(capacity非强制变化). */
/**************************************************************************/
vector<int> vIntTest;
cout << "0:The size is " << vIntTest.size() << endl;
cout << "0:The capacity is " << vIntTest.capacity() << endl;
cout << "0:The max capacity is " << vIntTest.max_size() << endl; for (int i = 0; i < 10; i++)
vIntTest.push_back(i);
cout << "1:The size is " << vIntTest.size() << endl;
cout << "1:The capacity is " << vIntTest.capacity() << endl; vIntTest.pop_back();
cout << "2:The size is " << vIntTest.size() << endl;
cout << "2:The capacity is " << vIntTest.capacity() << endl; vIntTest.insert(vIntTest.end(), 10, 100);
cout << "3:The size is " << vIntTest.size() << endl;
cout << "3:The capacity is " << vIntTest.capacity() << endl; vIntTest.at(0) = 100;
vIntTest.at(1) = 200;
cout << "After set by at():";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n'; vIntTest.back() = 1000;
cout << "After set by back():";
for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
cout << '\n';
/***************************************************************************/
/* rbegin(): 返回指向vector最后一个元素的迭代器,对翻转迭代器加操作将导致其向前移动. */
/* empty(): 判斷vector是否為空并返回,空返回true,非空返回false */
/* resize(): 調整容器的size以包含n個元素,移除多餘的元素或增大內存空間 */
/* reserve(): 要求vector capacity至少包含n個元素,如果n大于當前capacity,vector容量將增加 */
/***************************************************************************/
vector<int> reserveVec(5);
vector<int>::reverse_iterator ri; int i=0;
for(ri=reserveVec.rbegin(); ri!=reserveVec.rend(); ri++)
{
*ri = ++i;
} cout<<"reserveVec contains: ";
for(vector<int>::iterator it= reserveVec.begin(); it != reserveVec.end(); it++)
cout<<"*it ";
cout<<endl; /***************************************************************************/
/* data(): 返回vector首元素的内部C类型指针,通过该指针完成所有的操作. */
/* empty(): 判斷vector是否為空并返回,空返回true,非空返回false */
/* resize(): 調整容器的size以包含n個元素,移除多餘的元素或增大內存空間 */
/* reserve(): 要求vector capacity至少包含n個元素,如果n大于當前capacity,vector容量將增加 */
/***************************************************************************/ //data()
/*vector<int> myvector(5); int* p = myvector.data(); *p = 10;
++p;
*p = 20;
p[2] = 100; cout << "myvector contains:";
for (unsigned i = 0; i < myvector.size(); ++i)
cout << ' ' << myvector[i];
cout << '\n';*/ // reserve()
vector<int>::size_type sz; vector<int> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i = 0; i < 100; ++i) {
foo.push_back(i);
if (sz != foo.capacity()) {
sz = foo.capacity();
cout << "capacity changed: " << sz << '\n';
}
} vector<int> bar;
sz = bar.capacity();
bar.reserve(100); // this is the only difference with foo above
cout << "making bar grow:\n";
for (int i = 0; i < 100; ++i) {
bar.push_back(i);
if (sz != bar.capacity()) {
sz = bar.capacity();
cout << "capacity changed: " << sz << '\n';
}
} //resize
vector<int> myvector1; // set some initial content:
for (int i = 1; i < 10; i++)
myvector1.push_back(i); myvector1.resize(5);
myvector1.resize(8, 100);
myvector1.resize(12); cout << "myvector contains:";
for (int i = 0; i < myvector1.size(); i++)
cout << ' ' << myvector1[i];
cout << '\n';
//myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0 }

最新文章

  1. 匹夫细说C#:可以为null的值类型,详解可空值类型
  2. C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍
  3. iOS平台UDID方案比较
  4. java数组对象的浅层复制与深层复制
  5. Struts2常量的具体用法实例
  6. java中创建线程的几种方法及区别
  7. android学习日记05--Activity间的跳转Intent实现
  8. java含多个包的命令行下执行
  9. 64位Win2008_VS2012使用ODP.NET遭遇问题和解决办法
  10. Linux下Nginx+PHP 简单安装配置
  11. 利用Gson进行String和对象的转换
  12. javamail发送邮件(转)
  13. HDU 2546 饭卡 01背包变形
  14. 手机端 H5上传头像
  15. 手机app抓包
  16. 微信小程序中如何使用WebSocket实现长连接(含完整源码)
  17. [福大软工] Z班 团队作业——随堂小测(同学录) 作业成绩
  18. 关于git分支管理,推送拉取等等
  19. BZOJ 3097: Hash Killer I
  20. 如何快速的得到string的最后一个字符

热门文章

  1. [hdu7000]二分
  2. [bzoj1189]紧急疏散
  3. 使用 kubeadm 部署
  4. Java-ASM框架学习-从零构建类的字节码
  5. C/C++ Qt 自定义Dialog对话框组件应用
  6. 9.2 k8s结合Jenkins与gitlab实现代码升级与回滚
  7. Codeforces 997D - Cycles in product(换根 dp)
  8. Mysql优化,ICP、BNL算法、BKA算法、MMR算法
  9. Excel-返回列表或数据库中的分类汇总(汇总可以实现要还是不要统计隐藏行功能) subtotal()
  10. gcc 的编译流程 和gdb的调试方法