Velocity Reviews > C++ > C++ Primer 4th edition, ex4.3.1-4.28

# C++ Primer 4th edition, ex4.3.1-4.28

arnuld
Guest
Posts: n/a

 10-15-2006
Exercise 4.28: Write a program to read the standard input and build a
vector of ints from values that are read. Allocate an array of the same
size as the vector and copy the elements from the vector into the
array.

here is the code i wrote. does anyone has a better idea?

#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<int> ivec(ivec_sz);
unsigned j;

for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << "Enter an integer: ";
std::cin >> j;
*iter = j;
}

// creating an array of same size as "ivec"
const unsigned arr_sz = ivec_sz;
int ia[arr_sz];

// reading from "vector" & writing into the "array"
int *pia = ia;
std::vector<int>::iterator iter_ivec=ivec.begin();

for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
*pbegin = *iter_ivec;
++iter_ivec;
}

std::cout << "Printing array elements: " << std::endl;
for(int *pbegin = ia, *pend = ia + arr_sz;
*pbegin != *pend;
++pbegin)
{
std::cout << "array element: " << *pbegin << std::endl;
}

}

--arnuld
http://arnuld.blogspot.com

Kai-Uwe Bux
Guest
Posts: n/a

 10-15-2006
arnuld wrote:

> Exercise 4.28: Write a program to read the standard input and build a
> vector of ints from values that are read. Allocate an array of the same
> size as the vector and copy the elements from the vector into the
> array.
>
> here is the code i wrote. does anyone has a better idea?
>
> #include <iostream>
> #include <vector>
>
> int main() {
> const unsigned ivec_sz = 6;
> std::vector<int> ivec(ivec_sz);
> unsigned j;
>
> // adding elements to vector
> for(std::vector<int>::iterator iter = ivec.begin();
> iter != ivec.end();
> ++iter)
> {
> std::cout << "Enter an integer: ";
> std::cin >> j;
> *iter = j;
> }
>
> // creating an array of same size as "ivec"
> const unsigned arr_sz = ivec_sz;
> int ia[arr_sz];
>
> // reading from "vector" & writing into the "array"
> int *pia = ia;
> std::vector<int>::iterator iter_ivec=ivec.begin();
>
> for(int *pbegin = ia, *pend = ia + arr_sz;
> *pbegin != *pend;
> ++pbegin)
> {
> *pbegin = *iter_ivec;
> ++iter_ivec;
> }
>
> std::cout << "Printing array elements: " << std::endl;
> for(int *pbegin = ia, *pend = ia + arr_sz;
> *pbegin != *pend;
> ++pbegin)
> {
> std::cout << "array element: " << *pbegin << std::endl;
> }
>
> }

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

int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<int> i_vect ( std::istream_iterator<int>( std::cin ),
(std::istream_iterator<int>()) );
// create a dynamic array of the same size:
int* i_array = new int [ i_vect.size() ];
// and copy the elements from the vector into the array:
std::copy( i_vect.begin(), i_vect.end(), i_array );

// sanity check: output the array
std::copy( i_array, i_array + i_vect.size(),
std:stream_iterator<int>( std::cout, " " ) );
std::cout << '\n';
}

news_group> echo 1 2 3 4 | a.out
1 2 3 4

Best

Kai-Uwe Bux

Kai-Uwe Bux
Guest
Posts: n/a

 10-15-2006
Kai-Uwe Bux wrote:

> arnuld wrote:
>
>> Exercise 4.28: Write a program to read the standard input and
>> build a vector of ints from values that are read. Allocate an array of
>> the same size as the vector and copy the elements from the vector into
>> the array.
>>
>> here is the code i wrote. does anyone has a better idea?
>>
>> #include <iostream>
>> #include <vector>
>>
>> int main() {
>> const unsigned ivec_sz = 6;
>> std::vector<int> ivec(ivec_sz);
>> unsigned j;
>>
>> // adding elements to vector
>> for(std::vector<int>::iterator iter = ivec.begin();
>> iter != ivec.end();
>> ++iter)
>> {
>> std::cout << "Enter an integer: ";
>> std::cin >> j;
>> *iter = j;
>> }
>>
>> // creating an array of same size as "ivec"
>> const unsigned arr_sz = ivec_sz;
>> int ia[arr_sz];
>>
>> // reading from "vector" & writing into the "array"
>> int *pia = ia;
>> std::vector<int>::iterator iter_ivec=ivec.begin();
>>
>> for(int *pbegin = ia, *pend = ia + arr_sz;
>> *pbegin != *pend;
>> ++pbegin)
>> {
>> *pbegin = *iter_ivec;
>> ++iter_ivec;
>> }
>>
>> std::cout << "Printing array elements: " << std::endl;
>> for(int *pbegin = ia, *pend = ia + arr_sz;
>> *pbegin != *pend;
>> ++pbegin)
>> {
>> std::cout << "array element: " << *pbegin << std::endl;
>> }
>>
>> }

