By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

function returning two values

P: n/a
/* C++ Primer - 4/e
*
* 1st example from section 7.2.2, page 234
* returning 2 values from a function
*
* STATEMENT:
* to find a specific value in a vector and number of times
* that value occurs in th vector.
*/
#include <iostream>
#include <vector>

/* returns an iterator that refers to the first occurence of
"find_this_value"
the reference paramater "occurs" contains a second return value, the
number of times "find_this_value" occured in the vector
*/
std::vector<int>::const_iterator find_val(
std::vector<int>::const_iterator beg,
std::vector<int>::const_iterator end,
int find_this_value,
std::vector<int>::size_type& occurs)
{
/* res_iter will hold the first occurence, if any */
std::vector<int>::const_iterator res_iter = end;
occurs = 0;

for( ; beg != end; ++beg)
{
if ( *beg == find_this_value )
{
if ( res_iter == end ) /* this will remeber the 1st occurence */
{
res_iter = beg;
}
++occurs;
}
}

return res_iter;
}
int main()
{
std::cout << "enter some numbers to creat a vector : ";
std::vector<intivec;
int i;
while(std::cin >i)
{
ivec.push_back(i);
}

/* clear the input stream */
std::cin.clear();
std::cout << "which numbe you want to find: ";
int find_value;
std::cin >find_value;

std::vector<int>::const_iterator begin = ivec.begin();
std::vector<int>::const_iterator end = ivec.end();

find_val(begin, end, find_value);

return 0;
}

as expected this function never compiles because function call expects
the 4 arguments whereas 3 are given. but how i am supposed to provide
4th argument when what i want to find out is the 4th argument and the
author says that 4th argument is the 2nd return value. i am confused
on this

(BTW, it seems like circular-dependencies problem of emerge)

Aug 13 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
arnuld wrote:

<snip>
>
std::vector<int>::const_iterator begin = ivec.begin();
std::vector<int>::const_iterator end = ivec.end();
std::vector<int>::size_type occurs;

find_val(begin, end, find_value, occurs);
find_val(begin, end, find_value);

return 0;
}

as expected this function never compiles because function call expects
the 4 arguments whereas 3 are given. but how i am supposed to provide
4th argument when what i want to find out is the 4th argument and the
author says that 4th argument is the 2nd return value. i am confused
on this
Just add it!

--
Ian Collins.
Aug 13 '07 #2

