在<<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. document.body.clientHeight的取值

    http://www.cnblogs.com/fullhouse/archive/2012/01/05/2313800.html 有时候需要取页面的底部, 就会用到document.body.clie ...

  2. log4j.appender.stdout.layout.ConversionPattern

    http://501565246-qq-com.iteye.com/blog/1991881 http://wenku.baidu.com/link?url=e4Z9v9CY_gwRxHrggzHXx ...

  3. Canvas createImageData

    createImageData() 方法创建新的空白 ImageData 对象.新对象的默认像素值 transparent black. 对于 ImageData 对象中的每个像素,都存在着四方面的信 ...

  4. android Fragment和FragmentActivity

    MainActivity.java import android.app.AlertDialog; import android.app.Notification; import android.co ...

  5. TCP&UDP

    TCP(传输控制协议) 建立连接,形成传输数据的通道 在连接中进行大数据传输(数据大小不受限制) 通过三次握手完成连接,是可靠协议,安全送达(三次握手向服务器发送请求,响应请求回复,发送数据) 必须建 ...

  6. 内功心法 -- java.util.ArrayList<E> (2)

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...

  7. 使用JAVA开发微信公众平台(一)——环境搭建与开发接入

    一. 初始微信公众平台 微信公众平台,即我们平时所说的"公众号",曾用名"官方平台"."媒体平台",但最终命名为"公众平台&quo ...

  8. 从并发处理谈PHP进程间通信(一)外部介质

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  9. 修改WebView

    http://jwdev.cn/2015/09/28/use-javascript-to-delete-web-element/ 

  10. matlab中小技巧

    关于matlab中可能遇到的小知识点 一.字符串的比较 不能使用“==”,需要使用函数strcmp() %matlab中字符串的比较 %字符串比较要用strcmp.相同则返回1,不相同则返回0. cl ...