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_intersectio n(set1.begin(), set1.end(), set2.begin(),
set2.end(), ostream_iterato r<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 1748
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_intersectio n function is of type ostream_iterato r<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_intersectio n function is of type ostream_iterato r<T>(ostream&) or something of the sort. How can I copy the function to a locally defined array?
The set_intersectio n algorithm takes an iterator as the destination for
the data that it's copying. ostream_iterato r 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_intersectio n(set1.begin(), set1.end(), set2.begin(), set2.end(), ostream_iterato r<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_intersectio n(set1.begin(), set1.end(), set2.begin(), set2.end(),
inserter(set3, set3.begin())) ;
copy(set3.begin (), set3.end(), ostream_iterato r<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_intersectio n(set1.begin(), set1.end(), set2.begin(), set2.end(), ostream_iterato r<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_intersectio n(set1.begin(), set1.end(), set2.begin(),
set2.end(), inserter(set3, set3.begin())) ;
copy(set3.begin (), set3.end(), ostream_iterato r<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_intersectio n(set1.begin(), set1.end(), set2.begin(), set2.end(), ostream_iterato r<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_intersectio n(set1.begin(), set1.end(), set2.begin(),
set2.end(), inserter(set3, set3.begin())) ;
copy(set3.begin (), set3.end(), ostream_iterato r<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(*)
where I want to receive tuples of (item1, item2, item3) from the
iterables. But it doesn't work well for a situation like:
zip(*tuple_iter)
|
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 of a performance hit I'm
taking using this technique versus using 'copy' to copy directly into a
container with elements in place?
Thanks,
d
|
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
// (i.e. as rbegin, rend was passed)
}
How can I call lexicographical_compare inside the function so that it
traverses the range backwards?
|
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 function where I
with the help of iterators want to search through the container and
remove a certain object in the container.
Here is part of the code in that function:
|
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 =
s2 =
s3 =
for value in ???(s1, s2, s3):
| |
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 it has been suspended.
The C# iterators seem to be a special case of this general suspend/resume
concept. The "yield" statement suspends the execution of the current method
and calling MoveNext() resumes it.
I think it would be cleaner to...
|
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 as it is to the standard output.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |