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(5);

    // 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[1], sentence[3]);

    // 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 ----------------

 

时间: 2024-07-31 00:58:10

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

c++-简单重写stl里vector的构造函数

问题描述 简单重写stl里vector的构造函数 #include#include#includeusing namespace std; templateclass Vector{private: int size; T* list;public: Vector(int sz); Vector(int sz T t(int s)); ~Vector(){ delete[] list; } T& operator ;}; //构造函数templateVector::Vector(int sz){

ACM STL容器和算法

1.4      STL 的组成 STL有三大核心部分:容器(Container).算法(Algorithms).迭代器(Iterator),容器适配器(container adaptor),函数对象(functor),除此之外还有STL其他标准组件.通俗的讲: 容器:装东西的东西,装水的杯子,装咸水的大海,装人的教室--STL里的容器是可容纳一些数据的模板类. 算法:就是往杯子里倒水,往大海里排污,从教室里撵人--STL里的算法,就是处理容器里面数据的方法.操作. 迭代器:往杯子里倒水的水壶,

STL容器删除元素的陷阱

今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector<struct> mFriendList;...std::vector<struct>::iterator iter = mFriendList.begin();for ( ; iter != mFriendList.end(); ++iter){    if (...)        mFr

【温故而知新】C和C++5:STL容器技术综述

容器类是可以包含其他对象的类.STL中提供的较为常用的容器类有向量.链表.队列.集合和图等,每一种容器类都是一个模板,可以包含各种类型的对象.这些容器可以分为序列式和关联式两大类. 序列式容器主要有: 1.vector:向量类,可以认为是一种容量可变的数组,可以提供对元素的随机访问,而且可以在序列尾部快速插入和删除数据,并且在需要的时候可以方便地改变容器的大小: 2.list:双向链表类,不支持随机访问的数组类,遍历链表的元素需要从某个端点开始向前或向后遍历: 3.queue:队列,实现先进先出

不要在公共接口中传递STL容器

  最近的一个项目,是开发一个framework,提供给公司内部不同的产品线使用. 之间遇到的一个问题,就是STL容器的使用, 而结论是不要在公共接口中传递STL容器: 这里说的STL容器,但主要则是指容器,字符串类,但其实可以推广到在STL中提供的任何类型,这里说的公共接口,是指需要暴露给客户的sdk头文件,包括函数签名,或者类成员变量:也可以说,不要在暴露给客户的头文件中包含STL的头文件. 原因分析为什么有这个结论,我们可以从几个方面来论述: 客户端使用的STL版本可能不同因为STL作为标

C++ STL容器stack和queue详解_C 语言

stack是一个比较简单的容器,它的使用也很简单,stack是LIFO容器,就是后进先出,最后添加进去的元素,第一个取出来 stack初始化 std::stack<int> first; std::stack<int> second(first); std::stack<int, std;:vector<int>> third; //使用vector初始化stack ### stack常用方法### empty();//判断是否为空 push(Elem e)

STL容器与拷贝构造函数

所有容器提供的都是"value语意"而非"reference语意".容器内进行元素的安插操作时,内部实施的是拷贝操作,置于容器内.因此STL容器 的每一个元素都必须能够拷贝.---<<C++标准程序库>> 侯捷.孟岩译 p144页原文  以vector为例,往Vector中(实际上所有STL容器都是这样)放元素,Vector会调用元素类的拷贝构造函数生成的副本,当 Vector走出生存期时(),会自动调用其中每个元素的析构函数.比如,如果 v

C++ STL中用vector 改进内存的再分配

本文描述的是一种很常见的情况:当你在某个缓存中存储数据时,常常需要在运行时调整 该缓存的大小,以便能容纳更多的数据.本文将讨论如何使用 STL 的 vector 进行内存的再 分配. 这里描述的是一种很常见的情况:当你在某个缓存中存储数据时,常常需要在 运行时调整该缓存的大小,以便能容纳更多的数据.传统的内存再分配技术非常繁琐,而且 容易出错:在 C 语言中,一般都是每次在需要扩充缓存的时候调用 realloc().在 C++ 中 情况更糟,你甚至无法在函数中为 new 操作分配的数组重新申请内

【C/C++学院】0828-STL入门与简介/STL容器概念/容器迭代器仿函数算法STL概念例子/栈队列双端队列优先队列/数据结构堆的概念/红黑树容器

STL入门与简介 #include<iostream> #include <vector>//容器 #include<array>//数组 #include <algorithm>//算法 using namespace std; //实现一个类模板,专门实现打印的功能 template<class T> //类模板实现了方法 class myvectorprint { public: void operator ()(const T &