comparing elements of vector<int> and list<int>

A

arnuld

It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)


/* C++ Primer - 4/e
*
* exercise 9.20
* STATEMENT:
* Write a program to to compare whether a vector<int> contains the
* same elements as a list<int>.
*
*/


#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>


int main()
{
std::cout << "Enter some integers for VECTOR: ";
std::vector<int> ivec;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ivec ) );

std::cin.clear();
std::cout << "Enter some integers for LIST: ";
std::list<int> ilist;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ilist ) );

std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";

bool comp_result = true;
std::vector<int>::const_iterator viter = ivec.begin();
std::list<int>::const_iterator liter = ilist.begin();


/* If sizes are unequal, then they can not be equal :) */
if( ivec.size() != ilist.size() )
{
comp_result = false;
}
else
{
for( ; ( viter != ivec.end() ) || ( liter != ilist.end() );
++viter, ++liter )
{
if( *viter != *liter )
{
comp_result = false;
}
}
}



if( comp_result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}


return 0;
}



========= A FEW RUNS =================
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_09-20.cpp
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 1

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1
Enter some integers for LIST: 1 2 3 4 5 6 7 8 9 0 9 9 8 8 8 8 8 8 8 8 8 8
8 8 8 88 88 88 8 8 8 88 8 8 88 8 8 8 8 8 8 8 8

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 3

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are EQUAL
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT
equal



-- arnuld
http://lispmachine.wordpress.com
 
A

AnonMail2005

It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)

/* C++ Primer - 4/e
*
* exercise 9.20
* STATEMENT:
* Write a program to to compare whether a vector<int> contains the
* same elements as a list<int>.
*
*/

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>

int main()
{
std::cout << "Enter some integers for VECTOR: ";
std::vector<int> ivec;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ivec ) );

std::cin.clear();
std::cout << "Enter some integers for LIST: ";
std::list<int> ilist;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ilist ) );

std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";

bool comp_result = true;
std::vector<int>::const_iterator viter = ivec.begin();
std::list<int>::const_iterator liter = ilist.begin();

/* If sizes are unequal, then they can not be equal :) */
if( ivec.size() != ilist.size() )
{
comp_result = false;
}
else
{
for( ; ( viter != ivec.end() ) || ( liter != ilist.end() );
++viter, ++liter )
{
if( *viter != *liter )
{
comp_result = false;
}
}
}

if( comp_result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}

return 0;

}

========= A FEW RUNS =================
~/programming/c++ $ g++ -ansi -pedantic -Wall -Wextra ex_09-20.cpp
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 1

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1
Enter some integers for LIST: 1 2 3 4 5 6 7 8 9 0 9 9 8 8 8 8 8 8 8 8 8 8
8 8 8 88 88 88 8 8 8 88 8 8 88 8 8 8 8 8 8 8 8

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT equal
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2 3

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are EQUAL
~/programming/c++ $ ./a.out
Enter some integers for VECTOR: 1 2 3
Enter some integers for LIST: 1 2

Now we will compare VECTOR & LIST for equality :: VECTOR & LIST are NOT
equal

-- arnuldhttp://lispmachine.wordpress.com

You could also try std::equal for containers of the same length.

Also, your code should stop looping as soon as an element doesn't
match.

hth
 
R

red floyd

You could also try std::equal for containers of the same length.

Also, your code should stop looping as soon as an element doesn't
match.

hth

So, it's easy to say:

if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec.begin(), ivec.end(), ilist.begin());
 
A

arnuld

So, it's easy to say:

if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec.begin(), ivec.end(), ilist.begin());


I have wrapped everything into 2 functions and I used main(), only to call
them. how about this ?



bool comp_elem( std::vector<int> const& ivec, std::list<int> const& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}


void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}


int main()
{
std::cout << "Enter some integers for VECTOR: ";
std::vector<int> ivec;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ivec ) );

std::cin.clear();
std::cout << "Enter some integers for LIST: ";
std::list<int> ilist;
std::copy( std::istream_iterator<int>( std::cin ),
std::istream_iterator<int>(),
std::back_inserter( ilist ) );


std::cout << "\nNow we will compare VECTOR & LIST for equality :: ";
bool comp_result = comp_elem( ivec, ilist );
print_result( comp_result );


return 0;
}



-- arnuld
http://lispmachine.wordpress.com
 
J

Jerry Coffin

[ ... just FWIW]
bool comp_elem( std::vector<int> const& ivec, std::list<int> const& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}

return (ivec.size() == ilist.size()) &&
std::equal(ivec.begin(), ivec.end(), ilist.begin());
void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}

static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};

std::cout << strings[result] << std::endl;
 
J

James Kanze

;red floyd said:
(e-mail address removed) wrote:

[...]
So, it's easy to say:
if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec.begin(), ivec.end(), ilist.begin())

And even easier (and more readable) to say:

return ivec.size() == ilist.size()
&& std::equal( ivec.begin(), ivec.end(), ilist.begin() ) ;

(There's also a function lexographical_compare, or something
like that, which might---or might not---be usable here. I'd
look it up quickly, but the file server here is acting up,
and I can't access my online copies of the standard at the
moment.)
 
M

Marjancek

[ ... just FWIW]
bool comp_elem( std::vector<int> const& ivec, std::list<int> const& ilist )
{
if( ivec.size() != ilist.size() )
{
return false;
}
else
{
return std::equal( ivec.begin(), ivec.end(), ilist.begin() );
}
}

return (ivec.size() == ilist.size()) &&
std::equal(ivec.begin(), ivec.end(), ilist.begin());
void print_result( bool result )
{
if( result )
{
std::cout << "VECTOR & LIST are EQUAL" << std::endl;
}
else
{
std::cout << "VECTOR & LIST are NOT equal" << std::endl;
}
}

static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"

};

std::cout << strings[result] << std::endl;

'True' is not always 1, it would be safer to do

std::cout << strings[result ? 1 : 0] << std::endl;

Marjancek
 
R

red floyd

James said:
;red floyd said:
(e-mail address removed) wrote:
[...]
You could also try std::equal for containers of the same length.
Also, your code should stop looping as soon as an element doesn't
match.
So, it's easy to say:
if (ivec.size() != ilist.size())
return false;
else
return std::equal(ivec.begin(), ivec.end(), ilist.begin())

And even easier (and more readable) to say:

return ivec.size() == ilist.size()
&& std::equal( ivec.begin(), ivec.end(), ilist.begin() ) ;

Picky picky picky. :)
 
J

James Kanze

(e-mail address removed) says... [...]
static char const *strings[] = {
"VECTOR and LIST are NOT equal",
"VECTOR and LIST are EQUAL"
};
std::cout << strings[result] << std::endl;
'True' is not always 1,

In C++ it is. The standard requires it.
it would be safer to do
std::cout << strings[result ? 1 : 0] << std::endl;

If the type being tested is not a bool, then something like:

std::cout << strings[ result != 0 ] << std::endl ;

might be appropriate. But for a bool, true is guaranteed to
convert to 1, and false to 0.
 
J

Jerry Coffin

[ ... ]
'True' is not always 1, it would be safer to do

C and C++ are case sensitive. 'true' (not 'True') _is_ a value -- but
when/if you convert it to an integer, it is guaranteed to convert to 1
(and likewise, false to 0).
std::cout << strings[result ? 1 : 0] << std::endl;

This gains absolutely nothing. If you start with an integer, where any
non-zero value converts to 'true', you typically want to convert to a
bool with something like:

std::cout << strings[result != 0] << std::endl;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top