How to iterate and print Boost Ublas' Vector

Hi,

I tried to print Boost Ublas vector here.
But somehow it gives error. What's the right way to do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Begin
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/assign.hpp>
#include <vector>
using namespace std;
namespace ublas = boost::numeric::ublas;

int main () {
  ublas::vector<double> v1 (10);
  for (unsigned i = 0; i < v1.size (); ++ i){
      v1 (i) = i;
  }
   // This is not I want
   //cout << v1 << endl;

 // I want to print this way instead
 // but give error, how to do it correctly?
  for (int i = 0; v1.size(); i++) {
      cout << v1(i) << endl;
  }
}


The error message I got is this:
1
2
3
4
check failed in file /opt/local/include/boost/numeric/ublas/storage.hpp at line 195:
i < size_
terminate called after throwing an instance of 'boost::numeric::ublas::bad_index'
  what():  bad index
You forgot the comparison, v1.size() is always true.
Topic archived. No new replies allowed.