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. 一个完成的spring xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. Windows下C语言调用dll动态链接库

    dll是windows下的动态链接库文件,下面记录一下在windows下如何调用C语言开发的dll动态链接库. 1.dll动态链接库的源代码 hello_dll.c #include "st ...

  3. BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树

    4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...

  4. Oracle sql语句中(+)作用

    select * from operator_info o, group_info g  where o.group_id = g.group_id(+); 理解:    + 表示补充,即哪个表有加号 ...

  5. 华为S5300系列交换机V100R005SPH020升级补丁

    S23_33_53-V100R005SPH020.pat 附件: 链接:https://pan.baidu.com/s/1-qgNEtRsZbNnC4eK4DTctA  密码:wpn3

  6. chrome --headless --disable-gpu --dump-dom http://www.python.org

    Driving Headless Chrome with Python:Python chrome --headless --disable-gpu --dump-dom http://www.pyt ...

  7. panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

    go的结构体中私有的属性, 即使反射也获取不到

  8. 关于OPC Client 编写

    昨天又有人问我 OPC Client 编写,实际是他们不了解OPC 客户端的工作原理,要想写客户端程序,必须知道OPC对象, OPC逻辑对象模型包括3类对象:OPC server对象.OPC grou ...

  9. 十大开源ERP点评 献给深水区的中小企业和CIO们

    原文地址:http://www.oschina.net/news/58437/top-10-erp-software 如今,企业资源规划(ERP)和客户关系管理(CRM)系统的必要性已经被各种组织和企 ...

  10. 配置quartz数据源的三种方式

    如果是使用了JDBC JobStore或JobStoreCMT获得持久的Job时,就要配置相关的数据源了. 方式一:使用quartz.properties文件,这时只需要在property文件中增加如 ...