C++/Deque/deque subscript indexer
Use indexer to add elements to a deque
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
int main()
{
deque<int> deque1(5);
for (int i = 0; i < 5; ++i)
deque1[i] = i + 5;
deque<int>::iterator pos;
for (pos=deque1.begin(); pos!=deque1.end(); ++pos) {
cout << *pos << " ";
}
return 0;
}
/*
5 6 7 8 9
*/
Use subscript operator to modify element at location 1
#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 );
// use subscript operator to modify element at location 1
values[ 1 ] = 5.4;
cout << "\nAfter values[ 1 ] = 5.4, values contains: ";
std::copy( values.begin(), values.end(), output );
cout << endl;
return 0;
}
/*
values contains: 3.5 2.2 1.1
After values[ 1 ] = 5.4, values contains: 3.5 5.4 1.1
*/
Use subscript operator to obtain elements of values
#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
*/