Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ data() function

C++ data() function

C++ Container Class The C++ container class


data is a very special function in vector; itreturns a pointer to the underlying arraylets you use the vector like a C array.

data is a member function of the container class, used toreturns a pointer to the container's internal data storage. This allows the vector to interoperate with C-style APIs or other code that needs raw pointers.

data provides the bridge between vector and C code, reflecting vector's powerful compatibility.

Word meaning: data means 'data', i.e., to get a pointer to the underlying data.


Basic syntax and parameters

data is a member function of the container class; calling it requires no parameters.

Syntax format


T* data();
const T* data() const;

Parameter Description

- `data` takes no arguments.

Function description


Example

Let us thoroughly master data through a series of examples from simple to complex.

Example 1: Basic usage - getting the underlying array pointer

Example

#include

#include

int

main

(

)

{

   

// 1. Create a vector

    std

::

vector

<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
}

;

    std

::

cout

<<

"vector size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 2. Use data to get the underlying array pointer

   

int

ptr

=

numbers.

data

(

)

;

    std

::

cout

<<

"Access via pointer: "

<<

std

::

endl

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

"ptr["

<<

i

<<

"] = "

<<

ptr

[

i

]

<<

std

::

endl

;

   

}

   

return

0

;

}

Expected output:

vector的大小是: 5
Access via pointer:
ptr[0] = 10
ptr[1] = 20
ptr[2] = 30
ptr[3] = 40
ptr[4] = 50

Code analysis:

  1. numbers.data() returns a pointer to the first element.
  2. Through the returned pointer, you can use array subscript syntax to access all elements.

Example 2: Modifying elements via pointer

data The returned pointer can be used to modify elements.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<
int
numbers
=
{
10
,
20
,
30
}

;

    std

::

cout

<<

"Before modification: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Get a pointer via data() and modify the element

   

int

ptr

=

numbers.

data

(

)

;

    ptr

[

0

]

=

100

;

    ptr

[

1

]

=

200

;

    ptr

[

2

]

=

300

;

    std

::

cout

<<

"After modification: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

修改前: 10 20 30
修改后: 100 200 300

Code analysis:

Example 3: Interacting with C-style APIs

data One of the main uses of is to interact with C functions that need an array pointer.

Example

#include

#include

#include // for memset

// A hypothetical C-style function that computes the array sum

int

sumArray

(

const

int

arr,

size_t

size

)

{

   

int

sum

=

0

;

   

for

(

size_t

i

=

0

;

i

<

size

;

++

i

)

{

        sum

=

arr

[

i

]

;

   

}

   

return

sum

;

}

int

main

(

)

{

    std

::

vector

<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
}

;

   

// Use data() to pass the vector to a C-style function

   

int

total

=

sumArray

(

numbers.

data

(

)

, numbers.

size

(

)

)

;

    std

::

cout

<<

"Array elements: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Sum: "

<<

total

<<

std

::

endl

;

   

return

0

;

}

Expected output:

数组元素: 1 2 3 4 5
总和: 15

Code analysis:

Example 4: Use memset to manipulate memory

can be used via data() to directly manipulate the vector's underlying memory.

Example

#include

#include

#include // for memset

int

main

(

)

{

    std

::

vector

<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
}

;

    std

::

cout

<<

"Before zeroing: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Use memset to set all bytes to 0

   

// Note: this is only valid for integer types; complex types may cause problems

    std

::

memset

(

numbers.

data

(

)

,

0

, numbers.

size

(

)

sizeof

(

int

)

)

;

    std

::

cout

<<

"After zeroing: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

清零前: 10 20 30 40 50
清零后: 0 0 0 0 0

Code analysis:

Example 5: Safe use of the returned pointer

when the vector is reallocated, the previously obtained pointer becomes invalid.

Example

#include

#include

int

main

(

)

{

    std

::

vector

<
int
numbers
=
{
1
,
2
,
3
}

;

   

// Get the data pointer

   

int

ptr

=

numbers.

data

(

)

;

    std

::

cout

<<

"Value pointed to by the original pointer: "

<<

ptr

<<

std

::

endl

;

   

// Adding elements may cause memory reallocation

    numbers.

push_back

(

4

)

;

    numbers.

push_back

(

5

)

;

    numbers.

push_back

(

6

)

;

    numbers.

push_back

(

7

)

;

    numbers.

push_back

(

8

)

;

    numbers.

push_back

(

9

)

;

    numbers.

push_back

(

10

)

;

   

// Warning: ptr may already be invalid!

   

// In modern implementations, reallocation usually does not happen, but you should not rely on this

    std

::

cout

<<

"After adding elements, the value pointed to by data(): "

<<

(

numbers.

data

(

)

)

<<

std

::

endl

;

   

// Safe practice: call data() to get a new pointer each time

    ptr

=

numbers.

data

(

)

;

    std

::

cout

<<

"After re-obtaining the pointer: "

<<

ptr

<<

std

::

endl

;

   

return

0

;

}

Code analysis:


C++ Container Class The C++ container class