C
Christopher Benson-Manica
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<unsigned int>::iterator i=actual.begin() ; i !=
actual.end() ; ) {
std::set<unsigned int>::iterator j=etb.find( *i );
if( j != etb.end() ) {
std::set<unsigned int>::iterator del=i;
++i;
actual.erase( del );
etb.erase( j );
}
else {
++i;
}
}
for( std::set<unsigned int>::iterator i=actual.begin() ; i !=
actual.end() ; i++ ) {
std::cout << "actual: " << *i << std::endl;
}
for( std::set<unsigned int>::iterator i=etb.begin() ; i !=
etb.end() ; i++ ) {
std::cout << "etb: " << *i << std::endl;
}
return 0;
}
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<unsigned int>::iterator i=actual.begin() ; i !=
actual.end() ; ) {
std::set<unsigned int>::iterator j=etb.find( *i );
if( j != etb.end() ) {
std::set<unsigned int>::iterator del=i;
++i;
actual.erase( del );
etb.erase( j );
}
else {
++i;
}
}
for( std::set<unsigned int>::iterator i=actual.begin() ; i !=
actual.end() ; i++ ) {
std::cout << "actual: " << *i << std::endl;
}
for( std::set<unsigned int>::iterator i=etb.begin() ; i !=
etb.end() ; i++ ) {
std::cout << "etb: " << *i << std::endl;
}
return 0;
}