output iterators

  • Thread starter Anirudh Ramachandran
  • Start date
A

Anirudh Ramachandran

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
 
P

Pete Becker

Anirudh said:
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.
 
A

Anirudh Ramachandran

Pete said:
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
 
P

Pete Becker

Anirudh said:
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>
 
A

Alan Johnson

Anirudh said:
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;
}
 
A

Anirudh Ramachandran

Alan said:
Anirudh said:
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
 
A

Anirudh Ramachandran

Alan said:
Anirudh said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top