参考书目: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. 机器学习- Numpy基础 吐血整理

    Numpy是专门为数据科学或者数据处理相关的需求设计的一个高效的组件.听起来是不是挺绕口的,其实简单来说就2个方面,一是Numpy是专门处理数据的,二是Numpy在处理数据方面很牛逼(肯定比Pytho ...

  2. .NetCore集成Dapr踩坑经历

    该篇内容由个人博客点击跳转同步更新!转载请注明出处 前言 之前自己有个core2.2的项目一直是用的Surging作为微服务框架的,后来了解到了Dapr,发现比较轻量级,开发部署等也非常方便,故将自己 ...

  3. 通过公网连接阿里云redis,rinetd

    目前云数据库 Redis 需要通过 ECS 的内网进行连接访问.如果您本地需要通过公网访问云数据库 Redis,可以在 ECS Linux 云服务器中安装 rinetd 进行转发实现. 1.在云服务器 ...

  4. Linux下自动化部署ASP.NET CORE 3.1(Docker+Jenkins+Nginx)

    1.先配置好Docker阿里云加速,可以使用阿里云容器服务 (可自己在阿里云申请,要不然安装东西直接很慢)注意:https://XXXX.mirror.aliyuncs.com为阿里云加速服务分配地址 ...

  5. [NoSQL] 从模型关系看 Mongodb 的选择理由

    往期:Mongodb攻略 回顾 Mongodb 与关系型数据库的对应关系: MySQL   MongoDB database(数据库) database(数据库) table(表) collectio ...

  6. C Primer Plus(二)

    重读C Primer Plus ,查漏补缺 重读C Primer Plus,记录遗漏的.未掌握的.不清楚的知识点 分支和跳转 1.ctype.h头文件里包含了一些列用于字符判断的函数,包括判断数字.大 ...

  7. 用实例理解设计模式——代理模式(Python版)

    代理模式:为其他对象提供一种代理以控制对这个对象的访问. 在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式分为: 静态代理 动态代 ...

  8. python I/O编程

    1.文件读写 使用open打开文件,f=open('/user/test.txt','r'),r表示可读 如果文件不存在,则抛出IOError 文件打开,则用read()方法进行读取 最后关闭用clo ...

  9. 2019牛客暑期多校第二场题解FH

    F.Partition problem 传送门 题意:有2n个人,分两组,每组n个,要求sum(vij)最大值. 题解:n并不大我们可以枚举每个人是在1组还是2组爆搜. 代码: #include &l ...

  10. 双射 - hash去重

    题目描述Two undirected simple graphs and where are isomorphic when there exists a bijection on V satisfy ...