STL - 容器 - vector简单应用
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简单应用的更多相关文章
- C++线性序列容器<vector>简单总结
C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...
- 从零开始写STL—容器—vector
从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...
- [C++]STL容器Vector的内存释放
直接抛出两句话,说明到底应该如何释放Vector占用的内存. “vector的clear不影响capacity,你应该swap一个空的vector.” <Effective STL>中的“ ...
- STL容器vector应用注意事项
[1]提前分配足够空间以免不必要的重新分配和复制代价 关于vector容器重新分配和复制及析构释放的代价,请参见随笔<STL容器之vector>. 应用示例对比代码如下: #include ...
- STL容器 vector,list,deque 性能比较
C++的STL模板库中提供了3种容器类:vector,list,deque对于这三种容器,在觉得好用的同时,经常会让我们困惑应该选择哪一种来实现我们的逻辑.在少量数据操作的程序中随便哪一种用起来感觉差 ...
- STL容器 -- Vector
核心:Vector 是 STL 里的一个向量容器,可以像数组那样进行随机访问,能在尾部插入元素,对于元素的删除和插入可以动态管理内存. 头文件: #include <vector> 构造函 ...
- ACM常用STL容器
// STL(标准模板库),由三大部分组成:容器,算法,迭代器 // STL六大组件:container(容器),algorthm(算法),iterator(迭代器) // function obje ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
- STL之vector容器详解
vector 容器 vector是C++标准模版库(STL,Standard Template Library)中的部分内容.之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说: ...
随机推荐
- BZOJ 4380 Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- IntelliJ IDEA代码分屏显示
- perf 工具介绍3
http://blog.chinaunix.net/uid-10540984-id-3854969.html http://blog.csdn.net/zhangskd/article/details ...
- IT程序猿们,我该做什么选择呢
这个时刻,我想我遇到人生小拐点了,程序猿到了30岁,到达了一个分界线了,现在的我该何去何从呢? 先谈下简单的情况吧: 来这个公司2年了,之前因为身体的原因,不想那么累,于是选择了一份维护的工作,就来了 ...
- IoC Containers with Xamarin
When writing cross platform apps with Xamarin, our goal is share as close to 100% of our code across ...
- JAVA基础知识要点
MQ.dubbo.SpringCloud 1) 集合框架 2)线程 3)IO流 4)类和对象生命周期 5)JAVA的反射机制 6) JVM 7)数据结构和常用算法 8)设计模式 9)网络编程
- .NET:CLR via C# User-Mode Constructs
The CLR guarantees that reads and writes to variables of the following data types are atomic: Boolea ...
- 求助关于jquery easyUI中的treegrid组件,请各位帮忙给个思路,谢谢啦
现在项目中用到jquery easyUI中的treegrid组件,已经可以正常显示了.但是在保存的时候遇到问题,页面上参照官网的例子可以在页面更新,但是怎么获取编辑后的数据进而保存到数据库呢?
- 使用Artful MySQL模块出错
总是报错: root@gitlab:/opt# newlisp newLISP v.10.6.0 64-bit on Linux IPv4/6 UTF-8 libffi, options: newli ...
- NFV/SDN驱动OSS转型