473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange errors about set using STL

This is a example from a textbook,but there are some strange error that
I don't understand.Coul d anyone give me some help to realize the
operations on set.Thank you very much:-)
(I compile it with Dev C++ 4.9.9.2)

#include<iostre am>
#include<set>
#include<string >
#include <algorithm>

using namespace std;

typedef set<string> Set;
typedef Set::iterator It;

void print(Set);
Set operator + (Set&,Set&);
Set operator * (Set&,Set&);
Set operator - (Set&,Set&);

int main(void)
{
string str1[]={"A","B","C"," D","E","F","G"} ;
string str2[]={"A","E","I"," O","U"};
Set s1(str1,str1+7) ;
Set s2(str2,str2+5) ;

print(s1);
print(s2);
print(s1+s2);
print(s1*s2);
print(s1-s2);

return 0;
}

Set operator + (Set& s1,Set& s2)
{
Set s(s1);
s.insert(s2.beg in(),s2.end());
return s;
}

Set operator * (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_intersec tion(s1.begin() ,s1.end(),s2.be gin(),s2.end(), s.begin());
s.erase(it,s.en d());
return s;
}

Set operator - (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_differen ce(s1.begin(),s 1.end(),s2.begi n(),s2.end(),s. begin());
s.erase(it,s.en d());
return s;
}

void print(Set s)
{
cout<<"{";
for(It it=s.begin();it !=s.end();it++)
{
if(it==s.begin( ))cout<<*it;
else cout<<","<<*it;
}
cout<<"} "<<"size="<<s.s ize()<<endl;
}

Jun 28 '06 #1
4 3712

Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error that
I don't understand.Coul d anyone give me some help to realize the
operations on set.Thank you very much:-)
(I compile it with Dev C++ 4.9.9.2)


Your code compiled fine on MSVC++ 2005 Express Edition (although there
was a missing } brace at the end of the print() function). What errors
are you getting with Dev-C++?

Jun 28 '06 #2
In article <11************ **********@m73g 2000cwd.googleg roups.com>,
"Yuri CHUANG" <yu*******@126. com> wrote:
This is a example from a textbook,but there are some strange error that
I don't understand.
If the below is an example taken directly from a textbook, burn the book.
Could anyone give me some help to realize the
operations on set.Thank you very much:-)
(I compile it with Dev C++ 4.9.9.2)

#include<iostre am>
#include<set>
#include<string >
#include <algorithm>

using namespace std;

typedef set<string> Set;
typedef Set::iterator It;

void print(Set);
Set operator + (Set&,Set&);
Set operator * (Set&,Set&);
Set operator - (Set&,Set&);

int main(void)
{
string str1[]={"A","B","C"," D","E","F","G"} ;
string str2[]={"A","E","I"," O","U"};
Set s1(str1,str1+7) ;
Set s2(str2,str2+5) ;

print(s1);
print(s2);
print(s1+s2);
print(s1*s2);
print(s1-s2);

return 0;
}

Set operator + (Set& s1,Set& s2)
{
Set s(s1);
s.insert(s2.beg in(),s2.end());
return s;
}

Once a value has been inserted into a set, you can't change it. The
error you are getting is complaining that set_intersectio n and
set_difference are trying to change the values already contained in 's'.
Set operator * (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_intersec tion(s1.begin() ,s1.end(),s2.be gin(),s2.end(), s.begin());
s.erase(it,s.en d());
return s;
}

Set operator - (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_differen ce(s1.begin(),s 1.end(),s2.begi n(),s2.end(),s. begin());
s.erase(it,s.en d());
return s;
}


Here are the functions properly written:

Set operator * (const Set& s1,const Set& s2)
{
Set s;
set_intersectio n( s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter( s, s.begin() ) );
return s;
}

Set operator - (const Set& s1,const Set& s2)
{
Set s;
set_difference( s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter( s, s.begin() ) );
return s;
}

