C++ 容器对象vector和list 的使用
在<<c++ primer>>第四版Exercise Section 9.3.4 的Exercise 9.20 是这样的一道题目:编写程序判断一个vector<int> 容器包含的元素是否与list<int> 容器完全相同。测试代码如下:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <list>
#include <deque>
#include <vector> using namespace std; int main()
{
vector<int> vect;
list<int> li;
int vect_copy[] = {,,,,,,};
int li_copy[] = {,,,,,,};
vect.insert(vect.begin(),vect_copy, vect_copy+);
li.insert(li.begin(),li_copy,li_copy+); if (vect.size() != li.size())
{
cout << "it is different." << endl;
return ;
} for (vector<int>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
{
bool flag = false;
for (list<int>::iterator begin_li = li.begin(); begin_li != li.end(); ++begin_li)
{
if (*begin == *begin_li)
{
flag = true;
continue;
}
}
if (flag)
{ }
else
{
cout << *begin << " in vect is not in the list " << endl;
}
}
system("PAUSE");
return ;
}
容器对象有一个Insert成员函数,是用于在容器中插入元素使用,第一个参数是插入的位置,是个迭代器,后面两个参数是需要插入的元素迭代器开始和结尾。因为数组名是一个指针,因此这里也直接传入了数组名。
另外,这里有两个循环,分别遍历vector和list.
获取元素操作如下:
#include "stdafx.h"
#include <vector>
#include <iostream> using namespace std; int main()
{
vector<int> vect;
int arry[] = {,,,,,};
vector<int> vect2;
vect.insert(vect.begin(),arry,arry+);
cout << vect[] << endl;
cout << vect.front() << endl;
cout << *vect.begin() << endl;
cout << vect2[] << endl;
system("PAUSE");
return ;
}
这里需要留意的是,如果容器对象为空,则必须要先进行判断,否则操作未定义。如上代码是不能运行的。
删除元素的操作如下,这里的代码分别删除容器中的偶数和奇数。删除后打印容器。
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <list> using namespace std; int main()
{
int ia[] = {,,,,,,,,,,};
list<int> li;
vector<int> vect;
li.insert(li.begin(),ia,ia+);
vect.insert(vect.begin(),ia,ia+); // to operate the list object.
for (auto begin = li.begin(); begin != li.end();)
{
if ((*begin)% == )
{
begin = li.erase(begin);
continue;
}
++begin;
} cout << "To print the list with odd number:" << endl; // print the list.
for (auto begin = li.begin(); begin != li.end(); ++begin)
{
cout << *begin << endl;
} // to operate vector
for (vector<int>::iterator begin = vect.begin(); begin != vect.end();)
{
if (*begin % )
{
begin = vect.erase(begin);
continue;
}
++begin;
} cout << "To print the vector with with even number:" << endl;
for (vector<int>::iterator begin = vect.begin(); begin != vect.end();++begin)
{
cout << *begin << endl;
} system("PAUSE");
return ;
}
删除特定元素操作如下:
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <list>
#include <string> using namespace std; int main()
{
string arry[] = {"zhi","haha","heihei","hehe","huhu"};
list<string> li;
li.insert(li.begin(),arry,arry+sizeof(arry)/sizeof(*arry));
list<string>::iterator ite = find(li.begin(),li.end(),"haha");
li.erase(ite); for (list<string>::iterator begin = li.begin(); begin != li.end(); ++begin)
{
cout << *begin << endl;
}
system("PAUSE");
return ;
}
赋值操作如下,将一个vector中的元素拷贝到一个List 容器中。
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <list>
#include <string> using namespace std; int main()
{
list<char*> li;
vector<string> vect; char *listarry[] = {"hello","world","Li","Zhi"};
string stringarry[] = {"heihei","haha","hehe","huhu"};
li.insert(li.begin(), listarry, listarry+);
vect.insert(vect.begin(), stringarry, stringarry+); cout << "The vector is : " << endl;
for (vector<string>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
{
cout << *begin << endl;
} cout << "The list is : " << endl;
for (list<char*>::iterator begin = li.begin(); begin != li.end(); ++begin)
{
cout << *begin << endl;
} vect.assign(li.begin(),li.end()); cout << "after update. The vector is : " << endl;
for (vector<string>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
{
cout << *begin << endl;
} system("PAUSE");
return ;
}
C++ 容器对象vector和list 的使用的更多相关文章
- 顺序容器:vector,deque,list
1.顺序容器:vector,deque,list 容器类共享公共接口,只要学会其中一种类型就能运用另一种类型.每种容器提供一组不同的时间和功能这种方案,通常不需要修改代码,秩序改变类型声明,每一种容器 ...
- STL标准模板库 向量容器(vector)
向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...
- 02--STL序列容器(Vector)
一:vector容器简介 图片和顺序栈相似,但是vector数组是动态数组,支持随机存取--->但是在尾部添加或者溢出元素非常快速,中间插入删除费时 vector是将元素置于一个动态数组中加以管 ...
- C++ STL--顺序容器(vector)
STL(标准模板库) 一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表.队列.栈. C++标准模板库的核心包含以下组件: ...
- 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》
如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...
- 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined
手动获取容器对象时,报no qualifying bean of type is defined, 经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化. 1.SpringConf ...
- 获取spring容器对象方法和原因
为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...
- 配置Spring的用于初始化容器对象的监听器
<!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...
- javascript客户端遍历控件与获取父容器对象
javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll() { var inputs=document. ...
随机推荐
- 创建 OVS vlan101 并部署 instance - 每天5分钟玩转 OpenStack(139)
前面我们创建了 OVS vlan100 并部署了 instance,今天继续创建 vlan101. subnet IP 地址为 172.16.101.0/24. 底层网络发生了什么变化 Neutron ...
- WinForm DataGridView增删改查
DataGridView连接数据库对表进行增删改查 一.绑定数据源 //做一个变量控制页面刷新 ; public Form1() { InitializeComponent(); } private ...
- javascript 计算两个日期的差值
代码 Typescript版 /** * TimeSpan just like the class TimpSpan in C# ,represent the time difference * @c ...
- 从C#到TypeScript - 接口
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- matlab最简单程序模板
% 脚本文件: 温度转换 % 文件名:temp_conversion % 目标:将输入的华氏温度转换为绝对温度 % % 版本记录: % 时间 编者 描述 % -- :: 泡泡 原始代码 % % 定义变 ...
- Java(多态练习 instanceof)
/* 题目: (多态,instanceof)有如下代码 class Animal { private String name; // 1 } class Dog extends Animal { // ...
- Java之路——敬JAVA初学者(作者:MoMo)
作为一名大四的毕业生,大学三年过,有得有失.作为一个喜欢编程,喜欢JAVA的人,自学其实是一件美事,很有意思的事.要是能再找个女朋友一起学.嘿嘿,就不枉在大学走了一遭啊! 要离开学校了,还是想留 ...
- BZOJ 1875: [SDOI2009]HH去散步(矩阵乘法)
首先,题意就把我们引向了矩阵乘法,注意边长m<=60,那么就按边建图,变成一个120个点的图,然后乱搞就行了。 PS:WA了N久改了3次终于A了QAQ CODE: #include<cst ...
- Android业务组件化之Gradle和Sonatype Nexus搭建私有maven仓库
前言: 公司的业务组件化推进的已经差不多三四个月的时间了,各个业务组件之间的解耦工作已经基本完成,各个业务组件以module的形式存在项目中,然后项目依赖本地的module,多少有点不太利于项目的并行 ...
- echarts动态添加数据(饼图为例)
$.ajax({type : "POST",async : false,url : '${ctx}/basic/bsAllPictureGuarantee/pictJson',da ...