On Aug 13, 2:42 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-08-13 10:42, arnuld wrote:
>
> >> On Aug 13, 1:40 pm, arnuld <geek.arn...@gmail.com> wrote:
> >> i changed code to this:
>
> >> ........[SNIP].......
>
> >> WHAT IS THIS ?
>
> > i mean why i dis not get 2 values as output ?
>
> C++ only allows one return value, however by using references or
> pointers you can let the function modify variables in the scope of the
> caller, thus simulating more return values.
>
> > BTW, when i try to print "write_temp", i always get 0 (zero).. ???
>
> Probably because you are trying to print it like this:
>
> std::cout << "---------------------------\n"
> << *find_val( begin, end, find_value, write_temp )
> << " " << write_temp << std::endl;
>
> The problem with this statement is that the order of evaluation is
> unspecified, so write_temp can be evaluated before the call to
> find_val(), in which case it will be 0. Make sure to call the function
> and print the value of write_temp in two different statements.
you are right, it works. BTW, i changed the last part of code to
this.
/* a temperaory variable for 4th classic write argument */
std::vector<int>::size_type write_temp = 0;
int value_called = *find_val( begin, end, find_value, write_temp );
/* value stored because we will use it more than once */
std::cout << "------------- you are looking for -----------\n"
<< value_called
<< "\n\n";
std::cout << "number of times "
<< value_called
<< " was found: "
<< write_temp
<< std::endl;
return 0;
}