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

A

arnuld

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;
}

}

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

Kai-Uwe Bux

arnuld said:
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::eek: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
 
K

Kai-Uwe Bux

Kai-Uwe Bux said:
arnuld said:
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::eek: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 said:
}

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

Best

Kai-Uwe Bux
 
A

arnuld

Kai-Uwe Bux said:
#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::eek: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.
 
D

Daniel T.

arnuld said:
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.
 
D

Daniel T.

"arnuld said:
Kai-Uwe Bux said:
#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::eek: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.
 
K

Kai-Uwe Bux

arnuld said:
Kai-Uwe Bux said:
#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::eek: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$

Upgrade your C++ compiler. Recent versions of g++ conform better to the
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
 
A

arnuld

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.


Why the magic number 6?

nothing special about 6, i just wanted to have only 6 elements in the
vector.
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".
'pia' is defined but never used...

yes, my mistake, removing it from the code.
Your stop condition is confused. You don't want to dereference the
pointers before comparing. "pbegin != pend".

right, correctd in the code
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;

// 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
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 $


-------------------------------------------------------------------------------------------------
 
D

Daniel T.

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


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.
 
A

arnuld

Daniel said:
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
 
D

Daniel T.

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

anything else in the code you want me to improve?

It's not that easy, changing that one line should cause the code to not
compile, unless you didn't remove the 6 element limit.
 
A

arnuld

Daniel said:
It's not that easy, changing that one line should cause the code to not
compile, unless you didn't remove the 6 element limit.

i "know", i meant i got the idea. BTW, here is the modified-code:

#include <iostream>
#include <vector>

int main() {
std::vector<int> ivec;
unsigned j;

// adding elements to vector
while(std::cin >> j)
{
ivec.push_back(j);
}

// output elements of "ivec"
std::cout << "-------------- writing ELEMENTS of vector
-------------------"
<< std::endl;

for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << *iter << std::endl;
}



// creating an array of same size as "ivec"
const unsigned arr_sz = ivec.size();
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;
}
}

on my system, (1 hour ago i have installed Fedora Core 4, whihc has
"g++ 4.0.0"), when i enter any non-int element, the programme stops
adding "ints" & breaks out of the /while/ loop & then executes the
remaining programme.

-- arnuld
http:
 
D

Daniel T.

arnuld said:
i "know", i meant i got the idea. BTW, here is the modified-code:

#include <iostream>
#include <vector>

int main() {
std::vector<int> ivec;
unsigned j;

Why did you make 'j' unsigned? The user is supposed to be able to input
ints...
// adding elements to vector
while(std::cin >> j)
{
ivec.push_back(j);
}

// output elements of "ivec"
std::cout << "-------------- writing ELEMENTS of vector
-------------------"
<< std::endl;

for(std::vector<int>::iterator iter = ivec.begin();
iter != ivec.end();
++iter)
{
std::cout << *iter << std::endl;
}



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

Can someone else spot me on the above? I didn't think that was valid. I
thought the array size needed to be known at compile time.
// reading from "vector" & writing into the "array"
int *pia = ia;

Again with the unused pia...
std::vector<int>::iterator iter_ivec=ivec.begin();

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

I think a more idiomatic way of writing the above would be:

int* pbegin = ia;
for ( vector<int>::iterator it = ivec.begin(); it != ivec.end();
++it, ++pbegin )
*pbegin = *it;

Better still would be:

copy( ivec.begin(), ivec.end(), ia );
 
A

arnuld

Why did you make 'j' unsigned? The user is supposed to be able to input ints...

i wanted only +ve ints, my choice :), anyway i changed it to /int j/
// creating an array of same size as "ivec"
const unsigned arr_sz = ivec.size();
int ia[arr_sz];

Can someone else spot me on the above? I didn't think that was valid. I
thought the array size needed to be known at compile time.

Is it essential? if yes, why?
Again with the unused pia...

a typing mistake from my-side.
I think a more idiomatic way of writing the above would be:

int* pbegin = ia;
for ( vector<int>::iterator it = ivec.begin(); it != ivec.end();
++it, ++pbegin )
*pbegin = *it;

you are really good. what you do (i mean your job)
Better still would be:

copy( ivec.begin(), ivec.end(), ia );

(#include <algorithm> to make the above compile.)

Hmmm... i am on chapter 4 of "C++ Primer 4/e", so i did not know that.

thanks

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

Daniel T.

arnuld said:
i wanted only +ve ints, my choice :), anyway i changed it to /int j/

If you only want positive integers that's fine, just be consistent and
make the vector hold 'unsigned' as well.
// creating an array of same size as "ivec"
const unsigned arr_sz = ivec.size();
int ia[arr_sz];

Can someone else spot me on the above? I didn't think that was valid. I
thought the array size needed to be known at compile time.

Is it essential? if yes, why?

I'm not sure if it is essential or not, that's why I asked. :)
you are really good. what you do (i mean your job)

I'm writing video games for NintendoDS.
Hmmm... i am on chapter 4 of "C++ Primer 4/e", so i did not know that.

You'll get there. :)
 
D

Daniel T.

Daniel T. said:
// creating an array of same size as "ivec"
const unsigned arr_sz = ivec.size();
int ia[arr_sz];

Can someone else spot me on the above? I didn't think that was
valid. I thought the array size needed to be known at compile time.

Is it essential? if yes, why?

I'm not sure if it is essential or not, that's why I asked. :)

OK, I got my answer (see the thread "Dynamic sized array?")

Your code above is not legal C++. You need to new the array as in:

int* ia = new int[arr_sz];

which means you have to delete it when you are done with it:

delete [] ia;
 
T

Thomas J. Gritzan

Daniel said:
Daniel T. said:
// creating an array of same size as "ivec"
const unsigned arr_sz = ivec.size();
int ia[arr_sz];
Can someone else spot me on the above? I didn't think that was
valid. I thought the array size needed to be known at compile time.
Is it essential? if yes, why?
I'm not sure if it is essential or not, that's why I asked. :)

OK, I got my answer (see the thread "Dynamic sized array?")

Your code above is not legal C++. You need to new the array as in:

int* ia = new int[arr_sz];

which means you have to delete it when you are done with it:

delete [] ia;

std::vector<int> ia(arr_sz);

No delete needed.
 
M

Marcus Kwok

Thomas J. Gritzan said:
Daniel said:
Your code above is not legal C++. You need to new the array as in:

int* ia = new int[arr_sz];

which means you have to delete it when you are done with it:

delete [] ia;

std::vector<int> ia(arr_sz);

No delete needed.

The whole point of the exercise was to read into a vector<int>, then
copy it into a regular array. Therefore the array must be dynamically
allocated, and thus deleted.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top