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. ...
随机推荐
- 6.00.1x Introduction to computation
6.00 Introduction to Computer Science and Programming • Goal: –Become skillful at ...
- requireJS的初步掌握
前一段时间,因为一些事吧这个习惯落下了,现在争取重新捡起来. 最近开始自学requireJS,为了更好的掌握,所以写出一个自我理解的博客供参考. 分割线------------------------ ...
- look look C#7
vs2017也rc好几个版本了,本想跟进看看c#7加入了什么内容,去搜索c#7,确实找到了不少文章,无奈很多特性ide根本不让编译啊...所以今天主要列出已经确定了的c#7特性(一般来说rc后也不会加 ...
- SQL SERVER将多行数据合并成一行(转载)
昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出在我想将一个表的一个列的多行内容拼接成一行 比如表中有两列数据 : ep_classes ep_name A ...
- JavaScript严格模式说明带示例
严格模式(use strict) 目的 消除JS中一些不合理.不严谨之处,减少一些怪异行为 消除代码中的一些不安全之处,保障代码运行安全 提高编译器效率,增加运行速度 为以后的JS新规范做铺垫 兼容性 ...
- Win8下,以管理员身份启动VS项目
之前一直是先以管理员身份启动VS,然后再打开项目的,比较麻烦,找了好久,总算有一个处理方案了 在Windows7下 通常使用修改属性的方式:在任意快捷方式上右击,选择属性,选择高级,选择以管理员身份启 ...
- C++STL笔记
C++STL 1.vector 向量,长度可变的数组 头文件 #include<vector> 1.1vector的定义 vector<typename> name; 例如: ...
- asyncio
一.简介 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用, ...
- 解析jQuery中extend方法--用法《一》
extend方法在jQuery中是一个很重要的方法,jQuey内部用它来扩展属性方法.常用语jQuery插件开发. jQuery提供了两个方法,$.extend和$.fn.extend,两个方法内部实 ...
- js拉起或下载app
产品提了个需求,通过手机网页判断是否安装了自己公司app,如果安装了则拉起app,没有安装则跳转到下载页. 经过各种查阅资料尝试总结了一个还算可以的办法. 拉起app的原理就是js和原生统一好一个地址 ...