|
Boost : |
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2004-09-08 19:09:58
Stefan Seefeld wrote:
>
>> From: Neal D. Becker [mailto:ndbecker2_at_[hidden]]
>> Sent: September 8, 2004 15:59
>
>> u.begin() should return an iterator, and the iterator should define
>> operator[]. Why doesn't this work?
>
> huh ? Which iterator defines operator[] ? It may define
> operator+(size_type) or similar, if it allows random access, but not
> operator[]. If you know your container manages contiguous memory you may
> use '&*u.begin()' to access the raw memory, and apply your operator[] to
> that.
>
Consider this:
#include <vector>
#include <boost/numeric/ublas/vector.hpp>
template<typename it_t>
int adder (it_t in, int cnt) {
int sum = 0;
for (int i = 0; i < cnt; i++)
sum += in[i];
return sum;
}
int main() {
std::vector<int> a (10);
adder (a.begin(), 10); << this is OK
boost::numeric::ublas::vector<int> b;
adder (b.begin(), 10); << error, why?
}
g++34 -I /usr/local/src/boost.cvs -c Test.cc
Test.cc: In function `int adder(it_t, int) [with it_t =
boost::numeric::ublas::vector<int,
boost::numeric::ublas::unbounded_array<int, std::allocator<int> >
>::iterator]':
Test.cc:17: instantiated from here
Test.cc:8: error: no match for 'operator[]' in 'in[i]'
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk