C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器、迭代器、函数对象和算法的模板。其中容器是存储类型相同的数据的结构(如vector,list, deque, set, map等),算法完成特定任务,迭代器用来遍历容器对象,扮演容器和算法之间的胶合剂。
int a[] = {, , , , };
vector<int> v0(a, a+);
vector<int> v1(v0);
vector<int> v2(v0.begin(), v0.begin()+);
vector<int> v3();
vector<int> v{, , , , };
(2)vector方法
v2.assign(v1.end()-v1.size()/2, v1.end());
v2.assign(a, a+2);
v1.push_back(rand()%);
v1.pop_back();
cout<<v1.size()<<endl;
cout<<v1.capacity()<<endl;
if(!v1.empty())
{
v1.pop_back();
cout<<"v1 is not empty!"<<endl;
}
v1.resize(9, 6);
v1.reserve(14);
v0.insert(v0.begin(), a[]);
v1.insert(v1.begin(), a, a+);
v1.insert(v1.begin(), v0.begin()+, v0.begin()+); v1.earse(v1.end()-);
v1.earse(v1.end()-, v1.end());
front 返回容器的第一个元素
cout<<v1.front()<<endl;
cout<<v1.end()<<endl;
v1.clear();
vector<int>().swap(v1);
for(vector<int>::iterator iter0=vt1.begin(); iter0!=vt1.end(); iter0++)
{cout<<*iter0;}
当然,C++11提供了类型推导,上述语句可简化为为
for(auto iter0=vt1.begin(); iter0!=vt1.end(); iter0++)
{cout<<*iter0;}
for(unsinged int i=; i<vt1.size(); i++)
{cout<<vt1[i];}
void ShowInt(int &s)
{
cout<<s<<" ";
}
for_each(v1.begin(), v1.end(), ShowInt);
//C++11支持lambada表达式,可以写成一句话:
for_each(v1.begin(), v1.end(), [](int &s){
cout<<s<<" ";
});
sort 容器内对象元素按照指定的规则进行排序,默认按照从小到大排列(不包含比较函数),当然也可以自定义比较函数
bool CompareInt(int &t1, int &t2)
{
if(t1<t2)
return true;
return false;
}
sort(v1.begin(), v1.end(), CompareInt);
sort(v1.begin(), v1.end());
//C++11 lambada
sort(v1.begin(), v1.end(), [](int &t1, int &t2}->bool{
if(t1<t2)
return true;
return false;
});
auto iter0 = lower_bound(v1.begin(), v1.end(), );
cout<<*iter0<<endl;
iter0 = upper_bound(v1.begin(), v1.end(), );
cout<<*iter0<<endl;
random_shuffle 随机排列容器内元素,其中第三个变量为提供的自定义随机函数。
random_shuffle(v1.begin(), v1.end());
//C++11 lambada
random_shuffle(v1.begin, v1.end(), [](const unsigned int n)->unsigned int{
return rand()%n;
});
/*****************************************
#device.h
#用户自定义类模板
#命名空间 user
#用户自定义类模板 device<T>
#构造函数重载
#一元、 二元运算符重载
*******************************************/
#include <string>
#include <iostream> using namespace std;
namespace user
{
//自定义类模板
template<class T>
class device
{
public:
//构造函数重载
device(){}
device(T a){id = a; name=" ";}
device(string str){name = str; id=;}
device(string str, T a){name = str; id = a;}
~device(){};
void show_info(); //一元运算符重载
device<T>& operator++(int); //二元运算符重载
friend device<T>& operator+ <T>(device<T>&d, string &s);
friend bool operator< <T>(device<T> &d1, device<T> &d2);
friend ostream& operator<< <T>(ostream &out, const device<T> &d);
private:
T id;
string name;
}; template<class T>
void device<T>::show_info(void)
{
cout<<"id:"<<id<<" ";
cout<<"name:"<<name<<endl;
} template<class T>
ostream& operator<<(ostream &out, const device<T> &d)
{
out<<"id:"<<d.id<<" name:"<<d.name<<"\n";
return out;
} template<class T>
bool operator<(device<T> &d1, device<T> &d2)
{
if(d1.id < d2.id)
return true;
else if(d1.id == d2.id && d1.name<d2.name)
return true;
else
return false;
} template<class T>
device<T>& device<T>::operator++(int)
{
this->id++;
return *this;
} template<class T>
device<T>& operator+(device<T> &d, string &s)
{
d.name = s + d.name;
return d;
}
}
vectorDemo.cpp主要涉及智能指针,用户自定义类型及STL vector容器及算法的相应应用。
/*****************************************
#VectorDemo.cpp
#vector容器方法
#迭代器
#STL算法
#lambada表达式
#智能指针
*******************************************/
#include "VectorDemo.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <memory>
#include "device.h"
#include <string> using namespace std;
using namespace user; //函数模板
template<class T>
void ShowDevice(shared_ptr<device<T>> &s)
{
cout<<*s;
} template<class T>
void show_genericity_demo(vector<shared_ptr<device<T>>> &d)
{
//<<** 三次重载,第一次取智能指针,第二次取对象, 第三次输出
for(auto iter=d.begin(); iter!=d.end(); iter++)
{
cout<<**iter; //(*iter)->show_info();
}
cout<<endl;
} template<class T>
bool genericity_compare(shared_ptr<device<T>> &d1, shared_ptr<device<T>> &d2)
{
//重载运算符< 比较
if((*d1) < (*d2))
return true;
else
return false;
} //复杂容器vector模板,涉及智能指针和用户自定义类型
int genericity_vector_Demo(void)
{
shared_ptr<device<int>> spd[] = {
shared_ptr<device<int>>(new device<int>("TV", rand()%)),
shared_ptr<device<int>>(new device<int>("Phone", rand()%)),
shared_ptr<device<int>>(new device<int>("Computer", rand()%)),
shared_ptr<device<int>>(new device<int>("light", rand()%)),
shared_ptr<device<int>>(new device<int>("pot", rand()%)),
}; //vector容器创建和初始化,对象为device对应的智能指针
vector<shared_ptr<device<int>>> vspd(spd, spd+); //遍历显示vector中的所有对象,传入为shared_ptr指针,因此*号,在通过<< 运算符重载
for_each(vspd.begin(), vspd.end(), ShowDevice<int>);
cout<<endl; //排序 lambada表达式
sort(vspd.begin(), vspd.end(), [](shared_ptr<device<int>> &t1, shared_ptr<device<int>> &t2)->bool{
if((*t1)<(*t2))
return true;
return false;
}); //乱序排列容器内对象
random_shuffle(vspd.begin(), vspd.end());
show_genericity_demo<int>(vspd); cout<<*vspd.front()<<endl;
cout<<*vspd.back()<<endl; vspd.push_back(shared_ptr<device<int>>(new device<int>("icebox", rand()%)));
vspd.push_back(shared_ptr<device<int>>(new device<int>("PlayStation", rand()%))); //排序,因为容器元素为智能指针,不支持省略比较函数方法
sort(vspd.begin(), vspd.end(), genericity_compare<int>);
show_genericity_demo<int>(vspd); vspd.pop_back();
show_genericity_demo<int>(vspd); //遍历 +, ++运算符重载,实现对容器内对象元素修改.
for_each(vspd.begin(), vspd.end(), [](shared_ptr<device<int>> &s){
(*s) = (*s) + string("famlily ");
(*s)++;
});
show_genericity_demo<int>(vspd); return ;
}
相关代码:容器范例下载
参考书籍:
2. STL源码剖析
C++ STL vector容器学习的更多相关文章
- 浅谈C++ STL vector 容器
浅谈C++ STL vector 容器 本篇随笔简单介绍一下\(C++STL\)中\(vector\)容器的使用方法和常见的使用技巧.\(vector\)容器是\(C++STL\)的一种比较基本的容器 ...
- STL - vector容器
1Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添加 ...
- STL vector容器 和deque容器
前言 STL是C++的框架,然后vector容器和deque容器又是STL的一部分... 这块的内容都是理解.概念为主,没什么捷径,希望读者能静下来记. 先来讲vector容器(单端动态数组) 1.v ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- [C++STL] vector 容器的入门
vector容器的入门 #include<vector> 创建vector容器的几种方式 数据类型可以是结构体,也能是另外一个容器 vector 的初始化: (1) 创建并声明大小 vec ...
- STL vector容器需要警惕的一些坑
从迭代器中取值切记需要判断是否为空 例如: vector<int> vtTest; vtTest.clear(); if (vtTest.empty()){ ; } ]; 如果没有忘了判断 ...
- C++ STL vector的学习
vector就是一个不定长数组,vector是动态数组,随着元素的加入,它的内部机制会自行扩充空间以容纳新元素,使用vector之前,必须包含相应的头文件和命名空间. #include <vec ...
- c++ STL vector初步学习
/*vector(向量):是一种顺序容器,,动态数组,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷, ...
- stl map容器 学习
#include<map> 1.map的声明: map<string,int>map_1; map_1 就是一个string对int的映射. 2.map的用法(映射): map ...
随机推荐
- CentOS 6.2 SVN搭建 (YUM安装)
安装说明 安装了一下SVN服务器,本文没有与Apache整合,过程如下: 系统环境:CentOS-6.2安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN ...
- html5,加密元素
<form action=""> 账号:<input type="text" name="user"> ...
- link和@import的区别、及各自的应用
面试的过程中遇到的问题,当时自己回答的感觉自己心里还是很满意的,但是回来百度查看后才知道自己回答的有多么的糟糕: 下面我这这个知识点做一些总结的书面说明,为了少走点弯路,多涨点见识吧. 首先我们要了解 ...
- Android studio 中的配置编译错误总结
1.编译Andorid 工程的时候,有时候出现gradle 报下面的错误. Error:(1, 0) Cause: com/android/build/gradle/LibraryPlugin : U ...
- DOM的基本属性
结构和内容属性1.1 nodeType1.2 nodeName, tagName1.3 innerHTML1.4 innerHTML pitFalls1.5 nodeValue 总结DOM节点是一个对 ...
- dr.wondr博士随笔之某古旧非智能机T6XXX 恢复一例
大家好!欢迎再次来到dr.Wonde的微博! 今次我给大家带来索尼爱立信古董机T650i的取证工作展示! 首先请出今天我们的主角索尼爱立信 T650i>> 然后在工厂模式下,连接设备,看图 ...
- Weblogic的架构
WebLogic管理控制台 WebLogic服务器提供了一个健壮的基于Web 的工具——管理控制台,它是执行上述任务的主要工具.通过管理控制台,你可以访问WebLogic管理服务.管理服务实现了 S ...
- 制作QQ空间的一些想法
新的项目开始了,这一次是做一个网站类似于QQ空间那样的,基本功能比如说写日志,说说之类的都要有(说说是要有楼中楼嵌套的,应该能够上传图片),还要可以修改个人信息.登录注册之类的更不用说了,还要有一定的 ...
- The Number Off of FFF
X soldiers from the famous “FFF army'' is standing in a line, from left to right. You, as the captai ...
- 我人生中的jQuery选择器
Jquery选择器 一.Jquery选择器简介 JavaScript只是一种运行于客户端,可以被客户端浏览器解析的一段代码.它和java没有任何关系.JavaScript简称JS.jQuery是对JS ...