accessing contiguous std::vector elements as a pair

Discussion in 'C++' started by n.torrey.pines@gmail.com, Dec 20, 2007.

  1. Guest

    I'd like to be able to view two contiguous elements of a vector as a
    pair.

    Assuming I'm not accessing the last element, of course, and the
    element type is not bool, when is it safe to do so, from the language
    definition point of view?

    const pr& p = *(const pr*)(&v);

    // pr - either std::pair or hand-defined pair of elements
    // v - vector instance
    // i + 1 < v.size()
     
    , Dec 20, 2007
    #1
    1. Advertising

  2. Salt_Peter Guest

    On Dec 20, 6:45 pm, wrote:
    > I'd like to be able to view two contiguous elements of a vector as a
    > pair.
    >
    > Assuming I'm not accessing the last element, of course, and the
    > element type is not bool, when is it safe to do so, from the language
    > definition point of view?
    >
    > const pr& p = *(const pr*)(&v);
    >
    > // pr - either std::pair or hand-defined pair of elements
    > // v - vector instance
    > // i + 1 < v.size()


    When is it safe to do so? whenever you take the appropriate
    precautions.
    In this case, std::vectors have an at(...) member function that throws
    an out_of_range exception should someone accidentally attempt to
    access something out of bounds. There you go - problem solved.

    Using pointers for that has no safety and usually demands considerably
    more work (ie: maintenance).

    In the following, i'm not too worried about getting main's for-loop
    wrong, it'll throw if i do. Try it.

    #include <iostream>
    #include <vector>
    #include <stdexcept>

    template< typename T >
    class Container
    {
    std::vector< T > m_v;
    public:
    // default ctor
    Container() : m_v() { }
    Container(const size_t sz, const T& t) : m_v(sz, t) { }
    // member functions
    void push_back(const T& t) { m_v.push_back(t); }
    void showpair(const size_t index) const
    {
    std::cout << "m_v[" << index << "] ";
    std::cout << m_v.at(index);
    std::cout << "\tm_v[" << index + 1 << "] ";
    std::cout << m_v.at(index + 1);
    std::cout << std::endl;
    }
    size_t size() const { return m_v.size(); }
    };

    int main()
    {
    try
    {
    Container< int > container(5, 99);
    container.push_back(77);
    // use i < container.size() to throw an exception
    for(size_t i = 0; i < container.size() - 1; ++i)
    {
    container.showpair(i);
    }
    }
    catch(const std::exception& e)
    {
    std::cout << "\nError: ";
    std::cout << e.what() << std::endl;
    }
    }

    /*
    m_v[0] 99 m_v[1] 99
    m_v[1] 99 m_v[2] 99
    m_v[2] 99 m_v[3] 99
    m_v[3] 99 m_v[4] 99
    m_v[4] 99 m_v[5] 77
    */
     
    Salt_Peter, Dec 21, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 20, 5:22 pm, Salt_Peter <> wrote:
    > On Dec 20, 6:45 pm, wrote:
    >
    > > I'd like to be able to view two contiguous elements of a vector as a
    > > pair.


    > void showpair(const size_t index) const
    > {
    > std::cout << "m_v[" << index << "] ";
    > std::cout << m_v.at(index);
    > std::cout << "\tm_v[" << index + 1 << "] ";
    > std::cout << m_v.at(index + 1);
    > std::cout << std::endl;
    > }




    :)

    This is a joke, right?

    If not, I didn't mean "view" in the common sense, and "at()" doesn't
    add any safety if the index is already guaranteed to be in [0 ..
    size()-2]
     
    , Dec 21, 2007
    #3
  4. James Kanze Guest

    On Dec 21, 12:45 am, wrote:
    > I'd like to be able to view two contiguous elements of a vector as a
    > pair.


    > Assuming I'm not accessing the last element, of course, and the
    > element type is not bool, when is it safe to do so, from the language
    > definition point of view?


    > const pr& p = *(const pr*)(&v);


    > // pr - either std::pair or hand-defined pair of elements
    > // v - vector instance
    > // i + 1 < v.size()


    Anytime your implementation gives you a special guarantee that
    this will work. It's undefined behavior according to the
    standard (even though it's likely to work most of the time in a
    lot of implementations).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 21, 2007
    #4
    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. Newsgroup - Ann
    Replies:
    0
    Views:
    415
    Newsgroup - Ann
    Aug 15, 2003
  2. Ravi
    Replies:
    8
    Views:
    10,401
    Mike Wahler
    Mar 6, 2004
  3. Anonymous
    Replies:
    20
    Views:
    4,428
    Pete Becker
    Mar 30, 2005
  4. Jason Heyes
    Replies:
    8
    Views:
    767
    Andrew Koenig
    Jan 15, 2006
  5. Bart Simpson
    Replies:
    6
    Views:
    402
    Diego Martins
    Mar 13, 2006
Loading...

Share This Page