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

Discussion in 'C++' started by arnuld, Oct 15, 2006.

  1. arnuld

    arnuld Guest

    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
    arnuld, Oct 15, 2006
    #1
    1. Advertising

  2. arnuld

    Kai-Uwe Bux Guest

    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::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
    Kai-Uwe Bux, Oct 15, 2006
    #2
    1. Advertising

  3. arnuld

    Kai-Uwe Bux Guest

    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::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<int> a good habit anyway?]

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


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 15, 2006
    #3
  4. arnuld

    arnuld Guest

    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::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.
    arnuld, Oct 15, 2006
    #4
  5. arnuld

    Daniel T. Guest

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


    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., Oct 15, 2006
    #5
  6. arnuld

    Daniel T. Guest

    In article <>,
    "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::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.

    --
    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., Oct 15, 2006
    #6
  7. arnuld

    Kai-Uwe Bux Guest

    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::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
    Kai-Uwe Bux, Oct 16, 2006
    #7
  8. arnuld

    arnuld Guest

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

    // 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.
    arnuld, Oct 16, 2006
    #8
  9. arnuld

    Daniel T. Guest

    "arnuld" <> wrote:
    > Daniel T. wrote:
    >> "arnuld" <> 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.
    Daniel T., Oct 16, 2006
    #9
  10. arnuld

    arnuld Guest

    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
    arnuld, Oct 16, 2006
    #10
  11. arnuld

    Daniel T. Guest

    "arnuld" <> wrote:
    > 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.
    Daniel T., Oct 16, 2006
    #11
  12. arnuld

    arnuld Guest

    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<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:
    arnuld, Oct 16, 2006
    #12
  13. arnuld

    Daniel T. Guest

    "arnuld" <> wrote:
    > 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<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 );

    (#include <algorithm> to 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.
    Daniel T., Oct 16, 2006
    #13
  14. arnuld

    arnuld Guest

    Daniel T. wrote:

    > > int main() {
    > > std::vector<int> ivec;
    > > 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 <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
    arnuld, Oct 17, 2006
    #14
  15. arnuld

    Daniel T. Guest

    "arnuld" <> wrote:
    > Daniel T. wrote:
    >
    > > > int main() {
    > > > std::vector<int> ivec;
    > > > 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 <algorithm> to 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., Oct 17, 2006
    #15
  16. arnuld

    Daniel T. Guest

    "Daniel T." <> wrote:

    >>>> // 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.
    Daniel T., Oct 17, 2006
    #16
  17. Daniel T. schrieb:
    > "Daniel T." <> wrote:
    >
    >>>>> // 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.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    Thomas J. Gritzan, Oct 17, 2006
    #17
  18. arnuld

    Marcus Kwok Guest

    Thomas J. Gritzan <> wrote:
    > 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<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.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Oct 17, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Koenig

    Announcement: C++ Primer, 4th edition

    Andrew Koenig, Jan 17, 2005, in forum: C++
    Replies:
    9
    Views:
    428
    red floyd
    Jan 19, 2005
  2. hajime

    C++ Primer, 4th Edition

    hajime, Aug 16, 2005, in forum: C++
    Replies:
    5
    Views:
    445
    hajime
    Aug 16, 2005
  3. asdf
    Replies:
    6
    Views:
    278
    Default User
    Sep 18, 2006
  4. asdf
    Replies:
    3
    Views:
    244
    KiLVaiDeN
    Oct 11, 2006
  5. asdf
    Replies:
    1
    Views:
    337
    Victor Bazarov
    Nov 21, 2006
Loading...

Share This Page