在<<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 的使用的更多相关文章

  1. 顺序容器:vector,deque,list

    1.顺序容器:vector,deque,list 容器类共享公共接口,只要学会其中一种类型就能运用另一种类型.每种容器提供一组不同的时间和功能这种方案,通常不需要修改代码,秩序改变类型声明,每一种容器 ...

  2. STL标准模板库 向量容器(vector)

    向量容器使用动态数组存储.管理对象.因为数组是一个随机访问数据结构,所以可以随机访问向量中的元素.在数组中间或是开始处插入一个元素是费时的,特别是在数组非常大的时候更是如此.然而在数组末端插入元素却很 ...

  3. 02--STL序列容器(Vector)

    一:vector容器简介 图片和顺序栈相似,但是vector数组是动态数组,支持随机存取--->但是在尾部添加或者溢出元素非常快速,中间插入删除费时 vector是将元素置于一个动态数组中加以管 ...

  4. C++ STL--顺序容器(vector)

    STL(标准模板库) 一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量.链表.队列.栈. C++标准模板库的核心包含以下组件: ...

  5. 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》

    如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...

  6. 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined

    手动获取容器对象时,报no qualifying bean of type is defined, 经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化. 1.SpringConf ...

  7. 获取spring容器对象方法和原因

    为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...

  8. 配置Spring的用于初始化容器对象的监听器

    <!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...

  9. javascript客户端遍历控件与获取父容器对象

    javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll()    {        var inputs=document. ...

随机推荐

  1. Bootstrap入门(十二)组件6:导航标签页

    Bootstrap入门(十二)组件6:导航标签页   1.标签页 2.胶囊式标签页 3.垂直展示 4.两端对齐的标签页 5.禁用的链接 6.添加下拉菜单   先引入本地的CSS文件和JS文件(注:1. ...

  2. python实现二分查找与冒泡排序

    二分查找,代码如下: def binarySearch(l, t): low, high = 0, len(l) - 1 while low < high: 'print low, high' ...

  3. 理解javascript中的Function.prototype.bind

    在初学Javascript时,我们也许不需要担心函数绑定的问题,但是当我们需要在另一个函数中保持上下文对象this时,就会遇到相应的问题了,我见过很多人处理这种问题都是先将this赋值给一个变量(比如 ...

  4. MVC 5 on Windows Server 2008/IIS 7

    将网站部署在2008上,结果出现了 HTTP Error 403.14 - Forbidden The Web server is configured to not list the content ...

  5. TCP/IP协议族(二) HTTP报文头解析

    本篇博客我们就来详细的聊一下HTTP协议的常用头部字段,当然我们将其分为请求头和响应头进行阐述.下方是报文头每个字段的格式,首先是头部字段的名称,如Accept,冒号后方紧跟的是该字段名所对应的值,每 ...

  6. php抽奖概率算法(刮刮卡,大转盘)

    两种方法:①概率随着抽的奖项的变少而时刻变化 经典的概率算法函数:如下 <?php /*  * 经典的概率算法,  * $proArr是一个预先设置的数组,  * 假设数组为:array(20, ...

  7. Windows程序员必须知道的字符编码和字符集

     字符编码 (Character encoding) 在存储和传递文本过程中,为了使得所有电脑都能够正确的识别出文本内容,需要有一个统一的规则. 2. 字符集 (Character Set) ) 一般 ...

  8. MVC中登录页图片验证码总结

    直接上代码了 using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imagi ...

  9. shell-3

    每天学习一篇:利用跑代码或者空闲思考时间,启动环境时间等闲杂时间: http://blog.csdn.net/junjieguo/article/category/880326/2

  10. jQuery源码学习:Deferred Object

    本文所有讨论均基于jQuery版本3.1.1,官网http://jquery.com/. 一.Deferred Object 1. 简介和创建 详见API:http://api.jquery.com/ ...