STL中的vector 和list
参考书目: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的更多相关文章
- 转:用STL中的vector动态开辟二维数组
用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int mai ...
- STL中的Vector相关用法
STL中的Vector相关用法 标准库vector类型使用需要的头文件:#include <vector>. vector 是一个类模板,不是一种数据类型,vector<int> ...
- (转)C++ STL中的vector的内存分配与释放
C++ STL中的vector的内存分配与释放http://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html 1.vector的内 ...
- C++STL中的vector的简单实用
[原创] 使用C++STL中的vector, #include <stdio.h> #include<stdlib.h> #include<vector> usin ...
- STL中的vector实现邻接表
/* STL中的vector实现邻接表 2014-4-2 08:28:45 */ #include <iostream> #include <vector> #include ...
- stl 中List vector deque区别
stl提供了三个最基本的容器:vector,list,deque. vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此 它能非常好的支持随 ...
- c++ STL中的vector与list为什么没有提供find操作?
map里有,set里也有,vector,list没有,太不公平了吧. 其实应该考虑为什么map,set里有find操作. include<algorithm>里有通用的find操作,通用的 ...
- STL中向量vector笔记
vector的本质是:数组的封装 特点:读取能在常数时间内完成 Vector成员函数 函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数 ...
- STL中关于vector的一点有趣的事情
PLZ ADD SOURCE: http://www.cnblogs.com/xdxer/p/4072056.html 今日饭后,一哥发给我一段代码,让我看看会不会有什么问题. #include< ...
随机推荐
- .NET Core开发的iNeuOS工业互联平台,升级四大特性:配置数据接口、图元绑定数据、预警配置和自定义菜单
目 录 1. 概述... 2 2. 演示信息... 2 3. iNeuView(Web组态)配置数据接口... 2 4. iNeuView(Web组 ...
- java中发送http请求的方法
package org.jeecgframework.test.demo; import java.io.BufferedReader; import java.io.FileOutputStream ...
- lintcode入门37-算法实现
lintcode入门级算法题37 一.题目 反转一个3位整数 反转一个只有3位数的整数. 样例 样例 1: 输入: number = 123 输出: 321 样例 2 ...
- 洛谷$P2050\ [NOI2012]$美食节 网络流
正解:网络流 解题报告: 传送门$QwQ$ 昂开始看到$jio$得,哇长得好像上一题嗷$QwQ$ 然后仔细康康数据范围,发现,哇好像要几万个点,,,显然就$GG$了 但感$jio$思路方向好对的亚子? ...
- $Noip2014/Luogu2312$ 解方程
$Luogu$ $Sol$ 枚举解+秦九韶公式计算+取模. $Code$ #include<iostream> #include<cstdio> #include<cst ...
- Windows To Go 企业版2019 LTSC 开发环境部署
Windows To Go 是一项非常实用的功能,与传统方式安装Windows 10相比更具有灵活性,会根据每次接入的硬件型号保留不同版本驱动. 由于博主是一名全栈程序员(截至发稿处于菜鸟级别),对灵 ...
- Intellij IDEA2019.1.3破解
下载 JetbrainsCrack.jar(链接:https://pan.baidu.com/s/1Dkw1PruzBlEMjcYszNlSZA 提取码:2bf7),放到bin目录下(其实位置可以随便 ...
- 27.openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽
openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽 from openpyxl import Workbook,load_workbook from openpyxl.dra ...
- 使用内存映射文件MMF实现大数据量导出时的内存优化
前言 导出功能几乎是所有应用系统必不可少功能,今天我们来谈一谈,如何使用内存映射文件MMF进行内存优化,本文重点介绍使用方法,相关原理可以参考文末的连接 实现 我们以单次导出一个excel举例(csv ...
- YOLO V3训练自己的数据集
数据的输入几乎和Faster rcnn一样,标签格式xml是一样的. 相比Faster rcnn,数据多了一步处理,通过voc_annotation.py将图片路径和bbox+class存储在txt下 ...