P: n/a
On Aug 13, 1:09 pm, Ian Collins <ian-n...@hotmail.comwrote:
std::vector<int>::size_type occurs;
.......[SNIP].............
find_val(begin, end, find_value, occurs);
..............[SNIP]................
Just add it!
added, it compiles and runs and i modified last 2 lines of main() to
print the output to terminal but still no luck :(

/* C++ Primer - 4/e
*
* 1st example from section 7.2.2, page 234
* returning 2 values from a function
*
* STATEMENT:
* to find a specific value in a vector and number of times
* that value occurs in th vector.
*/
#include <iostream>
#include <vector>

/* returns an iterator that refers to the first occurence of
"find_this_value"
the reference paramater "occurs" contains a second return value, the
number of times "find_this_value" occured in the vector
*/
std::vector<int>::const_iterator find_val(
std::vector<int>::const_iterator beg,
std::vector<int>::const_iterator end,
int find_this_value,
std::vector<int>::size_type& occurs)
{
/* res_iter will hold the first occurence, if any */
std::vector<int>::const_iterator res_iter = end;
occurs = 0;

for( ; beg != end; ++beg)
{
if ( *beg == find_this_value )
{
if ( res_iter == end ) /* this will remeber the 1st occurence */
{
res_iter = beg;
}
++occurs;
}
}

return res_iter;
}
int main()
{
std::cout << "enter some numbers to creat a vector : ";
std::vector<intivec;
int i;
while(std::cin >i)
{
ivec.push_back(i);
}

/* clear the input stream */
std::cin.clear();
std::cout << "which numbe you want to find: ";
int find_value;
std::cin >find_value;

std::vector<int>::const_iterator begin = ivec.begin();
std::vector<int>::const_iterator end = ivec.end();

/* a temperaory variable for 4th classic write argument */
std::vector<int>::size_type write_temp = 0;

std::cout << "---------------------------\n";
find_val(begin, end, find_value, write_temp);

std::cout << std::endl;

return 0;
}
Aug 13 '07 #3

P: n/a
i changed code to this:

/* C++ Primer - 4/e
*
* 1st example from section 7.2.2, page 234
* returning 2 values from a function
*
* STATEMENT:
* to find a specific value in a vector and number of times
* that value occurs in th vector.
*/
#include <iostream>
#include <vector>

/* returns an iterator that refers to the first occurence of
"find_this_value"
the reference paramater "occurs" contains a second return value, the
number of times "find_this_value" occured in the vector
*/
std::vector<int>::const_iterator find_val(
std::vector<int>::const_iterator beg,
std::vector<int>::const_iterator end,
int find_this_value,
std::vector<int>::size_type& occurs)
{
/* res_iter will hold the first occurence, if any */
std::vector<int>::const_iterator res_iter = end;
occurs = 0;

for( ; beg != end; ++beg)
{
if ( *beg == find_this_value )
{
if ( res_iter == end ) /* this will remeber the 1st occurence */
{
res_iter = beg;
}
++occurs;
}
}

return res_iter;
}
int main()
{
std::cout << "enter some numbers to creat a vector : ";
std::vector<intivec;
int i;
while(std::cin >i)
{
ivec.push_back(i);
}

/* clear the input stream */
std::cin.clear();
std::cout << "which numbe you want to find: ";
int find_value;
std::cin >find_value;

std::vector<int>::const_iterator begin = ivec.begin();
std::vector<int>::const_iterator end = ivec.end();

/* a temperaory variable for 4th classic write argument */
std::vector<int>::size_type write_temp = 0;

std::cout << "---------------------------\n"
<< *find_val( begin, end, find_value, write_temp )
<< std::endl;

return 0;
}
========== OUTPUT ===========
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra 7.2.2_return-two-
values.cpp
~/programming/cpp $ ./a.out
enter some numbers to creat a vector : 1 2 3 4 5 5 0 2 3 2 5 9
which numbe you want to find: 2
---------------------------
2
~/programming/cpp $ ./a.out
enter some numbers to creat a vector : 1 2 20 9 8 0 6 20 20 98
which numbe you want to find: 20
---------------------------
20
~/programming/cpp $

WHAT IS THIS ?

Aug 13 '07 #4

P: n/a
On Aug 13, 1:40 pm, arnuld <geek.arn...@gmail.comwrote:
i changed code to this:
........[SNIP].......
WHAT IS THIS ?
i mean why i dis not get 2 values as output ?

BTW, when i try to print "write_temp", i always get 0 (zero).. ???

Aug 13 '07 #5

P: n/a
arnuld wrote:
std::vector<int>::size_type write_temp = 0;

std::cout << "---------------------------\n";
find_val(begin, end, find_value, write_temp);

std::cout << std::endl;
std::cout << write_temp << std::endl;

It helps if you output the value!

--
Ian Collins.
Aug 13 '07 #6

P: n/a
On Aug 13, 10:05 am, arnuld <geek.arn...@gmail.comwrote:
/* returns an iterator that refers to the first occurence of
"find_this_value"
the reference paramater "occurs" contains a second return value, the
number of times "find_this_value" occured in the vector
*/
std::vector<int>::const_iterator find_val(
std::vector<int>::const_iterator beg,
std::vector<int>::const_iterator end,
int find_this_value,
std::vector<int>::size_type& occurs)
{
/* res_iter will hold the first occurence, if any */
std::vector<int>::const_iterator res_iter = end;
occurs = 0;

for( ; beg != end; ++beg)
{
if ( *beg == find_this_value )
{
if ( res_iter == end ) /* this will remeber the 1st occurence */
{
res_iter = beg;
}
++occurs;
}
}
return res_iter;
}
You seem to have gotten most of it, but I'd certainly consider
defining a structure and returning it:

struct Results
{
std::vector< int >::const_iterator
firstPos ;
size_t count ;
} ;

Results
find_val( // ...

Which solution is more appropriate depends on the actual
application. If you always need both return values, the
structure (or even a full fledged class) is often the most
appropriate. But there are cases where the principal role of
the function is to find one of the values, and the second is
just an added feature. In such cases, it is traditional to use
a pointer for the added feature return value; if the user passes
a null pointer (which can be the default value), you don't
return this additional value.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 13 '07 #7

P: n/a
In article <11*********************@o61g2000hsh.googlegroups. com>,
ja*********@gmail.com says...

[ ... ]
You seem to have gotten most of it, but I'd certainly consider
defining a structure and returning it:

struct Results
{
std::vector< int >::const_iterator
firstPos ;
size_t count ;
} ;
When you're dealing with exactly two values, another way to define the
struct would be something like this:

std::pair<std::vector<int>::const_iterator, size_t>

Which is more appropriate will depend on how you're using the returned
value. For one example, std::pair is handy in quite a bit of template
code, because the names of the two members are pre-defined, making it
easier for some code to deal with pairs of essentially any two types.
OTOH, 'x.count' will usually be more informative than 'x.second'.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.