944,193 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1749
  • C++ RSS
Sep 11th, 2005
0

STL question concerning partial_sum?

Expand Post »
Hello ladies and gents,

I'm reading about the algorithm partial_sum and there are two different versions of this.

The first one just calculates cumulative numbers like this:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <numeric>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int a[4] = {10, 20, 30, 40}, b[4];
  10.  
  11. partial_sum(a, a+4, b);
  12.  
  13. copy(b, b+4, ostream_iterator<int>(cout, " "));
  14. cout<<endl;
  15.  
  16. cout<<"Press any key to continue!\n";
  17. cin.get();
  18.  
  19. return 0;
  20. }

The second one can be used for other algorithmetic reasons, like multiplying for example:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <numeric>
  3. #include <list>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. long a[4] = {10, 20, 30, 40}, b[4];
  10.  
  11. partial_sum(a, a+4, b, multiplies<long>());
  12.  
  13. copy(b, b+4, ostream_iterator<long>(cout, " "));
  14. cout<<endl;
  15.  
  16. cout<<"Press any key to continue!\n";
  17. cin.get();
  18.  
  19. return 0;
  20. }

The questions I have though are:

1) I can't seem to be able to use the second version with #include <vector>, why is that?
2) How can I know wich header file is exactly needed for this?
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

why not?
C++ Syntax (Toggle Plain Text)
  1. #include<vector>
  2. #include<iostream>
  3. #include<numeric>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> vec;
  8. for( int i = 0;i<10;++i)
  9. vec.push_back(i);
  10. partial_sum(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));
  11. cout<<endl;
  12. partial_sum(vec.begin(),vec.end(),ostream_iterator<int>(cout," "),multiplies<int>());
  13. return 0;
  14. }
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

Thanks for your example Stoned_coder, but that wasn't really my question

What I want to know is wich is the correct header file for using partial_sum, <list> or <vector>, but, I think Ive got it since I'm able to use <list> for both versions and not <vector> :!:
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

partial_sum is in <numeric>
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

That's exactly what was written in my book, the only thing is, if I use
C++ Syntax (Toggle Plain Text)
  1. partial_sum(a, a+4, b, multiplies<long>());
without <list>, it doesn't work

I get two errors:
Quote ...
Compiling...
main.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\LAVbBoek\main.cpp(13) : error C2065: 'multiplies' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\LAVbBoek\main.cpp(13) : error C2062: type 'long' unexpected
Error executing cl.exe.

main.obj - 2 error(s), 0 warning(s)
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

oops yeah i forgot that header too in my example. multiplies is in <functional>.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

Ah, ok thanks, that worked Strange that it isn't mentioned in the book though, they only talk about <numeric> :rolleyes:

Does the example work because <list> contains <functional>
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Sep 11th, 2005
0

Re: STL question concerning partial_sum?

i expect so
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with a program's error
Next Thread in C++ Forum Timeline: "Safe array" assignment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC