Connecting Tech Pros Worldwide Forums | Help | Site Map

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

arnuld
Guest
 
Posts: n/a
#1: Oct 15 '06
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<intivec(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


Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Oct 15 '06

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


arnuld wrote:
Quote:
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<intivec(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<inti_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::ostream_iterator<int>( std::cout, " " ) );
std::cout << '\n';
}

news_groupecho 1 2 3 4 | a.out
1 2 3 4



Best

Kai-Uwe Bux
Kai-Uwe Bux
Guest
 
Posts: n/a
#3: Oct 15 '06

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


Kai-Uwe Bux wrote:
Quote:
arnuld wrote:
>
Quote:
>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<intivec(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<inti_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::ostream_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<inta good habit anyway?]
Quote:
}
>
news_groupecho 1 2 3 4 | a.out
1 2 3 4
Best

Kai-Uwe Bux
arnuld
Guest
 
Posts: n/a
#4: Oct 15 '06

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


Kai-Uwe Bux wrote:
Quote:
Quote:
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>

int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_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::ostream_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
#5: Oct 15 '06

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


"arnuld" <arnuld3@gmail.comwrote:
Quote:
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.
Quote:
#include <iostream>
#include <vector>
>
int main() {
const unsigned ivec_sz = 6;
std::vector<intivec(ivec_sz);
Why the magic number 6?
Quote:
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.
Quote:
// 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...
Quote:
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".
Quote:
++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.
Quote:
++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
#6: Oct 15 '06

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


In article <1160946463.419101.263780@b28g2000cwb.googlegroups .com>,
"arnuld" <arnuld3@gmail.comwrote:
Quote:
Kai-Uwe Bux wrote:
Quote:
Quote:
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
>
int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_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::ostream_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<intvec;
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
#7: Oct 16 '06

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


arnuld wrote:
Quote:
Kai-Uwe Bux wrote:
Quote:
Quote:
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
>
int main ( void ) {
// build a vector from ints to be read from standard input:
std::vector<inti_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::ostream_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.
Quote:
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
#8: Oct 16 '06

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


Sure I have a better idea, but I'm not the one that has to understand
Quote:
the solution, you are.
I know & i always try but being a newbie many times i go wrong,
unintentionally
Quote:
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.
>
Quote:
#include <iostream>
#include <vector>

int main() {
const unsigned ivec_sz = 6;
std::vector<intivec(ivec_sz);
>
Why the magic number 6?
nothing special about 6, i just wanted to have only 6 elements in the
vector.
Quote:
Quote:
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".
Quote:
Quote:
// reading from "vector" & writing into the "array"
int *pia = ia;
Quote:
'pia' is defined but never used...
yes, my mistake, removing it from the code.
Quote:
Quote:
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
Quote:
Quote:
++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<intivec(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 $


-------------------------------------------------------------------------------------------------
Quote:
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
#9: Oct 16 '06

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


"arnuld" <arnuld3@gmail.comwrote:
Quote:
Daniel T. wrote:
Quote:
>"arnuld" <arnuld3@gmail.comwrote:
>>
>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.
>>
Quote:
>>#include <iostream>
>>#include <vector>
>>>
>>int main() {
>> const unsigned ivec_sz = 6;
>> std::vector<intivec(ivec_sz);
>>
>Why the magic number 6?
>
nothing special about 6, i just wanted to have only 6 elements in
the vector.
>
Quote:
Quote:
>> 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: Oct 16 '06

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


Daniel T. wrote:
Quote:
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

Daniel T.
Guest
 
Posts: n/a
#11: Oct 16 '06

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


"arnuld" <arnuld3@gmail.comwrote:
Quote:
Daniel T. wrote:
>
Quote:
>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?
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.

--
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
#12: Oct 16 '06

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


Daniel T. wrote:
Quote:
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<intivec;
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:

Daniel T.
Guest
 
Posts: n/a
#13: Oct 16 '06

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


"arnuld" <arnuld3@gmail.comwrote:
Quote:
Daniel T. wrote:
Quote:
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<intivec;
unsigned j;
Why did you make 'j' unsigned? The user is supposed to be able to input
ints...
Quote:
// 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.
Quote:
// reading from "vector" & writing into the "array"
int *pia = ia;
Again with the unused pia...
Quote:
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 );

(#include <algorithmto make the above compile.)
Quote:
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;
}
}
--
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
#14: Oct 17 '06

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


Daniel T. wrote:
Quote:
Quote:
int main() {
std::vector<intivec;
unsigned j;
Quote:
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/
Quote:
Quote:
// 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?
Quote:
Quote:
// reading from "vector" & writing into the "array"
int *pia = ia;
>
Again with the unused pia...
a typing mistake from my-side.
Quote:
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)
Quote:
Better still would be:
>
copy( ivec.begin(), ivec.end(), ia );
>
(#include <algorithmto 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

Daniel T.
Guest
 
Posts: n/a
#15: Oct 17 '06

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


"arnuld" <arnuld3@gmail.comwrote:
Quote:
Daniel T. wrote:
>
Quote:
Quote:
int main() {
std::vector<intivec;
unsigned j;
>
Quote:
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/
If you only want positive integers that's fine, just be consistent and
make the vector hold 'unsigned' as well.
Quote:
Quote:
Quote:
// 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. :-)
Quote:
Quote:
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)
I'm writing video games for NintendoDS.
Quote:
Quote:
Better still would be:

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

(#include <algorithmto make the above compile.)
>
Hmmm... i am on chapter 4 of "C++ Primer 4/e", so i did not know that.
You'll get there. :-)

--
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
#16: Oct 17 '06

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


"Daniel T." <daniel_t@earthlink.netwrote:
Quote:
Quote:
Quote:
>>> // 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;

--
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.
Thomas J. Gritzan
Guest
 
Posts: n/a
#17: Oct 17 '06

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


Daniel T. schrieb:
Quote:
"Daniel T." <daniel_t@earthlink.netwrote:
>
Quote:
Quote:
>>>> // 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<intia(arr_sz);

No delete needed.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Marcus Kwok
Guest
 
Posts: n/a
#18: Oct 17 '06

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


Thomas J. Gritzan <Phygon_ANTISPAM@gmx.dewrote:
Quote:
Daniel T. schrieb:
Quote:
>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<intia(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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Closed Thread