BTW everywhere you have a Set& as a parameter, it should be a const Set&.
Jun 28 '06 #3
un*********@gma il.com wrote:
Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error
that I don't understand.Coul d anyone give me some help to realize the
operations on set.Thank you very much:-)
(I compile it with Dev C++ 4.9.9.2)


Your code compiled fine on MSVC++ 2005 Express Edition (although there
was a missing } brace at the end of the print() function). What
errors are you getting with Dev-C++?


Try www.comeaucomputing.com/tryitout and prepare to be impressed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 28 '06 #4
un*********@gma il.com wrote:
Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error that
I don't understand.Coul d anyone give me some help to realize the
operations on set.Thank you very much:-)
(I compile it with Dev C++ 4.9.9.2)

Your code compiled fine on MSVC++ 2005 Express Edition (although there
was a missing } brace at the end of the print() function). What errors
are you getting with Dev-C++?

Realy??

--
Ian Collins.
Jun 28 '06 #5

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

Similar topics

3
1667
by: Jason | last post by:
I have two applications that I wrote that use asp and javascript source files. Recently both of these applications starting acting strange on my test box. Image icons will randomly stop showing up and any javascript that I use a src file with <script language="javascript" src="mytest.js"></script> ) returns random errors. Javascript on the page itself works fine. If I click f5 multiple times I will get different results. I am...
3
1753
by: Alfonso Morra | last post by:
I have some code that I am porting over from C. It is full of static functions and global variables. I have got around this by wrapping most of the code in a singleton object. However, I am findng that in my method definitions, I am having to fully scope the names of any other functions I am calling. I do not understand this. The clas declaration code look some thing like this: class A: public Singleton<A> { public:
1
1578
by: uthuras | last post by:
I'm having strange problem after upgrading my database using db2updv8 utility. The database reside on AIX 5.2 box. I'm using DB2 V8.1 FP 4a. My developer use DataStage 7.0 to access the database on the AIX box. Each time when a job is executed at DataStage, it will give the following warning messages CIF41TSDSIFM01..LOAD_JOB_LOG.W_STGDB: SQLTables: Error retrieving system catalog information for requested tables(s).
4
7339
by: Praveen_db2 | last post by:
Hi All I am getting strange errors in my db2diag.log can any one tell me what these errors mean?? Following is the code from my db2diag.log ********************************************************************************************* 2006-02-23-17.53.12.253000 Instance:DB2 Node:000 PID:1600(db2syscs.exe) TID:440 Appid:AC10E010.J70A.00E883122250 base sys utilities sqleagnt_sigsegvh Probe:1 Database:DEVM_DB Error in agent...
12
1498
by: ishtar2020 | last post by:
Hi everybody I've been writing my very first application in Python and everything is running smoothly, except for a strange problem that pops up every once in a while. I'm sure is the kind of newbie thing every seasoned programmer knows. Sometimes a receive strange Syntax Errors from parts of code that worked perfectly minutes ago. What's even more puzzling is that those errors are pointed to another part of the module when I do some...
1
2436
by: Larax | last post by:
Alright, so here's the problem. I define a global variable in my script and then add methods/properties to it. Everything works great, no error in Javascript Console. But when I refresh site, several errors "variable is not defined" are displayed. What is more strange, the script is still working ! After each another refresh these errors are keep showing, only after I close site and open it again, these errors disappear, but only to next...
6
2355
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
28
2071
by: charlie | last post by:
Hi, I found an article on informit.com that talks about C++ casts http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1 The strange thing is the author says the following code will *not* compile: double d=15.95; int n= static_cast<int(d);
11
1838
by: VijaKhara | last post by:
Hi all, I just write a very simple codes in C and vthere is a very strange bug which I cannot figure out why. The first loop is for v, and the second for k. There is no relationship between v and k but if I debug and watch the change of the variable after each command. When the sencond loop happends for k, the values of vs change and are set to be equal some values of k. Specifically, v is
0
8401
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, 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...
1
8054
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,...
0
8268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
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, 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...
1
5867
isladogs
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...
0
5440
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();...
0
3900
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...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
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 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.