参考书目: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中还有很多容器,暂时用不到,有时间要进行系统学习。

最新文章

  1. 很强大的HTML+CSS+JS面试题(附带答案)
  2. php面试题及答案
  3. Golang通过Thrift框架完美实现跨语言调用
  4. JVM内存格局总结
  5. Linux 网络编程详解四(流协议与粘包)
  6. Flume 实战(1) -- 初体验
  7. 关于页面 reflow 和 repaint
  8. Java-SSI框架学习
  9. 如何通过WiFi来进行Android的真机模拟
  10. Qt Linux 使用QJson库
  11. 模拟HTTP请求:Request Maker
  12. Delphi调用Android的.so文件(转)
  13. Google Earth影像数据破解之旅
  14. Linux - 简明Shell编程03 - 字符串(String)
  15. [Redmine] Centos5上安装Redmine3.0+nginx+thin部署
  16. FreeSql.Repository 通用仓储层功能
  17. 设计模式总结篇系列:桥接模式(Bridge)
  18. caog
  19. Spring MVC静态资源处理(转)
  20. SpringBoot 核心配置

热门文章

  1. DEVOPS技术实践_19:Pipeline的多参数json调用
  2. 超详细!如何利用Huginn制作专属RSS
  3. 洛谷$P3647\ [APIO2014]$连珠线 换根$dp$
  4. $CF912E\ Prime\ Gift$ 二分+搜索
  5. GPL协议中国第一案尘埃落定,相关开源软件应如何风控?
  6. K8s 实践 | 如何解决多租户集群的安全隔离问题?
  7. Spring AOP 基于AspectJ
  8. 论Java中的抽象类与接口
  9. Airbnb如何应用AARRR策略成为全球第一民宿平台
  10. Tasker如何使用Tasker插件以及Tasker第三方应用