Monday, December 21, 2009

Boost Vector Example

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Boost Vector Example
// Boost Vector operations Resize, Insert, Remove, Erase
// Date : 19 - Jun -2008
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ vector.cpp -o vector -I path_to_boost -lboost_system-gcc43-mt-1_39
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {

using namespace boost::numeric::ublas;
vector<double> v (10);

for (unsigned i = 0; i < v.size (); ++ i)
v (i) = i;

std::cout << v << std::endl;

//Using functions of vector
v.resize(15,1);
std::cout << "nsize increased to 15 "<< v << std::endl;

v.resize(10,1);
std::cout << v << std::endl;

v.insert_element(0,10);
std::cout << "nInserted 10 " << v << std::endl;

v.erase_element(0);
std::cout << "nRemoving " << v << std::endl;

v.clear();
std::cout << "nClearing " << v << std::endl;

}

OUTPUT:
[10](0,1,2,3,4,5,6,7,8,9)

size increased to 15
[15](0,1,2,3,4,5,6,7,8,9,0,0,0,0,0)

[10](0,1,2,3,4,5,6,7,8,9)

Inserted 10
[10](10,1,2,3,4,5,6,7,8,9)

Removing
[10](0,1,2,3,4,5,6,7,8,9)

Clearing
[10](0,0,0,0,0,0,0,0,0,0)

No comments:

Post a Comment