| | |
STL question concerning partial_sum?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
The second one can be used for other algorithmetic reasons, like multiplying for example:
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?
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)
#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:
C++ Syntax (Toggle Plain Text)
#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?
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
why not?
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
partial_sum is in <numeric>
That's exactly what was written in my book, the only thing is, if I use without <list>, it doesn't work 
I get two errors:
C++ Syntax (Toggle Plain Text)
partial_sum(a, a+4, b, multiplies<long>());

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)
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
oops yeah i forgot that header too in my example. multiplies is in <functional>.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Help with a program's error
- Next Thread: "Safe array" assignment
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






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