STL中特殊容器

  • Stacks(堆栈)

        class stack<>实作出一个stack(后进先出),为了运用stack必须先包含头文件<stack>

#include<stack>

在头文件中,class stack定义如下

namespace std{
        template<class T,
        class Container=deque<T>>class stack;
}

 第一个参数template参数代表元素类型。带有默认值的第二个template参数用来定义stack内部存放元素所用的实际容器,缺省使用deque。

下面这个声明就定义了一个元素类型为整数的stack

stack<int> st;

Stacks的核心接口就是三个成员函数push(),top(),pop():

push()会将一个元素置入stack内

top()会返回stack内的“下一个元素”

pop()会从stack中移除元素

下面展示stack的用法

#include<iostream>
#include<stack>
using namespace std;
int main()
{
	stack<int> st;

	//push three elements into stack
	st.push(1);
	st.push(2);
	st.push(3);

	//pop and print two elements from the stack
	cout << st.top() <<' ';
	st.pop();
	cout << st.top() << ' ';
	st.pop();

	//modify top element
	st.top() = 77;

	//push two new elements
	st.push(4);
	st.push(5);

	//pop one element without processing it
	st.pop();

	//pop and print remaining elements
	while (!st.empty())
	{
		cout << st.top() << " ";
		st.pop();
	}
	cout << endl;
}

最终运行结果如下 

  • Queues(队列)

        class queue<> 实作出一个queue(先进先出)。可以使用push()将任意数量的元素置入queue中,也可以使用pop()将元素依次移除。

为了运用queue,必须先包含头文件<queue>,在头文件中,class queue定义如下:

namespace std{
        template <class T,
        class Container=deque<T>>class queue;
}

第一个template参数代表元素型别,带有默认值的第二个template参数用来定义queue内部存放元素用的实际容器,缺省采用deque。

下面定义了一个内含字符串的queue

queue<string> buffer;

Queues的核心接口主要由成员函数push(),front(),back(),pop()构成 

push()会将一个元素置入queue中;

front()会返回queue内第一个被置入的元素;

back()会返回queue中最后一个元素;

pop()会从queue中移除一个元素;

queue运用示例

#include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
	queue<string> q;

	//insert three elements into the queue
	q.push("These ");
	q.push("are ");
	q.push("more than ");

	//read and print two elements from the queue
	cout << q.front();
	q.pop();
	cout << q.front();
	q.pop();

		// These are
	


	//insert two new elements
	q.push("four ");
	q.push("words! ");

	//skip one element
	q.pop();

	//read and print two elements
	cout << q.front();
	q.pop();
	cout << q.front() << endl;
	q.pop();

	//print number of elements in the queue
	cout << "number of elements in the queue: " << q.size() << endl;
}

 

  • Priority Queues(优先队列)

        class priority_queue<>实作出一个queue,其中的元素根据优先级被读取。它的接口和queues非常接近。但是要注意的是priority queue中的元素已经根据其值进行了排序,你可以通过template参数指定一个排序准则,缺省的排序准则是利用operator<形成降序排列。

priority queue和一般的queue都定义于头文件<queue>,其中 class priority_queue定义如下:

namespace std{
    template <class T,
            class Container=vector<T>,
            class Compare=less<typename Container::value_type>>calss priority_queue;
}

第一个template参数是元素型别,带有默认值的第二个template参数定义了priority queue内部用来存放元素的容器,缺省容器是vector,带有默认值的第三个template参数定义出“用于寻找下一个最高优先元素”的排序准则,缺省情况是以operator<作为比较标准

下面这个例子定义了一个元素型别为float 的priority queue:

priority_queue<float> pbuffer;

如果需要定义自己的排序准则,就必须传递一个函数(或仿函数)作为二元判断式,用以比较两个元素并以此作为排序准则,例如

priority_queue<float,std::vector<float>,std::greater<float>>pbuffer;

priority queue的核心接口主要由成员函数push(),top(),pop()组成

push()会将一个元素置入priority queue中;

top()会返回priority queue中的下一个元素;

pop()会从priority queue中移除一个元素;

priority queue运用示例

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	priority_queue<float> q;
	//insert three elements into the priority queue
	q.push(66.6);
	q.push(22.2);
	q.push(44.4);

	//read and print two elements
	cout << q.top() << " ";
	q.pop();
	cout << q.top() << endl;
	q.pop();

	    //result :66.6 44.4

	//insert three more elements
	q.push(11.1);
	q.push(55.5);
	q.push(33.3);

	//skip one element
	q.pop();

	//pop and print remaining elements
	while (!q.empty())
	{
		cout << q.top() << " ";
		q.pop();
	}
	cout << endl;

}

 在元素 66.6,22.2,44.4被置入后,程序打印出优先级最高的元素66.6和44.4,另外三个元素被置入以后,priority queue内含22.2 ,11.1 ,55.5 ,33.3(按照插入次序),下一个元素被pop()掉了,所以最后一个循环依次打印出33.3 ,22.2 , 11.1。

注意这里把元素放进去之后,它内部的存储顺序并不是按照插入顺序来的,而是按照第三个template参数来的。

  • Bitsets

        Bitsets造出一个内含位(bits)或布尔(Boolean)值且大小固定的array。当你需要管理各式标志,并以标志的任意组合来表现变量时,就可运用bitsets。

class bitset定义于头文件<bitset>之中,它是一个template class,有一个template参数,用来指定位的数量:

namespace std{
        template <size_t Bits>
        class bitset;
}

bitset 运用示例 

#include<bitset>
#include<iostream>
#include<string>
#include<limits>
using namespace std;
int main()
{
	//print some numbers in binary representation
	cout << "267 as binary short: "
		<< bitset<numeric_limits<unsigned short>::digits>(267) << endl;
	cout << "267 as binary long: "
		<< bitset<numeric_limits<unsigned long>::digits>(267) << endl;
	cout << "10000000 with 24 bits: "
		<< bitset<24>(10000000) << endl;
}

例子中的 

bitset<numeric_limits<unsigned long>::digits>(267) 

将数值267转换成一个bitset,其位数与型别unsigned long 相符。 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值