-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCStrData.cpp
64 lines (49 loc) · 1.4 KB
/
CStrData.cpp
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
/**
* \file CStrData.cpp
* \brief .c_str() and .data() are the same
*
* \see https://shaharmike.com/cpp/std-string/
*
* Calling .data() in C++98 would not necessarily return a null-terminated string.
* C++11 changes this and now both data() and c_str() return a string that must terminate with a '\0'.
*
* Another thing C++11 brings is the ability to dereference the pointer returned from
* c_str() / data() even if empty() == true.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
// No longer is it undefined to do.
// As now c_str() / data() will return a pointer to '\0'
// Not empty
{
const std::string str("test");
std::cout << STD_PRINT_VAR(str) << std::endl;
for (const char *c = str.data(); *c != 0; ++ c) {
std::cout << '\t' << STD_TRACE_VAR(c) << std::endl;
}
std::cout << "\n" << std::endl;
}
// Empty
{
const std::string str;
std::cout << STD_PRINT_VAR(str) << std::endl;
for (const char *c = str.data(); *c != 0; ++ c) {
std::cout << '\t' << STD_TRACE_VAR(c) << std::endl;
}
std::cout << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
str: [test]
c: test
c: est
c: st
c: t
str: []
#endif