>
> #include <algorithm>
> #include <iterator>
> #include <iostream>
> #include <vector>
>
> int main ( void ) {
> // build a vector from ints to be read from standard input:
> std::vector<int> i_vect ( std::istream_iterator<int>( std::cin ),
> (std::istream_iterator<int>()) );
> // create a dynamic array of the same size:
> int* i_array = new int [ i_vect.size() ];
> // and copy the elements from the vector into the array:
> std::copy( i_vect.begin(), i_vect.end(), i_array );
>
> // sanity check: output the array
> std::copy( i_array, i_array + i_vect.size(),
> std:stream_iterator<int>( std::cout, " " ) );
> std::cout << '\n';

I am getting tired:

delete [] i_array;

[Not that it makes any difference, but just to keep good habits. Then again,
how is using int* instead of vector<int> a good habit anyway?]

> }
>
> news_group> echo 1 2 3 4 | a.out
> 1 2 3 4

Best

Kai-Uwe Bux

arnuld
Guest
Posts: n/a

 10-15-2006
Kai-Uwe Bux wrote:
> > #include <algorithm>
> > #include <iterator>
> > #include <iostream>
> > #include <vector>
> >
> > int main ( void ) {
> > // build a vector from ints to be read from standard input:
> > std::vector<int> i_vect ( std::istream_iterator<int>( std::cin ),
> > (std::istream_iterator<int>()) );
> > // create a dynamic array of the same size:
> > int* i_array = new int [ i_vect.size() ];
> > // and copy the elements from the vector into the array:
> > std::copy( i_vect.begin(), i_vect.end(), i_array );
> >
> > // sanity check: output the array
> > std::copy( i_array, i_array + i_vect.size(),
> > std:stream_iterator<int>( std::cout, " " ) );
> > std::cout << '\n';

this is the output of this programme:

unix@debian:~/programming/cpp\$ g++ 11.cc

11.cc: In function `int main()':
11.cc:9: error: parse error before `)' token
11.cc:11: error: request for member `size' in `i_vect', which is of
non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
11.cc:13: error: request for member `begin' in `i_vect', which is of
non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
11.cc:13: error: request for member `end' in `i_vect', which is of
non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
11.cc:16: error: request for member `size' in `i_vect', which is of
non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'

unix@debian:~/programming/cpp\$

BTW, i am just reading 4th chapter, C++ newbie, never did any real life
coding, so i was not able to debug the programme.

Daniel T.
Guest
Posts: n/a

 10-15-2006
"arnuld" <(E-Mail Removed)> wrote:

> Exercise 4.28: Write a program to read the standard input and build a
> vector of ints from values that are read. Allocate an array of the same
> size as the vector and copy the elements from the vector into the
> array.
>
> here is the code i wrote. does anyone has a better idea?

Sure I have a better idea, but I'm not the one that has to understand
the solution, you are.

Your biggest need in the below is to learn how to allow the user to
enter a variable number of ints. The best way is to give him some method
of aborting input when he is done entering all the ints he wishes.

Have a go at it yourself, if you have trouble report back what you have
tried.

> #include <iostream>
> #include <vector>
>
> int main() {
> const unsigned ivec_sz = 6;
> std::vector<int> ivec(ivec_sz);

Why the magic number 6?

> unsigned j;
>
> // adding elements to vector
> for(std::vector<int>::iterator iter = ivec.begin();
> iter != ivec.end();
> ++iter)
> {
> std::cout << "Enter an integer: ";
> std::cin >> j;
> *iter = j;
> }

The above does not meet the requirements of the assignment. Your code
requires exactly 6 ints be entered, but the assignment doesn't.

> // creating an array of same size as "ivec"
> const unsigned arr_sz = ivec_sz;
> int ia[arr_sz];
>
> // reading from "vector" & writing into the "array"
> int *pia = ia;

'pia' is defined but never used...

> std::vector<int>::iterator iter_ivec=ivec.begin();
>
> for(int *pbegin = ia, *pend = ia + arr_sz;
> *pbegin != *pend;

Your stop condition is confused. You don't want to dereference the
pointers before comparing. "pbegin != pend".

> ++pbegin)
> {
> *pbegin = *iter_ivec;
> ++iter_ivec;
> }
>
> std::cout << "Printing array elements: " << std::endl;
> for(int *pbegin = ia, *pend = ia + arr_sz;
> *pbegin != *pend;

As above, so here.

> ++pbegin)
> {
> std::cout << "array element: " << *pbegin << std::endl;
> }
>
> }

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.

Daniel T.
Guest
Posts: n/a

 10-15-2006
