C++容器类的简单介绍

来源:互联网 发布:淘宝尾款怎么付 编辑:程序博客网 时间:2024/06/03 18:10

一、原型与构造函数

Vector的原型可定义为

vector<T, allocator <T> >

其构造函数为

vector()             //空的

vector(al)          //指定一种allocator

vector(n)          //用默认T()初始化n个元素

vector(n, val)   //用Val初始化n个元素

vector(n,val,al)         //用val初始化n个元素,用al做分配器

vector(first,last)       //从己有的first到last复制生成

vector(first,last,al) //从己有的first到last复制生成,用al做分配器

 

二、操作

1.开辟N个空间

vecobj.reserve(N);

2.当前(重新分配内存前)得到最大容量

capacity();

3.重新分配内存为N

resize(N)

如果变小,则删除多余。如果变大,则用T()添充

4.清空

clear();

注意,clear()和resize()都不一定使得vector变小,若欲释放内存,请使用vecobj.swap(vector<T, A>())

5.存取首尾元素

front()与back()操作,取后一个和最前一个元素,注意其返回是引用,其而是左值(l_value),因此可以赋值. 做类似于vecobj.front() = 3;的操作,但要保证front空间有效,否则形为无法预测。

6.取值

[]与at可以做此操作,at会检查,如果越界有会out_of_range的异常被throw

7.push_back, pop_back

要保证不为空

8.使用assign

assign可以改变大小和初值,大小是随意的,不受开始时大小的限制,如果设置为0,则清空。

assign(5,0)把vector改为5个大小,并用0添充

assign(iax+3,iax+5); 从数组第4到5个填充,注意左闭右开,即可取到iax[3]与iax[4]

9.使用insert

insert(it, x),在it前插入一个元素x

insert(it,first,last),在it前插入一个序列[first,last)左闭右开

10.使用erase

erase(it)删除在it处的元素,返回值为下一元素。如果intVec.erase(intVec.end());并不会报错,如果删除一个序列[first,last),使用erase(first,last)

11.BVector是vector<bool>的特化版,具体的用途有待查证

12.flip()把某一元素,求反。

13.swap. vecObj.swap(vecObj[i],vecObj[j]);

若要在容器中装一个对象并且能并检索,需要重载operator == ,如下:

#include <vector>

#include <iostream>

#include <stdlib.h>

#include <time.h>

//#include <getopt.h>

using namespace std;

 

class Obj

{

public:

         Obj(int x, int y, int z)

         {

                   this->x = x;

                   this->y = y;

                   this->z = z;

         }

         bool operator == (const Obj & obj)

         {

                   if(obj.x == x && obj.y == y && obj.z == z)

                            return true;

                   return false;

         }

         int getX()

         {

                   return this -> x;

         }

private:

         int x;

         int y;

         int z;

}

int main(int argc, char * argv[])

{

         vector<Obj> vecObj;

         Obj obj1(2,3,4);

         Obj obj2(4,5,6);

         vecObj.push_back(obj1);

         vecObj.push_back(obj2);

        

         vector<Obj>::iterator it =find(vecObj.begin(),vecObj.end(),Obj(2,3,4));

         if(it != vecObj.end())

                   cout << (*it).getX() << endl;

         return 0;

}

list的基本用法

与vector的用法基本相同,其中需要强调一点的是splice()函数,是指把指定段的另一个List插入到指定位置的前面。

splice(iterator it , list &x)

splice(iterator it, list &x, iterator first)

splice(iterator it,list &x, iterator first, iterator last)

 

一、原型与构造函数

typdef list<T, allocator<T> >  listObj;

构造函数

list() //空

list(al) //指定allocator的空表

list(n)//n个元素,所有元素都是T()出来的

list(n,val)//n个元素,所有元素都是T(val)出来的

list(n,val,al)//同上,并指定allocator为al

list(first, last) //复制构造

list(first,last,al) //指定allocator构造

二、操作

1.resize & clear

使用resize(n)改变大小,使用resize(n, val)如果需要用T(val) 来填满空闲值。

2.front ()& back()

如果listObj非常量对象,返回是一个左值函数

3.插入操作

insert(iterator it , val)

insert(iterator it, first, last)

insert(iteratot it, n, x)//插入n个x

4.移除

remove(x); //vector.erase(integrator it)

按值删

int iax[] ={3,4,5,6,6,7,8}

        list<int> lObj;

         lObj.insert(lObj.begin(),iax, iax + 7);

         lObj.remove(6); //

按函数条件删

 

#include <iostream>

#include <list>

using namespace std;

// a predicate implemented as a function:

bool single_digit (const int& value) { return (value<10); }

// a predicate implemented as a class:

class is_odd

{

public:

 bool operator() (const int& value) {return (value%2)==1; }

};

int main ()

{

 int myints[]= {15,36,7,17,20,39,4,1};

 list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1

 mylist.remove_if (single_digit);      // 15 36 17 20 39

 mylist.remove_if (is_odd());          // 36 20

 cout << "mylist contains:";

 for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)

    cout << " " << *it;

 cout << endl;

 return 0;

}

当然,对于class is_odd,也可以写成

template <class T>

class is_odd

{

};

调用时,则要改成

mylist.remove_if(is_odd<int>());



原创粉丝点击