SGI exclusive power (), iota () method

Source: Internet
Author: User
// File: 6numeric. CPP # include <numeric> # include <vector> # include <fucntional> # include <iostream> # include <iterator> // ostream_iterator # include <algorithm> using namespace STD; int main () {int Ia [5] = {1, 2, 3, 4, 5}; vector <int> IV (IA, Ia + 5); cout <accumulate (IV. begin (), IV. end (), 0) <Endl; // 15, I. e. 0 + 1 + 2 + 3 + 4 + 5 cout <accumulate (IV. begin (), IV. end (), 0, minus <int> () <Endl; //-15, I. e. 0-1-2-3-4-5 cout <inner_product (IV. begin (), IV. end (), IV. begin (), 10) <Endl; // 65, I. E 10 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 cout <inner_product (IV. begin (), IV. end (), IV. begin (), 10, minus <int> (), plus <int> () <Endl; //-20, I. e. 10-1 + 1-2 + 2-3 + 3-4 + 4-5 + 5 // The iterator below will be bound to cout, ostream_iterator <int> oite (cout, ""); partial_sum (IV. begin (), IV. end (), oite); // 1 3 6 10 15 (the nth element is the sum of the First n elements) partial_sum (IV. begin (), IV. end (), oite, minus <int> (); // 1-1-4-8-13 (the nth new element is the total operation of the first n old elements) adjacent_difference (IV. begin (), IV. end (), oite); // 1 1 1 1 1 (#1 element recording, # N new element equals # n old element-# n-1 old element) adjacent_difference (IV. begin (), IV. end (), oite, plus <int> (); // 1 3 5 7 9 (#1 element recording, # N new element equals OP (# n old element, # n-1 old element) cout <power (1000) <Endl; //, I. E 10*10*10 cout <power (10, 3, plus <int> () <Endl; // 30, I. e, 10 + 10 + 10 int n = 3; iota (IV. begin (), IV. end (), n); // enter n, n + 1, n + 2 in the specified range... for (INT I = 0; I <IV. size (); ++ I) cout <IV [I] <''; // 3 4 5 6 7 return 0 ;}

This code is taken from STL source code analysis p298, but cannot be compiled. There are two compilation errors.
\ P298 \ main. cpp | in function 'int main () ': |
\ P298 \ main. cpp | 42 | error: 'power' was not declared in this scope |
\ P298 \ main. cpp | 46 | error: 'iota 'was not declared in this scope |
\ P298 \ main. cpp | 47 | warning: Comparison between signed and unsigned integer expressions |
| === Build finished: 2 errors, 1 warnings ===|
The warning is obvious because the type does not match. If int is converted to size_t, there is no problem. This is not a problem.
First error: power is not declared. We know that the header file of power in C language is math. h. In C ++, we should write # include <cmath>. It is still wrong to add # include <cmath> to the code. Obviously, the power parameter has a similar function, so the header file should not be cmath. As a result, I still have no results after Google, so I am stupid. It is not a function in STL. I found the section of power in the STL source code analysis directory.
As soon as I read the book, I will know:
"The power algorithm is exclusive to SGI and is not listed in the STL standard. It is used to calculate the N power of a certain number ...."
The subsequent section is: "The iota algorithm is exclusive to SGI and is not listed in the STL standard. It is used to set the content of a certain range. Each element in the period starts from the specified value and appears in an ascending State ."

At this time, I realized that this was the case! I still wonder, I have heard of "ITOA" and "iota", and I still suspect that the book is wrong.
Well, since there is no definition, Let's define one. Let's talk about the source code in the book. It will be okay if the source code is exceeded.
The final version of the program is as follows:

// File: 6numeric. CPP # include <numeric> # include <vector> # include <functional> # include <iostream> # include <iterator> // ostream_iterator # include <algorithm> using namespace STD; // version 2, power. If it is specified as a multiplication operation, x ^ N is returned when n> = 0. // note that "monoidoperation" must meet the Associative Law) // but does not need to satisfy the exchange law template <class T, class interger, class monoidoperation> T power (t x, interger N, monoidoperation OP) {If (n = 0) // return identity_elemen T (OP); // retrieve the identity element of the "same identity element" // Zhang Jie Note: The above code error: 'Identity _ element' was not declared in this scope | // it may also be a version issue. Therefore, I directly return X, which is unreasonable, but I have not found a reasonable solution: Return X; else {While (N & 1) = 0) {n> = 1; X = OP (x, x);} t result = x; n >>= 1; while (n! = 0) {x = OP (x, x); If (N & 1 )! = 0) Result = OP (result, x); N >>= 1;} return result ;}// version 1, multiply the power of template <class t, class integer> inline t power (t x, integer N) {return power (x, N, multiplies <t> ()); // specify the calculation type as multiplication} // function significance: Enter value, value + 1, value + 2 template <class forwarditerator in the [first, last) interval, class T> void iota (forwarditerator first, forwarditerator last, T value) {While (first! = Last) * First ++ = value ++;} int main () {int Ia [5] = {1, 2, 4, 5}; vector <int> IV (IA, IA + 5); cout <accumulate (IV. begin (), IV. end (), 0) <Endl; // 15, I. e. 0 + 1 + 2 + 3 + 4 + 5 cout <accumulate (IV. begin (), IV. end (), 0, minus <int> () <Endl; //-15, I. e. 0-1-2-3-4-5 cout <inner_product (IV. begin (), IV. end (), IV. begin (), 10) <Endl; // 65, I. E 10 + 1*1 + 2*2 + 3*3 + 4*4 + 5*5 cout <inner_product (IV. begin (), IV. end (), IV. begin (), 10, minus <int> (), plus <int> () <Endl; //-20, I. e. 10-1 + 1-2 + 2-3 + 3-4 + 4-5 + 5 // The iterator below will be bound to cout, ostream_iterator <int> oite (cout, ""); partial_sum (IV. begin (), IV. end (), oite); cout <Endl; // This line of code is added by Zhang Jie, easy to display // 1 3 6 10 15 (the nth element is the total sum of the First n elements) partial_sum (IV. begin (), IV. end (), oite, minus <int> (); cout <Endl; // This line of code is added by Zhang Jie, ease of display // 1-1-4-8-13 (the nth new element is the total number of operations on the first n old elements) adjacent_difference (IV. begin (), IV. end (), oite); cout <Endl; // This line of code is added by Zhang Jie for ease of display // 1 1 1 1 (#1 element recording, # N new element equals # n old element-# n-1 old element) adjacent_difference (IV. begin (), IV. end (), oite, plus <int> (); cout <Endl; // This line of code is added by Zhang Jie, easy to display // 1 3 5 7 9 (#1 element recording, # N new element equals OP (# n old element, # n-1 old element) cout <power) <Endl; // 1000, I. E 10*10*10 cout <power (10, 3, plus <int> () <Endl; // 30, I. e, 10 + 10 + 10 int n = 3; iota (IV. begin (), IV. end (), n); // enter n, n + 1, n + 2 in the specified range... for (INT I = 0; I <IV. size (); ++ I) cout <IV [I] <''; // 3 4 5 6 7 cout <Endl; // This line of code is added by Zhang Jie, easy to display return 0 ;}
Note:
1. I have commented on the above Code. Other codes are all taken from Hou Jie's STL source code analysis.
2. The above code is compiled and run using code: blocks. Other ide results may vary, probably because STL versions are different. End of this Article Reprinted to indicate the source. Thank you. 2010-08-19

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.