473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Small code review

Assuming the input to this program is

1
2
3
0
2
3
4
0

, is its behavior guaranteed to be well-defined? Is there anything
else about it that warrants comments and/or ridicule? The idea is to
accept two lists of numbers (separated by 0's) and print the numbers
that are in exactly one of the two lists; it seems to work.

#include <iostream>
#include <set>

int main()
{
unsigned int foo;
std::set< unsigned int > actual;
std::set< unsigned int > etb;
std::cin >> foo;
while( foo ) {
actual.insert( foo );
std::cin >> foo;
}
std::cin >> foo;
while( foo ) {
etb.insert( foo );
std::cin >> foo;
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; ) {
std::set<unsign ed int>::iterator j=etb.find( *i );
if( j != etb.end() ) {
std::set<unsign ed int>::iterator del=i;
++i;
actual.erase( del );
etb.erase( j );
}
else {
++i;
}
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; i++ ) {
std::cout << "actual: " << *i << std::endl;
}
for( std::set<unsign ed int>::iterator i=etb.begin() ; i !=
etb.end() ; i++ ) {
std::cout << "etb: " << *i << std::endl;
}
return 0;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
2 1266
Christopher Benson-Manica wrote:
Assuming the input to this program is

1
2
3
0
2
3
4
0

, is its behavior guaranteed to be well-defined?
Methinks, yes.
Is there anything
else about it that warrants comments and/or ridicule?
See inline with code. Nothing to ridicule. Just two small things
I'd do differently.
The idea is to
accept two lists of numbers (separated by 0's) and print the numbers
that are in exactly one of the two lists; it seems to work.

#include <iostream>
#include <set>

int main()
{
unsigned int foo;
std::set< unsigned int > actual;
std::set< unsigned int > etb;
std::cin >> foo;
while( foo ) {
actual.insert( foo );
std::cin >> foo;
}
std::cin >> foo;
while( foo ) {
etb.insert( foo );
std::cin >> foo;
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; ) {
std::set<unsign ed int>::iterator j=etb.find( *i );
if( j != etb.end() ) {
std::set<unsign ed int>::iterator del=i;
++i;
You could easily do it in one statement:

std::set<unsign ed int>::iterator del = i++;
actual.erase( del );
etb.erase( j );
}
else {
++i;
}
I personally don't like extraneous curly braces. Personally.
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; i++ ) {
std::cout << "actual: " << *i << std::endl;
}
for( std::set<unsign ed int>::iterator i=etb.begin() ; i !=
etb.end() ; i++ ) {
std::cout << "etb: " << *i << std::endl;
}
return 0;
}


V
Jul 22 '05 #2

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:cl******** **@chessie.cirr .com...
Assuming the input to this program is

1
2
3
0
2
3
4
0

, is its behavior guaranteed to be well-defined? Is there anything
else about it that warrants comments and/or ridicule? The idea is to
accept two lists of numbers (separated by 0's) and print the numbers
that are in exactly one of the two lists; it seems to work.

#include <iostream>
#include <set>

int main()
{
unsigned int foo;
std::set< unsigned int > actual;
std::set< unsigned int > etb;
std::cin >> foo;
while( foo ) {
actual.insert( foo );
std::cin >> foo;
}
std::cin >> foo;
while( foo ) {
etb.insert( foo );
std::cin >> foo;
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; ) {
std::set<unsign ed int>::iterator j=etb.find( *i );
if( j != etb.end() ) {
std::set<unsign ed int>::iterator del=i;
++i;
actual.erase( del );
etb.erase( j );
}
else {
++i;
}
}
for( std::set<unsign ed int>::iterator i=actual.begin( ) ; i !=
actual.end() ; i++ ) {
std::cout << "actual: " << *i << std::endl;
}
for( std::set<unsign ed int>::iterator i=etb.begin() ; i !=
etb.end() ; i++ ) {
std::cout << "etb: " << *i << std::endl;
}

This is one of the occasions when the STL really does work. The above three
loops could be written as a couple of lines

std::ostream_it erator<unsigned int> output(std::cou t, "\n");
std::set_symmet ric_difference( actual.begin( ), actual.end( ), etb.begin( ),
etb.end( ), output);

You would lose the output "actual: " and "etb: " but if you were worried
about that then try this

std::ostream_it erator<unsigned int> output(std::cou t, "\n");
std::cout << "actual: ";
std::set_differ ence(actual.beg in(), actual.end(), etb.begin(), etb.end(),
output);
std::cout << "etb: ";
std::set_differ ence(etb.begin( ), etb.end(), actual.begin(), actual.end(),
output);

That's pretty close to what you had.

john
Jul 22 '05 #3

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

Similar topics

3
5225
by: Arvie | last post by:
I need some advice guys.. I am proposing that we get someone to do a complete audit/review of our Java application codebase, about 1000 JSPs/Servlets and 100 EJBs. If I get firms to submit proposals, what should I be asking /looking out for? I realise that running the applications through a migration tool will help but I am looking for a more through analysis of the actual codebase as well as assessment of the architecture Thanks
7
3942
by: Randall Parker | last post by:
Using IE 6.x (whatever is the latest) on Windows 2000. For these two CSS definitions if I remove the 2 lines that have the "mso-" font family definitions (mso-fareast-font-family, and mso-bidi-font-family) then the "SmallerText" assigned as a class to a div tag produces larger text than the "SmallerText2". So x-small is treated as a bigger font size than plain old small. How the heck is one supposed to know all the MS stuff one needs...
18
2150
by: Ben Hanson | last post by:
I have created an open source Notepad program for Windows in C++ that allows search and replace using regular expressions (and a few other extras). It is located at http://www.codeproject.com/cpp/notepadre.asp I'm trying to use best practice in my C++ programming and would appreciate any advice anyone can give. As code is far more than just a one page sample, just a review of one of the source files (or even just a function or two) is...
0
1396
by: Michael Douma | last post by:
This is about a small subcontracting project. This is an interesting project which produces interactive graphs of the gnutella network. We are updating a python project written by students at Berkeley a few years ago. This project is to switch from an out-dated data host to a new one. The following is a link to the source code for a python project which makes radial maps of gnutella hosts: ...
19
2284
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my personal server. Are there any code samples or books that someone can recommend so that I can implement this. I would prefer VB.NET code, but I am willing to convert from C# if necessary.
239
10323
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
17
3453
by: Brian Blais | last post by:
Hello, I have a couple of classes where I teach introductory programming using Python. What I would love to have is for the students to go through a lot of very small programs, to learn the basic programming structure. Things like, return the maximum in a list, making lists with certain patterns, very simple string parsing, etc. Unfortunately, it takes a lot of time to grade such things by hand, so I would like to automate it as much...
3
1601
by: JeanDean | last post by:
I am looking for freeware tool which can review the c++ code(compiled on g++). Please share your experiences and details obout the usage of the tool.
4
1916
maxx233
by: maxx233 | last post by:
Hello all, I'm new to OO design and have a question regarding where I should place some code. Here's a simplified situation: I'm making an app to do create, submit, and track employee reviews within our organization. Should I have a class called Review, with properties for things like "review date", "employee being reviewed", etc?.. And methods for things like submit(), generateReport(), delete(), etc? I don't really think I *need* an...
0
9643
marktang
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...
0
10313
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...
0
10147
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
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...
0
6735
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
5378
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...
1
4044
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
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
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...

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.