📄 glib-arrays.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Arrays</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-data-types.html" title="GLib Data Types"><link rel="prev" href="glib-String-Chunks.html" title="String Chunks"><link rel="next" href="glib-Pointer-Arrays.html" title="Pointer Arrays"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-String-Chunks.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-data-types.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Pointer-Arrays.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Arrays"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Arrays</span></h2><p>Arrays — arrays of arbitrary elements which grow automatically as elements are added.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Arrays.html#GArray">GArray</a>;<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-new">g_array_new</a> (<a href="glib-Basic-Types.html#gboolean">gboolean</a> zero_terminated, <a href="glib-Basic-Types.html#gboolean">gboolean</a> clear_, <a href="glib-Basic-Types.html#guint">guint</a> element_size);<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-sized-new">g_array_sized_new</a> (<a href="glib-Basic-Types.html#gboolean">gboolean</a> zero_terminated, <a href="glib-Basic-Types.html#gboolean">gboolean</a> clear_, <a href="glib-Basic-Types.html#guint">guint</a> element_size, <a href="glib-Basic-Types.html#guint">guint</a> reserved_size);#define <a href="glib-Arrays.html#g-array-append-val">g_array_append_val</a> (a,v)<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-append-vals">g_array_append_vals</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data, <a href="glib-Basic-Types.html#guint">guint</a> len);#define <a href="glib-Arrays.html#g-array-prepend-val">g_array_prepend_val</a> (a,v)<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-prepend-vals">g_array_prepend_vals</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data, <a href="glib-Basic-Types.html#guint">guint</a> len);#define <a href="glib-Arrays.html#g-array-insert-val">g_array_insert_val</a> (a,i,v)<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-insert-vals">g_array_insert_vals</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#guint">guint</a> index_, <a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> data, <a href="glib-Basic-Types.html#guint">guint</a> len);<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-remove-index">g_array_remove_index</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#guint">guint</a> index_);<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-remove-index-fast">g_array_remove_index_fast</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#guint">guint</a> index_);<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-remove-range">g_array_remove_range</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#guint">guint</a> index_, <a href="glib-Basic-Types.html#guint">guint</a> length);void <a href="glib-Arrays.html#g-array-sort">g_array_sort</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Doubly-Linked-Lists.html#GCompareFunc">GCompareFunc</a> compare_func);void <a href="glib-Arrays.html#g-array-sort-with-data">g_array_sort_with_data</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Doubly-Linked-Lists.html#GCompareDataFunc">GCompareDataFunc</a> compare_func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);#define <a href="glib-Arrays.html#g-array-index">g_array_index</a> (a,t,i)<a href="glib-Arrays.html#GArray">GArray</a>* <a href="glib-Arrays.html#g-array-set-size">g_array_set_size</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#guint">guint</a> length);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-Arrays.html#g-array-free">g_array_free</a> (<a href="glib-Arrays.html#GArray">GArray</a> *array, <a href="glib-Basic-Types.html#gboolean">gboolean</a> free_segment);</pre></div><div class="refsect1" lang="en"><a name="id3157275"></a><h2>Description</h2><p>Arrays are similar to standard C arrays, except that they grow automaticallyas elements are added.</p><p>Array elements can be of any size (though all elements of one array are thesame size), and the array can be automatically cleared to '0's andzero-terminated.</p><p>To create a new array use <a href="glib-Arrays.html#g-array-new"><code class="function">g_array_new()</code></a>.</p><p>To add elements to an array, use <a href="glib-Arrays.html#g-array-append-val"><code class="function">g_array_append_val()</code></a>, <a href="glib-Arrays.html#g-array-append-vals"><code class="function">g_array_append_vals()</code></a>,<a href="glib-Arrays.html#g-array-prepend-val"><code class="function">g_array_prepend_val()</code></a>, and <a href="glib-Arrays.html#g-array-prepend-vals"><code class="function">g_array_prepend_vals()</code></a>.</p><p>To access an element of an array, use <a href="glib-Arrays.html#g-array-index"><code class="function">g_array_index()</code></a>.</p><p>To set the size of an array, use <a href="glib-Arrays.html#g-array-set-size"><code class="function">g_array_set_size()</code></a>.</p><p>To free an array, use <a href="glib-Arrays.html#g-array-free"><code class="function">g_array_free()</code></a>.</p><div class="example"><a name="id3157395"></a><p class="title"><b>Example 3. Using a <span class="structname">GArray</span> to store <span class="type">gint</span> values</b></p><pre class="programlisting"> GArray *garray; gint i; /* We create a new array to store gint values. We don't want it zero-terminated or cleared to 0's. */ garray = g_array_new (FALSE, FALSE, sizeof (gint)); for (i = 0; i < 10000; i++) g_array_append_val (garray, i); for (i = 0; i < 10000; i++) if (g_array_index (garray, gint, i) != i) g_print ("ERROR: got %d instead of %d\n", g_array_index (garray, gint, i), i); g_array_free (garray, TRUE);</pre></div></div><div class="refsect1" lang="en"><a name="id3157413"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3157430"></a><h3><a name="GArray"></a>GArray</h3><a class="indexterm" name="id3157438"></a><pre class="programlisting">typedef struct { gchar *data; guint len;} GArray;</pre><p>Contains the public fields of an Array.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><a href="glib-Basic-Types.html#gchar">gchar</a> *<em class="structfield"><code>data</code></em>;</span></td><td>a pointer to the element data. The data may be moved as elements areadded to the <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a>.</td></tr><tr><td><span class="term"><a href="glib-Basic-Types.html#guint">guint</a> <em class="structfield"><code>len</code></em>;</span></td><td>the number of elements in the <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3157524"></a><h3><a name="g-array-new"></a>g_array_new ()</h3><a class="indexterm" name="id3157534"></a><pre class="programlisting"><a href="glib-Arrays.html#GArray">GArray</a>* g_array_new (<a href="glib-Basic-Types.html#gboolean">gboolean</a> zero_terminated, <a href="glib-Basic-Types.html#gboolean">gboolean</a> clear_, <a href="glib-Basic-Types.html#guint">guint</a> element_size);</pre><p>Creates a new <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>zero_terminated</code></em> :</span></td><td><code class="literal">TRUE</code> if the array should have an extra element at the endwhich is set to 0.</td></tr><tr><td><span class="term"><em class="parameter"><code>clear_</code></em> :</span></td><td><code class="literal">TRUE</code> if <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a> elements should be automatically cleared to 0when they are allocated.</td></tr><tr><td><span class="term"><em class="parameter"><code>element_size</code></em> :</span></td><td>the size of each element in bytes.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3157670"></a><h3><a name="g-array-sized-new"></a>g_array_sized_new ()</h3><a class="indexterm" name="id3157681"></a><pre class="programlisting"><a href="glib-Arrays.html#GArray">GArray</a>* g_array_sized_new (<a href="glib-Basic-Types.html#gboolean">gboolean</a> zero_terminated, <a href="glib-Basic-Types.html#gboolean">gboolean</a> clear_, <a href="glib-Basic-Types.html#guint">guint</a> element_size, <a href="glib-Basic-Types.html#guint">guint</a> reserved_size);</pre><p>Creates a new <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a> with <em class="parameter"><code>reserved_size</code></em> elementspreallocated. This avoids frequent reallocation, if you are going toadd many elements to the array. Note however that the size of thearray is still 0.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>zero_terminated</code></em> :</span></td><td><code class="literal">TRUE</code> if the array should have an extra element at the end with all bits cleared.</td></tr><tr><td><span class="term"><em class="parameter"><code>clear_</code></em> :</span></td><td><code class="literal">TRUE</code> if all bits in the array should be cleared to 0 on allocation.</td></tr><tr><td><span class="term"><em class="parameter"><code>element_size</code></em> :</span></td><td>size of each element in the array.</td></tr><tr><td><span class="term"><em class="parameter"><code>reserved_size</code></em> :</span></td><td>number of elements preallocated.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Arrays.html#GArray"><span class="type">GArray</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3157840"></a><h3><a name="g-array-append-val"></a>g_array_append_val()</h3><a class="indexterm" name="id3157851"></a><pre class="programlisting">#define g_array_append_val(a,v)</pre><p>Adds the value on to the end of the array.The array will grow in size automatically if necessary.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p><a href="glib-Arrays.html#g-array-append-val"><code class="function">g_array_append_val()</code></a> is a macro which uses a reference to the valueparameter <em class="parameter"><code>v</code></em>. This means that you cannot use it with literal valuessuch as "27". You must use variables.</p></div><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -