参考书目: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. $cometoj\#4\ D\ $求和 不是$dp$

    \(Des\) \(Sol\) \(upd:\)以下两段是错误做法,但我不想删掉\(.jpg\) -----------------------以下是错误部分--------------------- ...

  2. ruby 编写控制台进度条

    ruby 中,$stdout.flush 让控制台当前行内容可以重写,以此我们可以做出进度条的效果. def set_progress(index, char = '*') print (char * ...

  3. 使用PE启动盘清空电脑登入密码

    1.PE启动盘制作过程 要制作一个启动盘可以使用很多工具来制作,比如老毛桃.U深度.大白菜等软件都可以制作PE启动盘.此处就用老毛桃制作PE启动盘为例(http://www.laomaotao.tv/ ...

  4. Java中数组的使用

    1.声明数组 1.1声明一维数组 声明一维数组有下列两种格式: 数组的元素类型      数组名字[ ]; 数组的元素类型[ ]   数组名字 1.2声明二维数组 声明二维数组有下列两种格式: 数组的 ...

  5. TensorFlow——常见张量操作的API函数

    1.张量 张量可以说是TensorFlow的标志,因为整个框架的名称TensorFlow就是张量流的意思,全面的认识一下张量.在TensorFlow程序使用tensor数据结构来代表所有的数据,在计算 ...

  6. 字符串分类 - hash

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 题目描述 Eddy likes to play with string which is a s ...

  7. 2018徐州现场赛A

    题目链接:http://codeforces.com/gym/102012/problem/A 题目给出的算法跑出的数据是真的水 #include<iostream> #include&l ...

  8. 使用vscode运行python出现中文乱码的解决方法

    前提:自己安装了code runner的插件 快捷键Ctrl+Shift+P,打开设置Open Settings (JSON):

  9. NumPy排序

    numpy.sort()函数 该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法 使用numpy.sort()方法的格式为: numpy.sort(a,axis,kind,orde ...

  10. python scoket

    一.简介 scoket(套结字)在python就是模块 二.分类 基于文件型(不用) 基于网络型 名字:AF_INET AF_INET6 三.scoket应用 1.基于tcp 长连接:基于tcp的Se ...