-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.html
More file actions
350 lines (284 loc) · 17.8 KB
/
data.html
File metadata and controls
350 lines (284 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!doctype html>
<html class="cpprefjp" lang="ja" itemscope="" itemtype="http://schema.org/WebPage">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NXNBNVBTJS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-NXNBNVBTJS');
</script>
<meta charset="UTF-8">
<title>basic_string::data - cpprefjp C++日本語リファレンス</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="keywords" content="
C++,標準ライブラリ,リファレンス,ドキュメント,STL,std,string,std,basic_string,function
">
<meta name="title" content="basic_string::data - cpprefjp C++日本語リファレンス" />
<meta itemprop="name" content="basic_string::data - cpprefjp C++日本語リファレンス" />
<meta property="og:title" content="basic_string::data - cpprefjp C++日本語リファレンス" />
<meta property="og:url" content="https://cpprefjp.github.io/reference/string/basic_string/data.html" />
<meta property="og:site_name" content="cpprefjp - C++日本語リファレンス" />
<meta property="og:type" content="article" />
<meta property="og:description" content="文字配列表現を取得する。" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="basic_string::data - cpprefjp C++日本語リファレンス" />
<meta name="twitter:url" content="https://cpprefjp.github.io/reference/string/basic_string/data.html" />
<meta name="twitter:description" content="文字配列表現を取得する。" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="https://cpprefjp.github.io/rss.xml" />
<link rel="apple-touch-icon" sizes="180x180" href="../../../static/favicons/apple-touch-icon.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="icon" type="image/png" sizes="32x32" href="../../../static/favicons/favicon-32x32.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="icon" type="image/png" sizes="16x16" href="../../../static/favicons/favicon-16x16.png?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<link rel="manifest" href="../../../manifest.json?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<meta name="theme-color" content="#f5f8fc">
<link rel="stylesheet" href="../../../static/pygments/default.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95">
<!-- <link rel="stylesheet" href="../../../static/css/root.css"> -->
<link href="../../../static/kunai/css/kunai-stage-0.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../../static/kunai/css/kunai-stage-1.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../../static/kunai/css/kunai-stage-2.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<link href="../../../static/kunai/css/kunai-stage-3.css?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95" rel="stylesheet">
<script type="text/javascript" src="../../../static/kunai/js/kunai-vendor.js?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95"></script>
<script type="text/javascript" src="../../../static/kunai/js/kunai.js?cachebust=be86fa2321ebe02b6955b61b98b778e377bcbf95"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var kn = new Kunai;
kn.cpprefjp();
});
</script>
</head>
<body>
<header data-kunai-mdinfo="{"meta": {"header": ["string"], "namespace": ["std"], "class": ["basic_string"], "id-type": ["function"]}, "sources": [{"id": "52aa0cf65d0e5f5ff90d71522e82f01ca40c472c", "source": "#include <string>\n#include <cstdio>\n#include <cstring>\n\nint main()\n{\n std::string s(\"Hello, world!\\n\");\n std::fwrite(s.data(), 1, s.size(), stdout);\n}\n"}], "page_id": ["reference", "string", "basic_string", "data"]}">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../../../index.html">
<div class="title-wrapper clearfix">
<div class="title">cpprefjp - C++日本語リファレンス</div>
</div>
</a>
</div>
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>
<div class="google-search">
<script>
(function() {
var cx = '013316413321391058734:ji_u66hl7hq';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<div class="gcse-search"></div>
</div>
</li>
<li>
<a href="https://github.com/cpprefjp/site">GitHub Project</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<main id="main" role="main">
<div class="container-fluid">
<div class="row">
<div class="col-sm-9 col-sm-push-3" itemscope itemtype="http://schema.org/Article">
<div class="row">
<div class="col-sm-12 google-search-result">
<gcse:searchresults></gcse:searchresults>
</div>
</div>
<div class="row">
<div class="col-sm-12 content-header">
<ol class="breadcrumb">
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../../index.html" itemprop="url">
<i class="fa fa-fw fa-home"></i>
</a>
</span>
</li>
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../../reference.html" itemprop="url">
<span itemprop="name">リファレンス</span>
</a>
</span>
</li>
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../../reference/string.html" itemprop="url">
<span itemprop="name">string</span>
</a>
</span>
</li>
<li itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<a href="../../../reference/string/basic_string.html" itemprop="url">
<span itemprop="name">basic_string</span>
</a>
</span>
</li>
<li class="active" itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<span>
<span itemprop="name">data</span>
</span>
</li>
</ol>
<div class="crsearch"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12 edit-button">
<p class="text-right"><small>
最終更新日時(UTC):
<span itemprop="datePublished" content="2022-10-06T09:26:35">
2022年10月06日 09時26分35秒
</span>
<br/>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span itemprop="name">Akira Takahashi</span>
</span>
が更新
</small></p>
<p class="text-right">
<a class="history" target="_blank" href="https://github.com/cpprefjp/site/commits/master/reference/string/basic_string/data.md">
<span class="fa fa-fw fa-clock-o fa-flip-horizontal"></span>履歴
</a>
<a class="edit" target="_blank" href="https://github.com/cpprefjp/site/edit/master/reference/string/basic_string/data.md">
<span class="fa fa-fw fa-pencil"></span>編集
</a>
</p>
</div>
</div>
<div class="row">
<div class="col-sm-12 content-body">
<div class="identifier-type">function</div><div class="header"><string></div><h1 itemprop="name"><span class="namespace" title="namespace std">std::</span><span class="class" title="class basic_string">basic_string::</span><span class="token">data</span></h1>
<div itemprop="articleBody"><p><div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">charT</span><span class="o">*</span><span class="w"> </span><span class="nf">data</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="p">;</span><span class="w"> </span><span class="c1">// (1) C++03</span>
<span class="k">const</span><span class="w"> </span><span class="n">charT</span><span class="o">*</span><span class="w"> </span><span class="nf">data</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">noexcept</span><span class="p">;</span><span class="w"> </span><span class="c1">// (1) C++11</span>
<span class="k">constexpr</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">charT</span><span class="o">*</span><span class="w"> </span><span class="nf">data</span><span class="p">()</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">noexcept</span><span class="p">;</span><span class="w"> </span><span class="c1">// (1) C++20</span>
<span class="n">charT</span><span class="o">*</span><span class="w"> </span><span class="nf">data</span><span class="p">()</span><span class="w"> </span><span class="k">noexcept</span><span class="p">;</span><span class="w"> </span><span class="c1">// (2) C++17</span>
<span class="k">constexpr</span><span class="w"> </span><span class="n">charT</span><span class="o">*</span><span class="w"> </span><span class="nf">data</span><span class="p">()</span><span class="w"> </span><span class="k">noexcept</span><span class="p">;</span><span class="w"> </span><span class="c1">// (2) C++20</span>
</code></pre></div>
</p>
<h2>概要</h2>
<p>文字配列表現を取得する。</p>
<ul>
<li>(1) : 読み取り専用で文字配列の先頭へのポインタを取得する</li>
<li>(2) : 文字配列の先頭へのポインタを取得する</li>
</ul>
<h2><a class="cpprefjp-defined-word" data-desc="関数呼び出し式の評価結果となるオブジェクト・値">戻り値</a></h2>
<ul>
<li>(1) :<ul>
<li>C++03 : 文字配列の先頭へのポインタを返す。ただし、NULLで終端はされない</li>
<li>C++11 : C 言語で使用されている文字列表現である、NULL(つまり <code>charT()</code>)で終端された文字配列の先頭へのポインタを返す</li>
</ul>
</li>
<li>(2) :<ul>
<li>C 言語で使用されている文字列表現である、NULL(つまり <code>charT()</code>)で終端された文字配列の先頭へのポインタを返す</li>
</ul>
</li>
</ul>
<h2><a class="cpprefjp-defined-word" data-desc="関数等の意味論を構成する要素の1つ。Complexity。関数が使うアルゴリズムの空間計算量・時間計算量">計算量</a></h2>
<ul>
<li>(1), (2) : 定数時間</li>
</ul>
<h2>備考</h2>
<ul>
<li>(1) :<ul>
<li>C++03まで :<ul>
<li>本メンバ関数で返されたポインタが指す配列の値を変更してはいけない。</li>
<li>本メンバ関数を呼び出すと、対象オブジェクトの要素への既存の参照、ポインタ、イテレータは無効になる可能性がある。</li>
<li>本メンバ関数で返されたポインタは、対象オブジェクトに対する非constメンバ関数呼び出しにより無効になる可能性がある。<ul>
<li>なお、規格書に記載はないものの、<code><a href="../basic_string.html">basic_string</a></code> への非const参照を引数に取る標準ライブラリ関数を、対象オブジェクトを渡して呼び出した場合にも、無効になる可能性があるものと思われる</li>
</ul>
</li>
<li>本メンバが返すポインタは、<code><a href="size.html">size</a>() != 0</code> の場合、長さが <code><a href="size.html">size</a>()</code> の <code>charT</code> 型の配列を指す。この配列の要素は対象オブジェクトの文字列と等しい。</li>
<li><code><a href="size.html">size</a>() == 0</code> の場合、NULL ポインタ<strong>ではなく</strong>、当該ポインタのコピー、および、当該ポインタに対してゼロの加算が可能な値である。</li>
<li><code><a href="c_str.html">c_str()</a></code> と異なり、本メンバ関数で返る配列は NULL(<code>charT()</code>)で終端されていないので、注意すること。(通常は、<code><a href="size.html">size</a>()</code> と併せて利用することになるだろう)</li>
</ul>
</li>
<li>C++11から :<ul>
<li><code><a href="c_str.html">c_str()</a></code>メンバ関数と同一の動作をする。</li>
<li>本メンバ関数を呼び出しても、対象オブジェクトの要素への既存の参照、ポインタ、イテレータは無効にはならない。</li>
<li>本メンバ関数で返されたポインタは、対象オブジェクトに対する非constメンバ関数呼び出しにより無効になる可能性がある。</li>
<li>本メンバが返すポインタは、長さが <code><a href="size.html">size</a>() + 1</code> の <code>charT</code> 型の配列を指す。この配列は、最初の <code><a href="size.html">size</a>()</code> 要素は対象オブジェクトの文字列と等しく、最後の要素は NULL 文字、すなわち <code>charT()</code> である。 <ul>
<li>本メンバ関数が返すポインタを <code>p</code> とすると、範囲 <code>[0, <a href="size.html">size</a>()]</code> の全ての <code>i</code> について <code>p + i == <a href="../../memory/addressof.html">addressof</a>(operator[](i))</code> を満たす。このことから、<code>*<a href="end.html">end</a>() == charT()</code> を満たす。</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>対象オブジェクト内に NULL 文字があった場合、C 言語の文字列表現では正しく扱うことができないので注意すること。</li>
<li>(2) :<ul>
<li>この関数を使用するユーザーは、<code>p + <a href="size.html">size()</a></code> (NULL終端) に格納されている値を変更してはならない</li>
</ul>
</li>
</ul>
<h2>例</h2>
<p><div class="yata" id="52aa0cf65d0e5f5ff90d71522e82f01ca40c472c"><div class="codehilite"><pre><span></span><code><span class="cp">#include <a href="../../string.html"><string></a></span>
<span class="cp">#include <a href="../../cstdio.html"><cstdio></a></span>
<span class="cp">#include <a href="../../cstring.html"><cstring></a></span>
<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="n"><a href="../basic_string.html">std::string</a></span><span class="w"> </span><span class="n">s</span><span class="p">(</span><span class="s">"Hello, world!</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">fwrite</span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n"><span style="color:#ff0000">data()</span></span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n"><a href="size.html">s.size()</a></span><span class="p">,</span><span class="w"> </span><span class="n">stdout</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
</div></p>
<h3>出力</h3>
<p><pre><code>Hello, world!
</code></pre></p>
<h2>関連項目</h2>
<table border="1" bordercolor="#888" style="border-collapse:collapse">
<thead>
<tr>
<th>名前</th>
<th>説明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a href="c_str.html">c_str</a></code></td>
<td>C 言語での文字列表現を取得する</td>
</tr>
</tbody>
</table>
<h2>参照</h2>
<ul>
<li><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0272r1.html" target="_blank">P0272R1 Give <code>std::string</code> a non-const <code>.data()</code> member function.</a></li>
<li><a href="https://wg21.cmeerw.net/lwg/issue3131" target="_blank">LWG Issue 3131. <code>addressof</code> all the things</a></li>
<li><a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0980r1.pdf" target="_blank">P0980R1 Making <code>std::string</code> constexpr</a></li>
</ul></div>
</div>
</div>
</div>
<div id="sidebar" class="col-sm-3 col-sm-pull-9">
</div>
</div>
</div>
</main>
<footer class="footer navbar navbar-default">
<div class="container-fluid">
<p><small>
本サイトの情報は、
<a href="https://creativecommons.org/licenses/by/4.0/deed.ja" rel="nofollow">クリエイティブ・コモンズ 表示 4.0 非移植 ライセンス(CC BY)</a>
の下に提供されています。
</small></p>
</div>
</footer>
</body>
</html>