VectorTest.cpp

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include "VectorTest.h" using namespace std; void VectorTest::simpleOperation()
{
// create empty vector for strings
vector<string> sentence; // reserve memory for five elements to avoid reallocation
sentence.reserve(); // append some elements
sentence.push_back("Hello,");
sentence.insert(sentence.end(), { "how", "are", "you", "?" }); // print elements separated with spaces
copy(sentence.cbegin(), sentence.cend(),
ostream_iterator<string>(cout, " "));
cout << endl; // print "technical data"
cout << " max_size(): " << sentence.max_size() << endl;
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl; // swap second and fourth element
swap(sentence[], sentence[]); // insert element "always" before element "?"
sentence.insert(find(sentence.begin(), sentence.end(), "?"),
"always"); // assign "!" to the last element
sentence.back() = "!"; // print elements separated with spaces
copy(sentence.cbegin(), sentence.cend(),
ostream_iterator<string>(cout, " "));
cout << endl; // print some "technical data" again
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl; // delete last two elements
sentence.pop_back();
sentence.pop_back();
// shrink capacity (since C++11)
sentence.shrink_to_fit(); // print some "technical data" again
cout << " size(): " << sentence.size() << endl;
cout << " capacity(): " << sentence.capacity() << endl;
} void VectorTest::run()
{
printStart("simpleOperation()");
simpleOperation();
printEnd("simpleOperation()");
}

运行结果:

---------------- simpleOperation(): Run Start ----------------
Hello, how are you ?
max_size(): 153391689
size(): 5
capacity(): 5
Hello, you are how always !
size(): 6
capacity(): 7
size(): 4
capacity(): 4
---------------- simpleOperation(): Run End ----------------

STL - 容器 - vector简单应用的更多相关文章

  1. C++线性序列容器<vector>简单总结

    C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...

  2. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  3. [C++]STL容器Vector的内存释放

    直接抛出两句话,说明到底应该如何释放Vector占用的内存. “vector的clear不影响capacity,你应该swap一个空的vector.” <Effective STL>中的“ ...

  4. STL容器vector应用注意事项

    [1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...

  5. STL容器 vector,list,deque 性能比较

    C++的STL模板库中提供了3种容器类:vector,list,deque对于这三种容器,在觉得好用的同时,经常会让我们困惑应该选择哪一种来实现我们的逻辑.在少量数据操作的程序中随便哪一种用起来感觉差 ...

  6. STL容器 -- Vector

    核心:Vector 是 STL 里的一个向量容器,可以像数组那样进行随机访问,能在尾部插入元素,对于元素的删除和插入可以动态管理内存. 头文件: #include <vector> 构造函 ...

  7. ACM常用STL容器

    // STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...

  8. 带你深入理解STL之Vector容器

    C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...

  9. STL之vector容器详解

    vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...

随机推荐

  1. java多线程技术之(callable和future)

    接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果. Callable接口类似于Runnable,从名字就可以看出来了,但是Runnab ...

  2. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  3. ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

    ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  4. 你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)

    什么让你对C/C++如此恐惧? 本篇将继续上一篇来讨论段错误(Segmentation fault). 上一篇: 你的C/C++程序为什么无法运行?揭秘Segmentation fault(1) 追溯 ...

  5. git push时提示"fatal: The current branch master has no..."

    git push到远程仓库时提示:fatal: The current branch master2 has no upstream branch. To push the current branc ...

  6. hadoop 2.7.1 高可用安装部署

    hadoop集群规划 目标:创建2个NameNode,做高可用,一个NameNode挂掉,另一个能够启动:一个运行Yarn,3台DataNode,3台Zookeeper集群,做高可用. 在hadoop ...

  7. Oracle手工生成段建议(Segment Advisor)

    一.描写叙述 从oracle 10g開始,oracle引入了段顾问(Segment Advisor),用于检查数据库中是否有与存储空间相关的建议,而且从10gR2開始,oracle自己主动调度并执行一 ...

  8. Appium+python自动化5-Appium Inspector

    前言    appium Inspector从入门到放弃!反正你都打开了,那就看下为什么要放弃吧! Appium Inspector是appium自带的一个元素定位工具,上一篇介绍了如何使用uiaut ...

  9. 硬件负载均衡F5和软负责均衡Nginx

    请直接搜索相关文章了解:http://www.ideadata.com.cn/wisdomAction/readWisdom.do?id=75   F5,硬件 优点:能够直接通过智能交换机实现,处理能 ...

  10. spark读取 kafka nginx网站日志消息 并写入HDFS中(转)

    原文链接:spark读取 kafka nginx网站日志消息 并写入HDFS中 spark 版本为1.0 kafka 版本为0.8 首先来看看kafka的架构图 详细了解请参考官方 我这边有三台机器用 ...