ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415
Rep:
question abt STL algorithim 'difference'
Consider this piece of C++ code:
Code:
{
int A1[] = {1, 3, 5, 7, 9, 11};
int A2[] = {1, 1, 2, 3, 5, 8, 13};
int N1=5;
int N2=6;
cout << "Difference of A1 and A2: ";
set_difference(A1, A1 + N1, A2, A2 + N2,
ostream_iterator<int>(cout, " "));
}
The the difference is outputted to STDout, I want to difference to be outputted into another set, certainly the ostream_iterator<int> has to redirected to seomthing else, which would assign all the values to a new set, how do I do it?
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
int main()
{
int A1[] = {1, 3, 5, 7, 9, 11};
int A2[] = {1, 1, 2, 3, 5, 8, 13};
std::vector<int> Vout;
int Aout[] = {0,0,0,0,0,0,0};
int N1=5;
int N2=6;
std::cout << "Difference of A1 and A2: ";
std::set_difference(A1, A1 + N1, A2, A2 + N2,
std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
//Insert results into a vector.
std::set_difference(A1, A1 + N1, A2, A2 + N2, std::back_inserter(Vout));
//Copy vector elements into an array.
for(int i=0; i< Vout.size(); ++i)
{
Aout[i] = Vout[i];
}
//Copy array to standard output.
std::copy(Aout, Aout + 7, std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
return 0;
}
The "inserter" iterator adapters are pretty useful in this regard, but they only work with data elements that provide the required methods (e.g. "push_back()" or "push_front()). So I insert into another STL container and copy it to an array (which is presumably what you want. I made the output array as large as the largest input array so I wouldn't overwrite the end.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.