473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Oct 15 '06 #1
17 2195
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<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
Oct 15 '06 #2
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<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?]
}

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

Kai-Uwe Bux
Oct 15 '06 #3
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<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.

Oct 15 '06 #4
"arnuld" <ar*****@gmail.comwrote:
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<intivec(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.
Oct 15 '06 #5
In article <11**********************@b28g2000cwb.googlegroups .com>,
"arnuld" <ar*****@gmail.comwrote:
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<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.
Oct 15 '06 #6
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<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.
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
Oct 16 '06 #7
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<intivec(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<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 $
-------------------------------------------------------------------------------------------------
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.
Oct 16 '06 #8
"arnuld" <ar*****@gmail.comwrote:
Daniel T. wrote:
>"arnuld" <ar*****@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.
>>#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.
>> 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.
Oct 16 '06 #9
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

Oct 16 '06 #10
"arnuld" <ar*****@gmail.comwrote:
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?
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.
Oct 16 '06 #11
Daniel T. wrote:
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:

Oct 16 '06 #12
"arnuld" <ar*****@gmail.comwrote:
Daniel T. wrote:
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...
// 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 );

(#include <algorithmto make the above compile.)
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.
Oct 16 '06 #13
Daniel T. wrote:
int main() {
std::vector<intivec;
unsigned j;
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?
// reading from "vector" & writing into the "array"
int *pia = ia;

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

Oct 17 '06 #14
"arnuld" <ar*****@gmail.comwrote:
Daniel T. wrote:
int main() {
std::vector<intivec;
unsigned j;
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.
// 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. :-)
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.
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.
Oct 17 '06 #15
"Daniel T." <da******@earthlink.netwrote:
>>> // 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.
Oct 17 '06 #16
Daniel T. schrieb:
"Daniel T." <da******@earthlink.netwrote:
>>>> // 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
Oct 17 '06 #17
Thomas J. Gritzan <Ph*************@gmx.dewrote:
Daniel T. schrieb:
>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
Oct 17 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
1
by: Ciko | last post by:
I had a look at this book: "C Primer Plus, Fifth Edition Stephen Prata " and I found simply fantastic. My english is little but I had no problem to understand it. Was wondering if you...
2
by: asdf | last post by:
any guys read C++ primer 4th edition? I think that there is a small mistake on the page 163. Where can I find the errata?
3
by: huangshan | last post by:
hi all I read "C++ Primer, Fourth Edition" , but can't understand this sentence "By making the parameters const references, we allow types that do not allow copying." in "16.1. Template...
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
2
by: subramanian100in | last post by:
In C++ Primer(4th Edition) by Stanley Lippman, in page 162, the following is mentioned: The general syntactic form of a compound assignment operator is a op= b where op= may be one of the...
1
by: mmdspam | last post by:
Hi, Can someone please tell me the answer to exercise 1.20 in C++ Primer 4th edition. I am stuck on this question. Thank you in advance
2
by: jerry | last post by:
can anyone tell me where can i get the keys to c++ primer 4th edition, pdf or chm type are both ok. thank you!
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.