Vector of Arrays in C++

Discussion in 'C++' started by claire.bell1, Jul 5, 2003.

  1. claire.bell1

    claire.bell1 Guest

    Hi,
    Im having lots of problems trying to implement a vector of arrays in C++.
    Im trying to make a vector that will hold coordinates. Ive decided to
    implement a coordinate as an array of integers of length 2.
    My code to initialise this array is:
    vector<int[2]> coordVector;
    This seems to compile ok but im not sure if it is making a vector of arrays
    because the command:
    coordVector.push_back({0,0}) does not work.

    Im sure im just doing something simple wrong but im having a nightmare
    trying to fix it. Does anyone have any example code that uses a vector of
    arrays that i could look at?

    Thanks,
    Vipa.
     
    claire.bell1, Jul 5, 2003
    #1
    1. Advertising

  2. claire.bell1

    Mike Wahler Guest

    claire.bell1 <> wrote in message
    news:clINa.6330$...
    > Hi,
    > Im having lots of problems trying to implement a vector of arrays in C++.


    I'm not surprised. Containers require that the elements they
    contain be copyable and assignable. Arrays do not meet this
    requirement. You're already using a container (vector), so
    why are you using an array? How about a vector of vectors?

    > Im trying to make a vector that will hold coordinates. Ive decided to
    > implement a coordinate as an array of integers of length 2.


    IMO a poor decision. Either create a 'coordinate' type (class),
    or if you only need 'raw' storage for two values, use a 'std::pair'

    > My code to initialise this array is:
    > vector<int[2]> coordVector;
    > This seems to compile ok but im not sure if it is making a vector of

    arrays
    > because the command:
    > coordVector.push_back({0,0}) does not work.


    (If it were allowed) the element type is 'int[2]', so
    'push_back()'s argument must be this type or a type
    convertible to it. {0, 0} is not a valid expression
    except as an array initializer. It certainly does
    not have type 'int[2]'.

    >
    > Im sure im just doing something simple wrong


    Yes, you're trying to store noncopyable, nonassignable
    items in a container.

    >but im having a nightmare
    > trying to fix it.


    As long as you continue to try to break the language
    rules, the nightmares will continue.

    > Does anyone have any example code that uses a vector of
    > arrays that i could look at?


    Nope, because that's illegal.

    Try:

    #include <iostream>
    #include <ostream>
    #include <utility>
    #include <vector>

    typedef std::pair<int, int> coord_t;
    typedef std::vector<coord_t> coord_list_t;

    std::eek:stream& operator<<(std::eek:stream& os,
    const coord_t& coord)
    {
    return os << coord.first << ", " << coord.second;
    }

    std::eek:stream& operator<<(std::eek:stream& os,
    const coord_list_t& lst)
    {
    for(coord_list_t::size_type i = 0; i != lst.size(); ++i)
    os << lst << '\n';

    return os;
    }

    int main()
    {
    coord_list_t coordVector;
    coordVector.push_back(coord_t( 0, 0));
    coordVector.push_back(coord_t(42, 25));
    coordVector.push_back(coord_t(18, 69));

    std::cout << coordVector << '\n';
    return 0;
    }


    BTW which C++ book(s) are you reading? Perhaps you
    need better ones.

    -Mike
     
    Mike Wahler, Jul 5, 2003
    #2
    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. pmatos
    Replies:
    6
    Views:
    24,118
  2. Replies:
    8
    Views:
    1,998
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    393
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page