-
Notifications
You must be signed in to change notification settings - Fork 0
/
STL1_array.cpp
87 lines (53 loc) · 2.56 KB
/
STL1_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
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
#include <bits/stdc++.h>
using namespace std;
/*
NOTE:-
in Global scope :
Valid size of array of type ( int , float , double) is 10^7
and for array of type ( bool) it is 10^8.
but in local scope :
Valid size of array of type ( int , float , double) is 10^6
and for array of type ( bool) it is 10^7
*/
int main()
{
// syntax : array< data_type , size_of_array > array_name;
array<int,20> age;
// 1. Declarations of an array :
array<int,6>a; // garbage value at six places in memory
array<int,5>b{0}; // { 0 , 0 , 0 , 0 , 0 }
array<int,5>c{1}; // { 1 , 0 , 0 , 0 , 0 }
array<int,6>d{1,2,3}; // { 1 , 2 , 3 , 0 , 0 , 0 }
array<int,6>arr;
arr = { 1, 2 ,3 ,4 , 5 ,6}; // { 1, 2 ,3 ,4 , 5 ,6}
// 2. Functions on array:
arr.at(2); // point to the element at index = 2 i.e '3'
arr.begin(); // points to arr[0]
arr.end(); // points to arr[size + 1] i.e one right to the end of array
arr.size(); // size = 6
arr.front(); // returns element at begining of array i.e a[0] = 1
arr.back(); // returns element at ending of array i.e a[5] = 6
arr.fill(9); // { 9 , 9 , 9 , 9 , 9 , 9 }
sort(arr.begin() , arr.end()); // sorts an array in increasing order : 1, 2 ,3 ,4 ,5
// 3. accessing or printing an array
array<int,7>temp = {1,2,3,4,5,6,7};
// 3.1 Using a 'for' loop-
for(int i = 0 ; i<temp.size();i++)
{
cout<<temp[i]<<" ";
}
cout<<endl;
// 3.2 Using an 'iterator' or 'pointer' -
array<int,6>::iterator it = temp.begin();
for(it = temp.begin(); it!=temp.end();it++)
{
cout<<(*it)<<" ";
}
cout<<endl;
// 3.3 Using a 'for each ' loop and 'auto' keyword-
for(auto i : temp)
{
cout<<i<<" ";
}
return 0;
}