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 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)
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
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)
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;
}
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
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; } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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(*)...
|
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...
|
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...
|
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...
|
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 = ...
|
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...
|
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.
|
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);
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |