-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy patharrays.cpp
More file actions
executable file
·113 lines (93 loc) · 2.65 KB
/
arrays.cpp
File metadata and controls
executable file
·113 lines (93 loc) · 2.65 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
/*
* Arrays.cpp
*
* Created on: Jan 4, 2012
* Author: behnam
*/
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
// this one expect exactly 3x4
void foo(int a[4][3]) {}
// we can omit (only) the first size dimension(the most left one)
void foo(int a[][3], int size) {}
void foo(int *a[10], int size) {}
void foo(int **a, int m, int n) {}
void sendingArraytoFunctions() {
int array[2][3];
foo(array, 3);
int *p_array[10];
for (int i = 0; i < 10; i++)
p_array[i] = new int[4];
foo(p_array, 4);
int **pp_array;
int m, n;
m = 4;
n = 5;
pp_array = new int *[n];
for (int i = 0; i < m; i++)
pp_array[i] = new int[n];
int first2DArray[3][5] = {
{
1,
2,
3,
4,
5,
}, // row 0
{
6,
7,
8,
9,
10,
}, // row 1
{11, 12, 13, 14, 15} // row 2
};
foo((int **)&first2DArray[0][0], 3, 5);
}
void accessingArrayElements() {
int array[] = {1, 2, 3, 4, 5};
std::cout << "sizeof(array)=" << sizeof(array)
<< std::endl; // prints 20 (5 elements * 4 bytes each)
const int number_of_elemenets = sizeof(array) / sizeof(int);
std::cout << "number of elemenets = " << number_of_elemenets << std::endl;
// These are equal:
// array<=>&array[0]
// array+index<=>&array[index]
std::cout << "*array=" << *array << std::endl;
std::cout << "*(array+2)=" << *(array + 2) << std::endl;
for (int *p = array; p != array + number_of_elemenets; p++) {
std::cout << *p << std::endl;
}
}
void cppArrayExample() {
// std::array is a container that encapsulates fixed size arrays. // (not
// needed in C++11 after the revision and in C++14 and beyond)
std::array<int, 3> a2 = {1, 2, 3};
std::array<std::string, 2> a3 = {std::string("a"), "b"};
// container operations are supported
std::sort(a2.begin(), a2.end());
std::reverse_copy(a2.begin(), a2.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
struct Foo {};
void checkingIsArray() {
const char *s = "Behnam";
std::cout << std::boolalpha;
std::cout << std::is_array<Foo>::value << '\n';
std::cout << std::is_array<Foo[]>::value << '\n';
std::cout << std::is_array<Foo[3]>::value << '\n';
std::cout << std::is_array<float>::value << '\n';
std::cout << std::is_array<int>::value << '\n';
std::cout << std::is_array<int[]>::value << '\n';
std::cout << std::is_array<int[3]>::value << '\n';
std::cout << std::is_array<std::array<int, 3>>::value << '\n';
}
int main(int argc, char *argv[]) {
checkingIsArray();
return 0;
}