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

J

joshipura

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

Öö Tiib

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. ;)
 
I

Ike Naar

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

Öö Tiib

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

Ike Naar

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top