473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

output iterators

Hi all,

I have a homework problem here, where I have to perform a set
intersection on two sets and use the result elsewhere. I have this
code:

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4);
set_intersection(set1.begin(), set1.end(), set2.begin(),
set2.end(), ostream_iterator<int>(cout, " ")); //I want to do something
else here
return 0;
}

As I said, I dont want to simply output the intersection, but store the
result in an array. The SGI stl reference doesnt seem to provide any
support for this, and even goes on to say that output iterators cant be
read. How do I do it?

Thanks in advance,
anirudh

Jul 23 '05 #1
6 1736
Anirudh Ramachandran wrote:

As I said, I dont want to simply output the intersection, but store the
result in an array.


Begin by defining an array. Then use the same technique you used to read
from your arrays: pass the address of the first element.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

Pete Becker wrote:
Anirudh Ramachandran wrote:

As I said, I dont want to simply output the intersection, but store the result in an array.

Begin by defining an array. Then use the same technique you used to

read from your arrays: pass the address of the first element.


I'm sorry to sound stupid, but how do I do that? The final argument for
the set_intersection function is of type ostream_iterator<T>(ostream&)
or something of the sort. How can I copy the function to a locally
defined array?

anirudh

Jul 23 '05 #3
Anirudh Ramachandran wrote:

I'm sorry to sound stupid, but how do I do that? The final argument for
the set_intersection function is of type ostream_iterator<T>(ostream&)
or something of the sort. How can I copy the function to a locally
defined array?


The set_intersection algorithm takes an iterator as the destination for
the data that it's copying. ostream_iterator is an iterator that talks
to an ostream. If you want to talk to an array, use an iterator that
talks to an array. A pointer to the first element works well for this. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
Anirudh Ramachandran wrote:
Hi all,

I have a homework problem here, where I have to perform a set
intersection on two sets and use the result elsewhere. I have this
code:

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4);
set_intersection(set1.begin(), set1.end(), set2.begin(),
set2.end(), ostream_iterator<int>(cout, " ")); //I want to do something
else here
return 0;
}

As I said, I dont want to simply output the intersection, but store the
result in an array. The SGI stl reference doesnt seem to provide any
support for this, and even goes on to say that output iterators cant be
read. How do I do it?

Thanks in advance,
anirudh


If I understand correctly, you want to do something similar to below. I
use the "inserter" template function to create an output iterator for
set3, which receives the intersection.

Alan

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4), set3;

set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(),
inserter(set3, set3.begin())) ;

copy(set3.begin(), set3.end(), ostream_iterator<int>(cout, " ")) ;
return 0;
}
Jul 23 '05 #5

Alan Johnson wrote:
Anirudh Ramachandran wrote:
Hi all,

I have a homework problem here, where I have to perform a set
intersection on two sets and use the result elsewhere. I have this
code:

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4);
set_intersection(set1.begin(), set1.end(), set2.begin(),
set2.end(), ostream_iterator<int>(cout, " ")); //I want to do something else here
return 0;
}

As I said, I dont want to simply output the intersection, but store the result in an array. The SGI stl reference doesnt seem to provide any support for this, and even goes on to say that output iterators cant be read. How do I do it?

Thanks in advance,
anirudh

If I understand correctly, you want to do something similar to below.

I use the "inserter" template function to create an output iterator for set3, which receives the intersection.

Alan

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4), set3;

set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set3, set3.begin())) ;

copy(set3.begin(), set3.end(), ostream_iterator<int>(cout, " ")) ; return 0;
}

Thanks a lot, that worked :)

anirudh

Jul 23 '05 #6

Alan Johnson wrote:
Anirudh Ramachandran wrote:
Hi all,

I have a homework problem here, where I have to perform a set
intersection on two sets and use the result elsewhere. I have this
code:

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4);
set_intersection(set1.begin(), set1.end(), set2.begin(),
set2.end(), ostream_iterator<int>(cout, " ")); //I want to do something else here
return 0;
}

As I said, I dont want to simply output the intersection, but store the result in an array. The SGI stl reference doesnt seem to provide any support for this, and even goes on to say that output iterators cant be read. How do I do it?

Thanks in advance,
anirudh

If I understand correctly, you want to do something similar to below.

I use the "inserter" template function to create an output iterator for set3, which receives the intersection.

Alan

#include <iostream>
#include <iterator>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int a[] = {1, 2, 3, 4, 5};
int b[] = {3, 4, 5, 6};
set <int> set1(a, a+5), set2(b, b+4), set3;

set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set3, set3.begin())) ;

copy(set3.begin(), set3.end(), ostream_iterator<int>(cout, " ")) ; return 0;
}


Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
1
by: Marcin Kaliciñski | last post by:
template<class RanAccIt> void some_algorithm(RanAccIt begin, RanAccIt end) { // this algorithm involves calling std::lexicographical_compare // on range [begin, end), and on reverse of this range...
8
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a...
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
14
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
2
by: arnuld | last post by:
I have 2 programs. 1st PROGRAM: copies an input file to standard output but it does so at the expense of deleting all white-space (space, tabs, newlinies etc). 2nd PROGRAM: copies the input...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.