Opened 16 years ago

Closed 16 years ago

#267 closed task (fixed)

There is a problem with vector sizes in this code snippet

Reported by: Jari Häkkinen Owned by: Jari Häkkinen
Priority: major Milestone: 0.6.4
Component: core Version: 0.6.3
Keywords: Cc:


template <typename T >
struct VectorPlus : public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
	std::vector<T> operator()(const std::vector<T>& u, const std::vector<T>& v) const 
		if ( u.size() > v.size() ){
			std::vector<T> res(u.size());
			transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
			copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
			return res;
		std::vector<T> res(v.size());
		transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
		if ( v.size() > u.size() )
			copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
		return res;


Change History (7)

comment:1 Changed 16 years ago by Jari Häkkinen

Status: newassigned

comment:2 Changed 16 years ago by Jari Häkkinen

Version: trunk0.6.3

This is also a problem in the trunk. I'll fix it manually, i.e., no merge will be done since utility.h has been split in the trunk.

comment:3 Changed 16 years ago by Jari Häkkinen

Fixed in changeset:478 for the 0.6 branch.

comment:4 Changed 16 years ago by Peter Johansson

Just a comment on this code. In my taste it is preferable to use reserve and back_inserter iterator rather than resize and normal iterator.

That is irrelavant for this bug, though.

comment:5 Changed 16 years ago by Jari Häkkinen

There is no resize here. The res vector is allocated in proper size directly. However, using the back_inserter may be somewhat quicker since the res vector get initialized and later on copied.

comment:6 Changed 16 years ago by Peter Johansson

Yes, sorry, my point is that the default constructor for T is called u.size() times, which could be avoided using a back_inserter. But you already understood that.

comment:7 Changed 16 years ago by Peter Johansson

Resolution: fixed
Status: assignedclosed

(In [485]) Fixes #267 in trunk. This ChangeSet?? should be equivalent with [478], but using a back_insert_iterator to speed up it slightly.

Note: See TracTickets for help on using tickets.