Tuples Herb Sutter

Source: Internet
Author: User
Tags integer division

As I reported in the previous period, at the 20,020-month standard meeting, two library extensions were passed as standard Couyan deep.

1 is the object wrappers of the polymorphic function proposed by Doug Gregor ' s.

2 Jaakko Järvi ' s proposed tuple type.

Both of these are directly coming in the boost project. (Boost project is a C + + libraries collection) Last time, I promised to introduce these two extensions in this and next issue, this month, let me briefly introduce the tuple type.

Tuple Types: A simple motivating example

If you want to use a function to return more than one return value, for example:

// yields a quotient only
//
int IntegerDivide( int n, int d ) {
return n / d;
}
// Sample use:
cout << "quotient = " << IntegerDivide( 5, 4 );

Are there any errors in this implementation? Perhaps not, after all, in the compiler, we have embedded integer division. Including results can also be rounded.

But if we want to do more. In particular, you want to provide a way to get other information about division, such as the remainder of division. If you do not change the structure of the function. It is not an easy thing to implement the required function.

One way we add an output variable to a function.

// Example 1(b): Integer division,
// yielding a quotient and remainder,
// one as the return value and one via
// an output parameter
//
int IntegerDivide( int n, int d, int& r ) {
r = n % d;
return n / d;
}
// Sample use:
int remainder;
int quotient = IntegerDivide( 5, 4, remainder );
cout << "quotient = " << quotient
<< "remainder = " << remainder;

The implementation of this method is compared, but we often achieve this. This way of returning the return value of a function by returning a value and an output variable seems a little weird. Some might say that the following approach is better.

// Example 1(c): Integer division,
// yielding a quotient and remainder,
// this time via two output parameters
//
void IntegerDivide( int n, int d, int& q, int& r ) {
r = n % d;
q = n / d;
}
// Sample use:
int quotient, remainder;
IntegerDivide( 5, 4, quotient, remainder );
cout << "quotient = " << quotient
<< "remainder = " << remainder;

This approach may be more coordinated. But it is still rather vague and unsatisfactory. A little thought, we'll remember why: Ralph Waldo Emerson advised us: "A dumb idea of consistency is a monster of thought chaos" (a foolish consistency is the hobgoblin of Little Minds). This version works, but I don't blame you if you think it's unstable.

So what should we do? At this point we usually remember that we have a tool in the standard library: std::p air, after all, there are many functions in the standard Template library that can return several values, iterator range is as a separate value-and, mostly, through Pair<iterator,iterator > implemented, the same method can be run as follows:

// Example 1(d): Integer division,
// yielding a quotient and remainder,
// this time both in the return value
//
std::pair<int,int> IntegerDivide( int n, int d ) {
return pair<int,int>( n/d, n%d );
}
// Sample use:
pair<int, int> quot_rem = IntegerDivide( 5, 4 );
cout << "quotient = " << quot_rem.first
<< "remainder = " << quot_rem.second;

It can be seen that this is a satisfactory approach, while it can also be improved.

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.