New comer's question on STL/vector::data() over Dev-Cpp

Discussion in 'C++' started by joshipura@gmail.com, Jul 1, 2013.

  1. Guest

    Hello all,
    I am new to C++.

    Question #1: What the problem is:
    ============
    I have Dev-Cpp 4.9.9.2 on Windows 7/64 bit.
    I am trying out code from http://www.cplusplus.com/reference/vector/vector/data/ on it.
    It fails with
    main.cpp:42: error: 'class std::vector<int, std::allocator<int> >' has no member named 'data'

    --->Is the website code wrong and there is no vector::data()?
    --->Is the package of Dev-Cpp wrong?
    --->Both?

    Question #2: Why the problem is:
    ============
    I need help in solving this for Sudoku solver program.
    I store row, column and box as vectors and need to find an intersection of the trio.
    As I understand from examples from http://www.cplusplus.com, in order to use std::set_intersection, I need to pass two arrays with lengths and a vector to store the difference.
    This forces me to convert row, column and box into arrays using vector::data(), which fails.

    --->Is there a way out without using vector::data()?

    Thanks in advance!
    , Jul 1, 2013
    #1
    1. Advertising

  2. Öö Tiib Guest

    On Monday, 1 July 2013 14:01:48 UTC+3, wrote:
    > Question #1: What the problem is:
    > ============
    > I have Dev-Cpp 4.9.9.2 on Windows 7/64 bit.
    >
    > I am trying out code from http://www.cplusplus.com/reference/vector/vector
    > /data/ on it.
    >
    > It fails with
    >
    > main.cpp:42: error: 'class std::vector<int, std::allocator<int> >' has
    > no member named 'data'
    >
    > --->Is the website code wrong and there is no vector::data()?
    > --->Is the package of Dev-Cpp wrong?
    > --->Both?


    That data() was in the vector of gcc long before C++11 as extension.
    Its difference with '&vec.front()' or '&vec[0]' is that it is not
    undefined behavior when vector is empty.

    Not sure, I don't have 4.9. Open the <vector> header and look for 'data()'
    in it also look over your command line.


    > Question #2: Why the problem is:
    > ============
    > I need help in solving this for Sudoku solver program.
    >
    > I store row, column and box as vectors and need to find an intersection
    > of the trio.


    Sudoku has fixed 81 fields.
    That feels like 'std::array<Field,81> board;' to me not piles of vectors
    or something else as complicated. So you can use index from 0 to 80 for
    to access a field (like 'board[42]').

    Finding that "intersection" by complex runtime calculations is like
    finding '5*6' out by using nested for cycles. Consider:

    int mul = 0;
    for(int i = 0; i<5; ++i)
    for(int j = 0; j<6; ++j)
    ++mul;
    // heureka!!! 5*6 is 30!!!

    For each Sudoku field there is 20 other fields that may not have same value.
    You know their indexes already right now (like you know that 5*6 is 30) so
    do not make your program to calculate them each time. ;)
    Öö Tiib, Jul 1, 2013
    #2
    1. Advertising

  3. Ike Naar Guest

    On 2013-07-01, ?? Tiib <> wrote:
    > That data() was in the vector of gcc long before C++11 as extension.
    > Its difference with '&vec.front()' or '&vec[0]' is that it is not
    > undefined behavior when vector is empty.


    &vec[0] has no undefined behaviour when the vector is empty.
    It's okay to take the address of the one-past-the-end element,
    as long as it's not dereferenced.
    Ike Naar, Jul 2, 2013
    #3
  4. Öö Tiib Guest

    On Tuesday, 2 July 2013 07:56:05 UTC+3, Ike Naar wrote:
    > On 2013-07-01, ?? Tiib <> wrote:
    > > That data() was in the vector of gcc long before C++11 as extension.
    > > Its difference with '&vec.front()' or '&vec[0]' is that it is not
    > > undefined behavior when vector is empty.

    >
    > &vec[0] has no undefined behaviour when the vector is empty.
    > It's okay to take the address of the one-past-the-end element,
    > as long as it's not dereferenced.


    I would also love if it was like you say but unfortunately that is not
    the case. vector::eek:perator[] may not throw exeptions on case of out of
    bounds position argument but otherwise its behavior is undefined.
    Öö Tiib, Jul 2, 2013
    #4
  5. Ike Naar Guest

    On 2013-07-02, Ike Naar <> wrote:
    > On 2013-07-01, ?? Tiib <> wrote:
    >> That data() was in the vector of gcc long before C++11 as extension.
    >> Its difference with '&vec.front()' or '&vec[0]' is that it is not
    >> undefined behavior when vector is empty.

    >
    > &vec[0] has no undefined behaviour when the vector is empty.
    > It's okay to take the address of the one-past-the-end element,
    > as long as it's not dereferenced.


    I was wrong here. &vec[0] *does* have undefined behaviour when
    vec.empty() holds. Sorry for the confusion.
    Ike Naar, Jul 2, 2013
    #5
    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. littleneo
    Replies:
    2
    Views:
    485
    Bob Gailer
    Oct 9, 2003
  2. Replies:
    8
    Views:
    1,877
    Csaba
    Feb 18, 2006
  3. senthi

    new comer

    senthi, Sep 12, 2007, in forum: C Programming
    Replies:
    1
    Views:
    612
  4. New comer

    , Nov 13, 2007, in forum: Python
    Replies:
    2
    Views:
    597
    BLeAm
    Nov 13, 2007
  5. new comer

    , Dec 28, 2007, in forum: Java
    Replies:
    4
    Views:
    566
    Roedy Green
    Jan 9, 2008
Loading...

Share This Page