RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1208 | Replies: 7 | Thread Tools  Display Modes
Reply
Join Date: Sep 2004
Posts: 420
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

STL question concerning partial_sum?

  #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:
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main()
{
	int a[4] = {10, 20, 30, 40}, b[4];

	partial_sum(a, a+4, b);

	copy(b, b+4, ostream_iterator<int>(cout, " "));
	cout<<endl;

	cout<<"Press any key to continue!\n";
	cin.get();

	return 0;
}

The second one can be used for other algorithmetic reasons, like multiplying for example:
#include <iostream>
#include <numeric>
#include <list>

using namespace std;

int main()
{
	long a[4] = {10, 20, 30, 40}, b[4];

	partial_sum(a, a+4, b, multiplies<long>());

	copy(b, b+4, ostream_iterator<long>(cout, " "));
	cout<<endl;

	cout<<"Press any key to continue!\n";
	cin.get();

	return 0;
}

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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

  #2  
Sep 11th, 2005
why not?
#include<vector>
#include<iostream>
#include<numeric>
using namespace std;
int main()
{
   vector<int> vec;
   for( int i = 0;i<10;++i)
      vec.push_back(i);
   partial_sum(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));
   cout<<endl;
   partial_sum(vec.begin(),vec.end(),ostream_iterator<int>(cout," "),multiplies<int>());
   return 0;
}
Reply With Quote  
Join Date: Sep 2004
Posts: 420
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

  #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  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

  #4  
Sep 11th, 2005
partial_sum is in <numeric>
Reply With Quote  
Join Date: Sep 2004
Posts: 420
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

  #5  
Sep 11th, 2005
That's exactly what was written in my book, the only thing is, if I use
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  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

  #6  
Sep 11th, 2005
oops yeah i forgot that header too in my example. multiplies is in <functional>.
Reply With Quote  
Join Date: Sep 2004
Posts: 420
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: STL question concerning partial_sum?

  #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  
Join Date: Jul 2005
Location: London
Posts: 164
Reputation: Stoned_coder is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Stoned_coder Stoned_coder is offline Offline
Junior Poster

Re: STL question concerning partial_sum?

  #8  
Sep 11th, 2005
i expect so
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 4:07 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC