473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

function returning two values

/* 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
7 2379
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
1
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.