热爱互联网

c++ 11 —- array

http://www.cplusplus.com/reference/array/array/
一个数组而已,下面是例子:

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
#include <iostream>
#include <array>
#include <cstring>
#include <tuple>
using namespace std;
 
int main()
{
	array<int,5> arr = {1,3,4,5,2};
 
	/* iterators: begin,end,rbegin,rend,cbegin,cend,crbegin,crend  */
	// 1 3 4 5 2
	for(auto iter = arr.begin(); iter != arr.end() ; ++iter)
		cout << *iter << " ";
	cout << endl;
	// can't modify *iter
	for(auto iter = arr.cbegin(); iter != arr.end(); ++iter)
		cout << *iter << " ";
	cout << endl;
 
	// 2 5 4 3 1
	for(auto iter = arr.rbegin(); iter != arr.rend(); ++iter)
		cout << *iter << " ";
	cout << endl;
	// can't modify *iter
	for(auto iter = arr.crbegin(); iter != arr.crend(); ++iter)
		cout << *iter << " ";
	cout << endl;
 
 
	/*  capacity: size, max_size, empty  */
	// size and max_size of an array object always match.
	cout << arr.size() <<endl;   // 5
	cout << arr.max_size() <<endl; // 5
	cout << arr.empty() <<endl; // 0
 
 
	/*  elements access: operator[], at, front, back, data  */
	// arr[n],arr.at(n);  array::at checks the array bounds and
	// signals whether n is out of range  by throwing an exception.
	// array::data, return pointer to the data contained by the array object.
	for(int i=0; i < arr.size(); ++i)
		cout << arr[i] << " ";
	cout << endl;
 
	for(int i=0; i < arr.size(); ++i)
		cout << arr.at(i) << " ";
	cout << endl;
 
	cout << arr.front() <<endl; // 1
	cout << arr.back() <<endl;  // 2
 
	array<char,5> arr1 = {'h','e','l','l','o' };
	cout << arr1.data() <<endl;  // hello
 
	const char* cstr = "hello world";
	array<char,11> arr2;
	memcpy(arr2.data(),cstr,11);
	cout << arr2.data() <<endl; // hello world
 
 
	/*  modifiers: fill, swap  */
	array<int,5> arr3;
	arr3.fill(6);
	for(auto x : arr3)   // 6 6 6 6 6
		cout << x << " ";
	cout <<endl;
 
	array<int,5> arr4;
	arr4.fill(9);
	arr4.swap(arr3); // swap
 
	for(auto x : arr4) // 6 6 6 6 6
		cout << x << " ";
	cout << endl;
 
	for(auto x : arr3) // 9 9 9 9 9
		cout << x << " ";
	cout << endl;
 
	/* non-member function overloads. get(array), relational operators(array) */
	array<int,3> arr5 = {10,20,30};
	tuple<int,int,int> tu(1,2,3);
 
	tuple_element<0,decltype(arr5)>::type myelement;
	myelement = get<2>(arr5);
	get<2>(arr5) = get<0>(arr5);
	get<0>(arr5) = myelement;
 
	cout << get<0>(arr5) << " " << get<2>(arr5) <<endl;
	cout << get<0>(tu) << " " << get<1>(tu) << " " <<get<2>(tu) << endl;
 
	// array comparisons
	array<int,3> a = { 1,2,3 };
	array<int,3> b = { 1,2,3 };
	array<int,3> c = { 4,5,6 };
 
	if( a == b ) cout << "a and b are equal" <<endl;
	if( b != c ) cout << "b and c are not equal" <<endl;
	if( b < c )  cout << "b is less than c" <<endl;
	if( c > b )  cout << "c is greater than b" <<endl;
	if( a <= b ) cout << "a is less than or equal to b" <<endl;
	if( a >= b ) cout << "b is greater than or equal to a" <<endl;
 
 
 
	/*  non-member class specializations.
	 *  tuple_element<array>, tuple_size<array>
	 *  */
	// tuple_element
	auto tu1 = make_tuple(10,'a');
	array<int,4> d = {1,2,3,4};
 
	tuple_element<0,decltype(tu1)>::type first = get<0>(tu1);
	tuple_element<1,decltype(tu1)>::type second = get<1>(tu1);
	cout << first << " " << second <<endl;   // 10 a
 
	tuple_element<2,decltype(d)>::type third = get<2>(d);
	cout << third << endl;  // 3
 
	// tuple_size
	cout << tuple_size<decltype(tu1)>::value << endl;
 
	cout << tuple_size<decltype(d)>::value << endl;
 
	return 0;
}

Post a Comment

Your email is kept private. Required fields are marked *