In article <(E-Mail Removed) .com>,
"arnuld" <(E-Mail Removed)> wrote:

> Kai-Uwe Bux wrote:
> > > #include <algorithm>
> > > #include <iterator>
> > > #include <iostream>
> > > #include <vector>
> > >
> > > int main ( void ) {
> > > // build a vector from ints to be read from standard input:
> > > std::vector<int> i_vect ( std::istream_iterator<int>( std::cin ),
> > > (std::istream_iterator<int>()) );
> > > // create a dynamic array of the same size:
> > > int* i_array = new int [ i_vect.size() ];
> > > // and copy the elements from the vector into the array:
> > > std::copy( i_vect.begin(), i_vect.end(), i_array );
> > >
> > > // sanity check: output the array
> > > std::copy( i_array, i_array + i_vect.size(),
> > > std:stream_iterator<int>( std::cout, " " ) );
> > > std::cout << '\n';

>
> this is the output of this programme:
>
> unix@debian:~/programming/cpp\$ g++ 11.cc
>
> 11.cc: In function `int main()':
> 11.cc:9: error: parse error before `)' token
> 11.cc:11: error: request for member `size' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:13: error: request for member `begin' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:13: error: request for member `end' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:16: error: request for member `size' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
>
> unix@debian:~/programming/cpp\$
>
> BTW, i am just reading 4th chapter, C++ newbie, never did any real life
> coding, so i was not able to debug the programme.

Older compilers will have a problem with Kal's code. To work around
that, replace line 11 with:

std::vector<int> vec;
std::copy( std::istream_iterator<int>( cin ),
std::istream_iterator<int>(),
std::back_inserter( vec ) );

The whole program would be much easer to read if he had a using
declaration instead of std:: all over the place IMHO.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.

Kai-Uwe Bux
Guest
Posts: n/a

 10-16-2006
arnuld wrote:

> Kai-Uwe Bux wrote:
>> > #include <algorithm>
>> > #include <iterator>
>> > #include <iostream>
>> > #include <vector>
>> >
>> > int main ( void ) {
>> > // build a vector from ints to be read from standard input:
>> > std::vector<int> i_vect ( std::istream_iterator<int>( std::cin ),
>> > (std::istream_iterator<int>()) );
>> > // create a dynamic array of the same size:
>> > int* i_array = new int [ i_vect.size() ];
>> > // and copy the elements from the vector into the array:
>> > std::copy( i_vect.begin(), i_vect.end(), i_array );
>> >
>> > // sanity check: output the array
>> > std::copy( i_array, i_array + i_vect.size(),
>> > std:stream_iterator<int>( std::cout, " " ) );
>> > std::cout << '\n';

>
> this is the output of this programme:
>
> unix@debian:~/programming/cpp\$ g++ 11.cc
>
> 11.cc: In function `int main()':
> 11.cc:9: error: parse error before `)' token
> 11.cc:11: error: request for member `size' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:13: error: request for member `begin' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:13: error: request for member `end' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
> 11.cc:16: error: request for member `size' in `i_vect', which is of
> non-aggregate type `std::vector<int, std::allocator<int> > ()(...)'
>
> unix@debian:~/programming/cpp\$

standard. I tested it with g++-3.4.6, g++-4.0.2, and g++-4.1.1. All compile
the code without any complaint.

> BTW, i am just reading 4th chapter, C++ newbie, never did any real life
> coding, so i was not able to debug the programme.

The program is not buggy. It is standard conforming. Apparently, it just
triggered a bug in the (outdated) compiler you used.

Best

Kai-Uwe Bux

arnuld
Guest
Posts: n/a

 10-16-2006
> Sure I have a better idea, but I'm not the one that has to understand
> the solution, you are.

I know & i always try but being a newbie many times i go wrong,
unintentionally

> Your biggest need in the below is to learn how to allow the user to
> enter a variable number of ints. The best way is to give him some method
> of aborting input when he is done entering all the ints he wishes.
>
> Have a go at it yourself, if you have trouble report back what you have
> tried.
>
> > #include <iostream>
> > #include <vector>
> >
> > int main() {
> > const unsigned ivec_sz = 6;
> > std::vector<int> ivec(ivec_sz);

>
> Why the magic number 6?

nothing special about 6, i just wanted to have only 6 elements in the
vector.

> > unsigned j;
> >
> > // adding elements to vector
> > for(std::vector<int>::iterator iter = ivec.begin();
> > iter != ivec.end();
> > ++iter)
> > {
> > std::cout << "Enter an integer: ";
> > std::cin >> j;
> > *iter = j;
> > }

>
> The above does not meet the requirements of the assignment. Your code
> requires exactly 6 ints be entered, but the assignment doesn't.

not sure what exactly you mean. "ivec" is explicitly initialised with
size = 6 & implicit initialiation will initialises every element to
"0", hence i have avector of 6 zeros. after that with "*iter = j" i am
replacing every element with what user entered &"iter != ivec.end()"
will take care of not to go beyond the "ivec".

> > // reading from "vector" & writing into the "array"
> > int *pia = ia;

> 'pia' is defined but never used...

yes, my mistake, removing it from the code.

> > for(int *pbegin = ia, *pend = ia + arr_sz;
> > *pbegin != *pend;

>
> Your stop condition is confused. You don't want to dereference the
> pointers before comparing. "pbegin != pend".

right, correctd in the code

> > ++pbegin)
> > {
> > *pbegin = *iter_ivec;
> > ++iter_ivec;
> > }
> >
> > std::cout << "Printing array elements: " << std::endl;
> > for(int *pbegin = ia, *pend = ia + arr_sz;
> > *pbegin != *pend;

>
> As above, so here.

corrected in code. here is the modified code with output:

// this programme reads the "ints" from std::cin & create a
"vector<int>"
// from them & then copy the vector elements to an array of same size

#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<int> ivec(ivec_sz);
unsigned j;

for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << "Enter an integer: ";
std::cin >> j;
*iter = j;
}

// creating an array
const unsigned arr_sz = ivec_sz;
int ia[arr_sz];

// reading from "vector" & writing into the "array"
std::vector<int>::iterator iter_ivec=ivec.begin();

for(int *pbegin = ia, *pend = ia + arr_sz;
pbegin != pend;
++pbegin)
{
*pbegin = *iter_ivec;
++iter_ivec;
}

std::cout << "Printing array elements: " << std::endl;
for(int *pbegin = ia, *pend = ia + arr_sz;
pbegin != pend;
++pbegin)
{
std::cout << "array element: " << *pbegin << std::endl;
}

}

---------------------------------------------------------------------------------------
/home/unix/programming/cpp \$ ./a.out
Enter an integer: 20
Enter an integer: 21
Enter an integer: 22
Enter an integer: 23
Enter an integer: 24
Enter an integer: 25
Printing array elements:
array element: 20
array element: 21
array element: 22
array element: 23
array element: 24
array element: 25
/home/unix/programming/cpp \$

-------------------------------------------------------------------------------------------------

> There are two things that simply cannot be doubted, logic and perception.
> Doubt those, and you no longer have anyone to discuss your doubts with,
> nor any ability to discuss them.

Daniel T.
Guest
Posts: n/a

 10-16-2006
"arnuld" <(E-Mail Removed)> wrote:
> Daniel T. wrote:
>> "arnuld" <(E-Mail Removed)> wrote:
>>
>> Your biggest need in the below is to learn how to allow the user to
>> enter a variable number of ints. The best way is to give him some
>> method of aborting input when he is done entering all the ints he
>> wishes.
>>
>> Have a go at it yourself, if you have trouble report back what you
>> have tried.
>>
>>> #include <iostream>
>>> #include <vector>
>>>
>>> int main() {
>>> const unsigned ivec_sz = 6;
>>> std::vector<int> ivec(ivec_sz);

>>
>> Why the magic number 6?

>
> nothing special about 6, i just wanted to have only 6 elements in
> the vector.
>
>>> unsigned j;
>>>
>>> // adding elements to vector
>>> for(std::vector<int>::iterator iter = ivec.begin();
>>> iter != ivec.end();
>>> ++iter)
>>> {
>>> std::cout << "Enter an integer: ";
>>> std::cin >> j;
>>> *iter = j;
>>> }

>>
>> The above does not meet the requirements of the assignment. Your
>> code requires exactly 6 ints be entered, but the assignment doesn't.

>
> not sure what exactly you mean. "ivec" is explicitly initialised
> with size = 6 & implicit initialiation will initialises every
> element to "0", hence i have avector of 6 zeros. after that with
> "*iter = j" i am replacing every element with what user entered
> &"iter != ivec.end()" will take care of not to go beyond the "ivec".

What I mean is, the design didn't say "read up to 6 integers from the
standard input", it said "read the standard input and build a
vector of ints." You have placed an arbitrary limit of 6 on the number
of integers the user can enter and that is a big problem.

Change your code so the user can enter as many ints as he wants.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.

arnuld
Guest
Posts: n/a

 10-16-2006
Daniel T. wrote:

> What I mean is, the design didn't say "read up to 6 integers from the
> standard input", it said "read the standard input and build a
> vector of ints." You have placed an arbitrary limit of 6 on the number
> of integers the user can enter and that is a big problem.
>
> Change your code so the user can enter as many ints as he wants.

that is easy, i only need /while(std::cin >> j)/.

anything else in the code you want me to improve?

-- arnuld
http:/arnuld.blogspot.com