By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Strange errors about set using STL

P: n/a
This is a example from a textbook,but there are some strange error that
I don't understand.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<iostream>
#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.begin(),s2.end());
return s;
}

Set operator * (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_intersection(s1.begin(),s1.end(),s2.begin() ,s2.end(),s.begin());
s.erase(it,s.end());
return s;
}

Set operator - (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_difference(s1.begin(),s1.end(),s2.begin(),s 2.end(),s.begin());
s.erase(it,s.end());
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.size()<<endl;
}

Jun 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error that
I don't understand.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)


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

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .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<iostream>
#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.begin(),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_intersection 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_intersection(s1.begin(),s1.end(),s2.begin() ,s2.end(),s.begin());
s.erase(it,s.end());
return s;
}

Set operator - (Set& s1,Set& s2)
{
Set s(s1);
It
it=set_difference(s1.begin(),s1.end(),s2.begin(),s 2.end(),s.begin());
s.erase(it,s.end());
return s;
}


Here are the functions properly written:

Set operator * (const Set& s1,const Set& s2)
{
Set s;
set_intersection( 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

P: n/a
un*********@gmail.com wrote:
Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error
that I don't understand.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)


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

P: n/a
un*********@gmail.com wrote:
Yuri CHUANG wrote:
This is a example from a textbook,but there are some strange error that
I don't understand.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)

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 discussion thread is closed

Replies have been disabled for this discussion.