STL question concerning partial_sum?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

STL question concerning partial_sum?

 
0
  #1
Sep 11th, 2005
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:
  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

 
0
  #2
Sep 11th, 2005
why not?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

 
0
  #3
Sep 11th, 2005
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> :!:
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

 
0
  #4
Sep 11th, 2005
partial_sum is in <numeric>
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

 
0
  #5
Sep 11th, 2005
That's exactly what was written in my book, the only thing is, if I use
  1. partial_sum(a, a+4, b, multiplies<long>());
without <list>, it doesn't work

I get two errors:
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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

 
0
  #6
Sep 11th, 2005
oops yeah i forgot that header too in my example. multiplies is in <functional>.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

 
0
  #7
Sep 11th, 2005
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>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

 
0
  #8
Sep 11th, 2005
i expect so
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC