LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2005, 12:39 PM   #1
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
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?
 
Old 07-30-2005, 04:38 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Code:
#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.
 
Old 07-30-2005, 09:38 PM   #3
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
Thanks, for ur prompt reply
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question abt select() on FC 3 new user Programming 3 11-14-2005 05:45 AM
question abt xine woodland56 Linux - Software 5 10-15-2005 06:11 PM
Question Abt Audio Programming sibtay Programming 1 10-11-2004 12:56 PM
Question abt glibc 2.3.3 ugenn Linux - Software 0 06-04-2004 03:54 AM
a question abt services jayakrishnan Linux - General 0 11-20-2003 11:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration