参考书目:visual c++ 入门经典 第七版 Ivor Horton著 第十章

认识两个容器:vector和list

容器:是STL(Standard Template Library 标准模板库)的六大组件之一。(容器,容器适配器,迭代器,算法,函数对象,函数适配器)

容器是用来存储和组织其他对象的对象。提供要存储的对象的类型就可以从STL模板中创建容器类。

Vector <T>:表示一个在必要时刻可增加容量的数组,该数组存储T类型的元素。只能在矢量容器的末尾添加新元素。

Vector <int> mydata ;//创建一个存储int 类型的值的容器,存储元素的初始容量是0;

mydata.push_back(99);//向矢量末尾添加一个新元素;

mydata.pop_back();//删除末尾一个元素

mydata.clear();

mydata.insert(begin(mydata)+1,88);//在第1个元素后面插入新的元素88

vec.insert(begin(vec)+1,3,22);//在第一个元素后面插入3个元素,22,22,22

vec.reserve(datasize);//为容器预留空间

例子:(vector容器的构造和读取)

//Person.h
#pragma once
#include <iostream>
#include<cstring> class Person
{
public:
Person();
public:
~Person(); private:
void initName(const char* first, const char* second);
char* firstname;
char* secondname;
public:
void showperson()const;
Person(char* first, char* second);
Person(const Person & p);
Person(Person&& p);
Person& operator=(const Person& p);
// move
Person& operator=(Person&& p);
bool operator<(const Person& p) const;
};
//Person.cpp
#include "Person.h" Person::Person()
: firstname(NULL)
, secondname(NULL)
{
} Person::~Person()
{
} void Person::initName(const char* first, const char* second)
{
size_t length{ strlen(first) + };
firstname = new char[length];
strcpy_s(firstname, length, first);
length = strlen(second) + ;
secondname = new char[length];
strcpy_s(secondname, length, second);
} void Person::showperson()const
{
std::cout << firstname << "" << secondname << std::endl;
} Person::Person(char* first, char* second)
{
initName(first, second);
} Person::Person(const Person & p)
{
initName(p.firstname, p.secondname);
} Person::Person(Person&& p)
{
firstname = p.firstname;
secondname = p.secondname;
//reset rvalue object pointer to prevent deletion
p.firstname = nullptr;
p.secondname = nullptr;
} //copy
Person& Person::operator=(const Person& p)
{
//TODO: insert return statement here
if (&p != this)
{
delete[] firstname;
delete[] secondname;
initName(p.firstname, p.secondname);
}
return *this;
} // move
Person& Person::operator=(Person&& p)
{
if (&p != this)
{
delete[] firstname;
delete[] secondname;
firstname = p.firstname;
secondname = p.secondname;
//reset rvalue object pointer to prevent deletion
p.firstname = nullptr;
p.secondname = nullptr;
}
return *this;
//TODO: insert return statement here
} bool Person::operator<(const Person& p) const
{
int result{ strcmp(secondname, p.secondname) };
return (result<||result==&&strcmp(firstname,p.firstname)<);
}
//Ex10.02.cpp
//storing objects in a vector
#include <iostream>
#include<vector>
#include "Person.h" using std::vector;
using std::cout;
using std::endl; int main()
{
vector<Person> people;
const size_t maxlength{ };
char firstname[maxlength];
char secondname[maxlength];
while ()
{
cout << "enter a first name or press Enter to end: ";
std::cin.getline(firstname, maxlength, '\n');
if (strlen(firstname) == )
{
break;
}
cout << "enter the second name :";
std::cin.getline(secondname, maxlength, '\n');
people.emplace_back(Person(firstname, secondname));
}
cout << endl;
auto iter = cbegin(people);
while (iter != cend(people))
{
iter->showperson();
++iter;
}
char mynamef [] = { "myfirst" };
char mynames[] = { "mysecond" };
Person insert_t ( mynamef, mynames ); people.insert(begin(people) + , insert_t);
iter = begin(people)+;
iter->showperson();
}

List<T>

实现了双向链表,优点是:可以在固定时间内在序列的任意位置插入或删除元素,确定是列表不能根据位置直接访问其元素。

访问元素的方法是,从某个已知位置开始遍历列表中的元素。

创建:std::list<double> values (50,2.728);

插入:values.insert(++begin(values),75);

构建元素:emplace( , );emplace_back( );emplace_front();

访问:for (const auto & s:values){std::cout<<s<<endl;}

例子:

