forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.cpp
53 lines (38 loc) · 1.01 KB
/
array.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
//
// Created by light on 19-12-16.
//
#include <iostream>
#include <map> // std::map
#include "../container1/output_container.h"
using namespace std;
#define ARRAY_LEN(a) \
(sizeof(a) / sizeof((a)[0]))
void test(int a[8]) {
cout << ARRAY_LEN(a) << endl;
}
void test1(int arr[]) {
// 不能编译
// std::cout << std::size(arr)
// << std::endl;
}
typedef char mykey_t[8];
typedef std::array<char, 8> mykey_t1;
int main() {
int a[8];
test(a);
// C++17 直接提供了一个 size 方法,可以用于提供数组长度,
int arr[] = {1, 2, 3, 4, 5};
std::cout << "The array length is "
<< std::size(arr)
<< std::endl;
// 并且在数组退化成指针的情况下会直接失败
test1(arr);
std::map<mykey_t, int> mp;
mykey_t mykey{"hello"};
// mp[mykey] = 5;
// 轰,大段的编译错误
std::map<mykey_t1, int> mp1;
mykey_t1 mykey1{"hello"};
mp1[mykey1] = 5; // ok
cout << mp1 << endl;
}