C++/Deque/deque
Содержание
- 1 Combine insert and end to add elements to a deque
- 2 create a deque
- 3 Create your own stack based on deque
- 4 deque.push_back( value )
- 5 deque.push_front( value )
- 6 Initialize deque with 26 copies of the letter x
- 7 Save transformed data from vector to deque (Vector1 + Vector2 = Result (in Deque))
- 8 Use generic deque to store chars
- 9 Use generic deque to store integers
- 10 Use generic deque to store strings
- 11 Use std::copy to print out all elements in a deque
Combine insert and end to add elements to a deque
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <list>
#include <deque>
#include <vector>
using namespace std;
int main()
{
deque<string> deq;
deq.insert(deq.end(), "AAA");
deq.insert(deq.end(), "DDDDDD");
deque<string>::iterator pos;
for (pos=deq.begin(); pos!=deq.end(); ++pos) {
cout << *pos << " ";
}
return 0;
}
/*
AAA DDDDDD
*/
create a deque
#include <iostream>
using std::cout;
using std::endl;
#include <deque> // deque class-template definition
#include <algorithm> // copy algorithm
#include <iterator> // ostream_iterator
int main()
{
std::deque< double > values; // create deque of doubles
std::ostream_iterator< double > output( cout, " " );
// insert elements in values
values.push_front( 2.2 );
values.push_front( 3.5 );
values.push_back( 1.1 );
cout << "values contains: ";
std::copy( values.begin(), values.end(), output );
values.pop_front(); // remove first element
cout << "\nAfter pop_front, values contains: ";
std::copy( values.begin(), values.end(), output );
cout << endl;
return 0;
}
/*
values contains: 3.5 2.2 1.1
After pop_front, values contains: 2.2 1.1
*/
Create your own stack based on deque
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <deque>
#include <exception>
using namespace std;
template <class T>
class Stack {
protected:
std::deque<T> c; // container for the elements
public:
/* exception class for pop() and top() with empty stack
*/
class ReadEmptyStack : public std::exception {
public:
virtual const char* what() const throw() {
return "read empty stack";
}
};
// number of elements
typename std::deque<T>::size_type size() const {
return c.size();
}
// is stack empty?
bool empty() const {
return c.empty();
}
// push element into the stack
void push (const T& elem) {
c.push_back(elem);
}
// pop element out of the stack and return its value
T pop () {
if (c.empty()) {
throw ReadEmptyStack();
}
T elem(c.back());
c.pop_back();
return elem;
}
// return value of next element
T& top () {
if (c.empty()) {
throw ReadEmptyStack();
}
return c.back();
}
};
int main()
{
try {
Stack<int> st;
// push three elements into the stack
st.push(1);
st.push(2);
st.push(3);
// pop and print two elements from the stack
cout << st.pop() << " ";
cout << 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 three elements
* - ERROR: one element too many
*/
cout << st.pop() << " ";
cout << st.pop() << endl;
cout << st.pop() << endl;
}
catch (const exception& e) {
cerr << "EXCEPTION: " << e.what() << endl;
}
}
/*
3 2 4 77
EXCEPTION: read empty stack
*/
deque.push_back( value )
#include <iostream>
using std::cout;
using std::endl;
#include <deque> // deque class-template definition
#include <algorithm> // copy algorithm
#include <iterator> // ostream_iterator
int main()
{
std::deque< double > values; // create deque of doubles
std::ostream_iterator< double > output( cout, " " );
values.push_front( 2.2 );
values.push_front( 3.5 );
values.push_back( 1.1 );
cout << "values contains: ";
// use subscript operator to obtain elements of values
for ( int i = 0; i < values.size(); i++ )
cout << values[ i ] << " ";
cout << endl;
return 0;
}
/*
values contains: 3.5 2.2 1.1
*/
deque.push_front( value )
#include <iostream>
using std::cout;
using std::endl;
#include <deque> // deque class-template definition
#include <algorithm> // copy algorithm
#include <iterator> // ostream_iterator
int main()
{
std::deque< double > values; // create deque of doubles
std::ostream_iterator< double > output( cout, " " );
values.push_front( 2.2 );
values.push_front( 3.5 );
values.push_back( 1.1 );
cout << "values contains: ";
// use subscript operator to obtain elements of values
for ( int i = 0; i < values.size(); i++ )
cout << values[ i ] << " ";
cout << endl;
return 0;
}
/*
values contains: 3.5 2.2 1.1
*/
Initialize deque with 26 copies of the letter x
#include <iostream>
#include <cassert>
#include <list>
#include <deque>
#include <algorithm> // For merge
using namespace std;
int main()
{
// Initialize deque1 with 26 copies of the letter x:
deque<char> deque1(26, "x");
deque<char>::iterator i;
cout.precision(10);
for (i = deque1.begin(); i != deque1.end(); ++i)
cout << *i << endl;
return 0;
}
/*
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
x
*/
Save transformed data from vector to deque (Vector1 + Vector2 = Result (in Deque))
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <functional>
using namespace std;
int main ()
{
vector <int> v1, v2;
for (int nNum = 0; nNum < 10; ++ nNum){
v1.push_back (nNum);
v2.push_back (10 - nNum);
}
deque <int> d (v1.size ());
transform ( v1.begin (), v1.end (), v2.begin (), d.begin (), plus <int> () );
for (size_t nIndex = 0; nIndex < v1.size (); ++ nIndex){
cout << nIndex << " \t " << v1 [nIndex];
cout << v2 [nIndex] << " \t = ";
cout << d [nIndex] << endl;
}
return 0;
}
Use generic deque to store chars
#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm> // For find
using namespace std;
int main()
{
char x[5] = {"a", "r", "e", "q", "t"};
deque<char> deque1(&x[0], &x[5]);
// Search for the first occurrence of the letter e:
deque<char>::iterator where = find(deque1.begin(), deque1.end(), "e");
assert (*where == "e" );
cout << "Ok." << endl;
return 0;
}
/*
Ok.
*/
Use generic deque to store integers
#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm> // For find
using namespace std;
int main()
{
int x[5] = {1, 2, 3, 4, 5};
deque<int> deque1(&x[0], &x[5]);
// Search for the first occurrence of the letter e:
deque<int>::iterator where = find(deque1.begin(), deque1.end(), 1);
cout << *where << endl;
return 0;
}
/*
1
*/
Use generic deque to store strings
#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm> // For find
using namespace std;
int main()
{
string x[5] = {"1234", "2345", "3456","4567", "5678"};
deque<string> deque1(&x[0], &x[5]);
// Search for the first occurrence of the letter e:
deque<string>::iterator i;
cout.precision(10);
for (i = deque1.begin(); i != deque1.end(); ++i)
cout << *i << endl;
return 0;
}
/*
1234
2345
3456
4567
5678
*/
Use std::copy to print out all elements in a deque
#include <iostream>
using std::cout;
using std::endl;
#include <deque> // deque class-template definition
#include <algorithm> // copy algorithm
#include <iterator> // ostream_iterator
int main()
{
std::deque< double > values; // create deque of doubles
std::ostream_iterator< double > output( cout, " " );
// insert elements in values
values.push_front( 2.2 );
values.push_front( 3.5 );
values.push_back( 1.1 );
cout << "values contains: ";
std::copy( values.begin(), values.end(), output );
values.pop_front(); // remove first element
cout << "\nAfter pop_front, values contains: ";
std::copy( values.begin(), values.end(), output );
cout << endl;
return 0;
}
/*
values contains: 3.5 2.2 1.1
After pop_front, values contains: 2.2 1.1
*/