//example for list
//get some sentences from keyboard ,then store it in the list
#include <iostream>
#include <list>
#include<string>
#include <functional> using std::string;
using std::cout;
using std::endl; void listAll(const std::list<string> & strings)
{
for (auto & s : strings)
{
cout << s << endl;
}
}
int main()
{
std::list<string> text;//创建list
cout << "Enter a few lines of text.just press Enter to end :" << endl;
string sentence;
while (getline(std::cin, sentence, '\n'), !sentence.empty())
{
text.push_front(sentence);
}
cout << "your text in reverse order: " << endl;//倒叙输出
listAll(text); text.sort();//排序
cout << "\nyour text in ascending sequence :" << endl;
listAll(text); }

后记:

这两个容器还只停留在能用的阶段,要在程序中理解和体会二者的区别与优劣,并深入学习关于数据结构的知识。
在STL中还有很多容器,暂时用不到,有时间要进行系统学习。

STL中的vector 和list的更多相关文章

  1. 转:用STL中的vector动态开辟二维数组

    用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...

  2. STL中的Vector相关用法

    STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...

  3. (转)C++ STL中的vector的内存分配与释放

    C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...

  4. C++STL中的vector的简单实用

    [原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...

  5. STL中的vector实现邻接表

    /* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include  ...

  6. stl 中List vector deque区别

    stl提供了三个最基本的容器:vector,list,deque.         vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此     它能非常好的支持随 ...

  7. c++ STL中的vector与list为什么没有提供find操作?

    map里有,set里也有,vector,list没有,太不公平了吧. 其实应该考虑为什么map,set里有find操作. include<algorithm>里有通用的find操作,通用的 ...

  8. STL中向量vector笔记

    vector的本质是:数组的封装 特点:读取能在常数时间内完成 Vector成员函数 函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数 ...

  9. STL中关于vector的一点有趣的事情

    PLZ ADD SOURCE: http://www.cnblogs.com/xdxer/p/4072056.html 今日饭后,一哥发给我一段代码,让我看看会不会有什么问题. #include< ...

随机推荐

  1. 转载:通过监控Nginx日志来实时屏蔽高频恶意访问的IP

    通过监控Nginx日志来实时屏蔽高频恶意访问的IP   目前在我的VPS上主要通过两种方式来限制ip的访问次数. 通过Nginx的limit_req配置来限制同一ip在一分钟内的访问次数 通过Ngin ...

  2. 洛谷$P$3301 $[SDOI2013]$方程 $exLucas$+容斥

    正解:$exLucas$+容斥 解题报告: 传送门! 在做了一定的容斥的题之后再看到这种题自然而然就应该想到容斥,,,? 没错这题确实就是容斥,和这题有点儿像 注意下的是这里的大于和小于条件处理方式不 ...

  3. HelloTalk 基于 OpenResty 的全球化探索之路

    2019 年 12 月 14 日,又拍云联合 Apache APISIX 社区举办 API 网关与高性能服务最佳实践丨Open Talk 广州站活动,HelloTalk, Inc. 后台技术负责人李凌 ...

  4. Linux下Qt+CUDA调试并运行

    Qt与CUDA相结合具体的操作主要修改qt项目中的配置文件pro.下面以测试的项目为例. 因为这是一个测试案例,代码很简单,下面将这几个文件的代码贴出来,方面后面对应pro文件和Makefile文件中 ...

  5. 9.python中sys.argv[]用法说明

    在python中sys.argv[]是用来获取命令行输入的参数的(参数和参数之间空格区分),sys.argv[0]表示代码本身文件路径,所以从参数1开始,表示获取的参数了 举例说明:创建一个程序名为t ...

  6. 初级程序员如何一分钟?解决一个BUG

    博主说明 -- 重要.重要.重要的事情说三遍 写这篇文章是主要锻炼写博客的能力以及记录自己的成长经历,要是写的不对欢迎大佬评论指正,同时希望对大家有所帮助.然后我写博客尽量简洁+图片+宏观的方式,便于 ...

  7. 小小TODO标识,你用对了吗?

    前言 有时,您需要标记部分代码以供将来参考,比如: 优化,改进,可能的更改,要讨论的问题等. 通常我们会在代码中加入如下的标记表示待办: //TODO 我将要在这里做 xxx 你这样做,别人也会这样做 ...

  8. Spring工程报错

    错误日志: 2014-09-24 10:50:16 [org.springframework.context.support.FileSystemXmlApplicationContext]-[INF ...

  9. docker制作cenos+php56+nginx镜像

    首先你环境要安装好docker 1 获取centos镜像. docker search centos 选取第一个官方镜像. docker pull  docker.io/centos 新建镜像挂载目录 ...

  10. php获取本年、本月、本周时间戳和日期格式

    时间戳格式: //获取今日开始时间戳和结束时间戳 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0 ...