Re: playing with vectors
On 2007-08-03 13:57, arnuld wrote:
/* C++ Primer 4/e
* STATEMENT
* given 2 vectors of integers, write a programme to determine
whether one vector * is the prefix of the other vector e.g. if 1st vector
has elements (0,1,1,2) and 2nd * vecotr has elements (0,1,1,2,3,5,8) then
programme should say "TRUE" and if 2nd * vector is smaller then too it
should say "TRUE", else it should say "FALSE". *
*/
Another of the authors vague assignments, should it print TRUE if the
second vector is smaller than the first regardless of the content of the
vectors? I suspect not but its hard to tell.
#include<iostream>
#include<vector>
int main()
{
std::vector<int> ivec1, ivec2;
int ival;
/* creation of 1st vector */
std::cout << "Enter elements for 1st vector" << std::endl;
while(std::cin >> ival)
{
ivec1.push_back(ival);
}
std::cout << "1st vector is created with elements: ";
for(std::vector<int>::const_iterator iter=ivec1.begin();
iter != ivec1.end(); ++iter)
{
std::cout << *iter << " ";
}
std::cout << "\n-------------------" << std::endl;
/* creation od 2nd vector */
ival = 1; /* because ival had EOF value since used last time */
No need to do this, the value of ival will be overwritten when two lines
down in the loop.
std::cout << "Now enter elements for 2nd vector" << std::endl;
while(std::cin >> ival)
{
ivec2.push_back(ival);
}
std::cout << "2st vector is created with elements: ";
for(std::vector<int>::const_iterator iter=ivec2.begin();
iter != ivec2.end(); ++iter)
{
std::cout << *iter << " ";
}
std::cout << std::endl << std::endl;
Organisational tips: Create a function taking two vectors as arguments
and returns a bool which performs the check for you.
unsigned sizeSmaller;
int size1 = ivec1.size();
int size2 = ivec2.size();
/* this "if-else" clause will make the next "for" loop a generalise one
and the "else clause" will work even for vectors of same length */
if(size1 < size2)
{
sizeSmaller = size1;
}
else
{
sizeSmaller = size2;
}
Replace the above with std::min(), don't forget to include <algorithm>.
bool prefix_test = true;
for(std::vector<int>::size_type ix=0;
(ix != sizeSmaller) && prefix_test;
++ix)
You can get rid of prefix_test and sizeSmaller by rewriting the loop
like this:
for (std::vector<int>::size_type ix = 0;
(ix < ivec1.size() && ix < ivec2.size());
++ix)
/* notice the test-condition:
1st, "ix" is an unsigned int and that is why we made sizeSmaller an
unsigned int. 2nd condition will break the loop as soon as we will
meet with 1st false test :) */
{
if(ivec1[ix] != ivec2[ix])
{
prefix_test = false;
}
}
/* print the result */
if(prefix_test)
{
std::cout << "--> TRUE" << std::endl;
}
else
{
std::cout << "--> FALSE" << std::endl;
}
return 0;
}
this programme compiles and runs but it has a semantic bug. i intended
that it will ask me to input elements for both vectors but it only askes
me to input elements for the 1st vector. it seems like the 2nd while loop
never runs.
That's because cin has reaches EOF, you need to reset it before you can
read any more, put std::cin.clear(); before the second loop.
--
Erik Wikstr?m