20071029

C++: STL vector<>::front/begin() and vector<>::back/end() methods.

// vector::front - Returns reference to first element of vector.
// vector::back - Returns reference to last element of vector.
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
// vector::size - Returns number of elements in the vector.
// vector::begin - Returns an iterator to start traversal of the vector.
// vector::end - Returns an iterator for the last element of the vector.
// vector::erase - Deletes elements from a vector (single & range).

vector<int> vec;

// Intialize the array to contain the members [100, 200, 300, 400]
for (int i=0; i<4; i++)
vec.push_back((i + 1) * 100);

cout << "First element: " << vec.front() << endl;
cout << "Last element: " << vec.back() << endl;
cout << "Elements in vector: " << vec.size() << endl;

// Delete the last element of the vector. Remember that the vector
// is 0-based, so theVector.end() actually points 1 element beyond the end.
vec.erase(vec.end() - 1);

cout << endl << "After erasing last element, new last element is: " << vec.back() << endl;

// Delete the first element of the vector.
vec.erase(vec.begin());

cout << "After erasing first element, new first element is: "
<< vec.front() << endl;

cout << "Elements in vector: " << vec.size() << endl;
// Insert an element (number -11) in front of the vector.
vec.insert(vec.begin(),-11);
link: Visual C++ Developer Center / vector::front and vector::back

C++ Classes: Overloading ostream/istream operators, defining copy ctor, assignment operator, and operator +.

/*-------------------------------------------------------*/
file obj_test.h:
/*-------------------------------------------------------*/
#ifndef __OBJ_TEST_H
#define __OBJ_TEST_H

#include <vector>
#include <string>

using namespace std;

class obj_test {
public:
obj_test(void);
obj_test(vector<float> samples): _samples(samples) { };
~obj_test(void);

// copy constructor
obj_test(const obj_test& obj);
// assignment operator
obj_test& operator =(const obj_test& rhs);
// + operator
obj_test operator +(const obj_test& rhs);

friend ostream& operator <<(ostream& os, obj_test &test);
friend istream& operator >>(istream &is, obj_test &test);

private:
// temp samples
vector<float> _samples;

};

ostream& operator <<(ostream &os, obj_test &test);
istream& operator >>(istream &is, obj_test &test);

#endif
/* __OBJ_TEST_H */
/*-------------------------------------------------------*/
file obj_test.cpp:
/*-------------------------------------------------------*/
#include "test.h"

obj_test::obj_test(void) {
}


// copy constructor
obj_test::obj_test(const obj_test& obj): _samples(obj._samples) { }

// assignment operator
obj_test& obj_test::operator =(const obj_test& rhs) {

// guard against self-assignment Stanley B. Lippman C++ Primer p.729
if (this!=&rhs) {
_samples=rhs._samples;
}

return *this;
}

obj_test obj_test::operator +(const obj_test& rhs) {
obj_test test;

_samples.insert(_samples.end(),rhs._samples.begin(),rhs._samples.end());
test._samples=_samples;

return test;
}

obj_test::~obj_test(void) {
_samples.clear();
}


ostream& operator <<(ostream& os, obj_test &test) {

os << "samples: " << endl;

copy(test._samples.begin(),test._samples.end(),ostream_iterator<float>(os,"\n"));

return os;
}

istream& operator >>(istream &is, obj_test &test) {
string l;
getline(is,l);

float sample;
while (is >> sample)
test._samples.push_back(sample);

return is;
}
/*-------------------------------------------------------*/
file obj_test_driver.cpp:
/*-------------------------------------------------------*/
#include <iostream>
#include <sstream>
#include "test.h"

using namespace std;

void main() {

vector <float> samples;

samples.push_back(float(1.4));
samples.push_back(float(3));
samples.push_back(float(5.6));

cout << "test obj intialized with vector<float> samples:" << endl << endl;
obj_test test(samples);

cout << test << endl;

stringstream ss;

ss << test;

obj_test test_clone;

ss >> test_clone;

cout << "test clone should be the same with test:" << endl;
cout << test_clone;

getchar();

return;
}

eggs.in.art (my non-technical blog)