This page presents a simpe example of using a double
ended STL container.
It is implemented as part of a c++
series of exercises
, that I did.
-
The example has simple class, with two fields, float and
a string. First the deque
is filled and then printed.
In this example an integer is converted to string using a
ostringstream.
- #include <iostream>
- //setw setfill
- #include <iomanip>
- #include <sstream>
- #include <string>
- #include <cassert>
- #include <deque>
- using namespace std;
- class c_block_member {
- public:
- c_block_member(string name, float f);
- string nm;
- float fp;
- };
- c_block_member::c_block_member(string name, float f) {
- nm=name.substr(0);
- fp=f;
- }
- int main() {
- deque<c_block_member> q_block_member;
- c_block_member *bm;
- unsigned i;
- ostringstream os_inst;
- string s_inst;
- //build deque
- for(i=1; i<=10; ++i) {
- //default fill is space
- os_inst << "U_" << setfill ('0') << setw (8) << i;
- s_inst=os_inst.str();
- os_inst.str(""); os_inst.clear();
- bm = new c_block_member(s_inst, i*(1.2));
- q_block_member.push_back(*bm);
- }
- delete bm;
- //print deque
- deque<c_block_member>::iterator it;
- cout << "dbg " << endl;
- for (it = q_block_member.begin(); it!=q_block_member.end(); ++it) {
- cout << it->nm << ";" << (*it).fp << endl;
- }
- }
-
To view the content of the deque container, the following gdb commands can
be used:
p *(q_block_member._M_impl._M_start)
p *(q_block_member._M_impl._M_finish)
print "%f\n", *(q_block_member._M_impl._M_start)
print "%f\n", (q_block_member._M_impl._M_start)[0]
print "%f\n", (q_block_member._M_impl._M_start)[1]
|