Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > transform, boost::lambda and subscript

Reply
Thread Tools

transform, boost::lambda and subscript

 
 
mojmir
Guest
Posts: n/a
 
      08-30-2008
hello,

i have two vectors and i am combining them into third one:
std::vector<int> orig, result;
std::vector<size_t> perm; // permutations

but i cannot compile following line:
std::transform(perm.begin(), perm.end(), result.begin(), orig[_1]);

because compiler seems to have trouble selecting among:
reindex.cpp:43: error: no match for 'operator[]' in 'orig[_1]'
stl_vector.h:462: candidates are:
_Alloc::reference std::vector<..>:perator[](size_t)
stl_vector.h:476:
_Alloc::const_reference std::vector<..>:perator[](size_t) const

i've tried another form that works
std::transform(perm.begin(), perm.end(), result.begin(),
*(&orig[0]+_1));

can you please help me understand what is going on?

many thanks in advance,
mojmir
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-30-2008
mojmir wrote:

> hello,
>
> i have two vectors and i am combining them into third one:
> std::vector<int> orig, result;
> std::vector<size_t> perm; // permutations
>
> but i cannot compile following line:
> std::transform(perm.begin(), perm.end(), result.begin(), orig[_1]);
>
> because compiler seems to have trouble selecting among:
> reindex.cpp:43: error: no match for 'operator[]' in 'orig[_1]'
> stl_vector.h:462: candidates are:
> _Alloc::reference std::vector<..>:perator[](size_t)
> stl_vector.h:476:
> _Alloc::const_reference std::vector<..>:perator[](size_t) const
>
> i've tried another form that works
> std::transform(perm.begin(), perm.end(), result.begin(),
> *(&orig[0]+_1));
>
> can you please help me understand what is going on?


The problem is caused by the language rule that operator[] must be a member
function. In order to turn an expression involving operator[] into a lambda
expression, the left hand operand needs to be "lambdafied" first. Try:

std::transform(perm.begin(), perm.end(), result.begin(), var(orig)[_1]);


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subscript and superscript shahid C++ 14 02-06-2009 11:11 PM
Vector help, subscript out of range, and basic tutoring yogi_bear_79 C++ 11 03-16-2008 06:33 PM
out_of_range exception and subscript operator subramanian100in@yahoo.com, India C++ 2 08-12-2007 03:56 PM
Big latex subscript using python.sty and manual.cls Bo Peng Python 0 02-15-2005 08:43 PM
Display superscript and subscript text in a label =?Utf-8?B?UmFuaSBQb25tYXRoaQ==?= Microsoft Certification 1 04-14-2004 12:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57