Array是一个顺序容器,和其他标准容器相比它的特点是容器的大小固定,顺序存储。
1:array的构造函数
array();
array(const array & right);
2:array的成员变量 [L]Type Definition[/L] [L]Description[/L] [L]array::const_iterator[/L] [L]The type of a constant iterator for the controlled sequence.[/L] [L]array::const_pointer[/L] [L]The type of a constant pointer to an element.[/L] [L]array::const_reference[/L] [L]The type of a constant reference to an element.[/L] [L]array::const_reverse_iterator[/L] [L]The type of a constant reverse iterator for the controlled sequence.[/L] [L]array::difference_type[/L] [L]The type of a signed distance between two elements.[/L] [L]array::iterator[/L] [L]The type of an iterator for the controlled sequence.[/L] [L]array::pointer[/L] [L]The type of a pointer to an element.[/L] [L]array::reference[/L] [L]The type of a reference to an element.[/L] [L]array::reverse_iterator[/L] [L]The type of a reverse iterator for the controlled sequence.[/L] [L]array::size_type[/L] [L]The type of an unsigned distance between two elements.[/L] [L]array::value_type[/L] [L]The type of an element.[/L]
3:array的关于迭代器的成员函数
[L]Iterators[/L]
[L]begin Return iterator to beginning (public member function )[/L]
[L]end Return iterator to end (public member function )[/L]
[L]rbegin[/B] Return reverseiterator to reverse beginning (public member function )[/L]
[L]rend[/B] Returnreverse iterator to reverse end (public member function )[/L]
[L]cbegin[/B] Returnconst_iterator to beginning (public member function )[/L]
[L]cend[/B] Returnconst_iterator to end (public member function )[/L]
[L]crbegin[/B] Returnconst_reverse_iterator to reverse beginning (public memberfunction )[/L]
[L]crend[/B] Returnconst_reverse_iterator to reverse end (public member function )[/L]
4:array中关于容量的函数
[L]Capacity[/L]
[L]size[/B] Returnsize (public member function )[/L]
[L]max_size[/B] Returnmaximum size (public member function )[/L]
[L]empty[/B] Testwhether array is empty (public member function )[/L]
4.1 size()函数的用法,从结果中可以看出array容量的一些端倪来。 size_type size() const; #include <iostream> #include <array> int main () { std::array<int,5> myints; std::cout << "size of myints: " << myints.size() << std::endl; std::cout << "sizeof(myints): " << sizeof(myints) << std::endl; return 0; }结果 :size of myints: 5sizeof(myints): 20 4.2 max_size()函数的用法,说明这个函数和list中的max_size()的不同 size_type max_size() const; #include <iostream> #include <array> int main () { std::array<int,10> myints; std::cout << "size of myints: " << myints.size() << '\n'; std::cout << "max_size of myints: " << myints.max_size() << '\n'; return 0; } 结果:size of myints: 10max_size of myints: 10
4.3 empty函数
bool empty() const;
5.array中关于元素操作的函数
[L]Element access[/L]
[L]operator[][/B] Accesselement (public member function )[/L]
[L]at[/B] Accesselement (public member function )[/L]
[L]front[/B] Accessfirst element (public member function )[/L]
[L]back[/B] Accesslast element (public member function )[/L]
[L]data[/B] Getpointer to data (public member function )[/L]
5.1 operator[]操作符
reference operator[](size_type off); const_reference operator[](size_type off) const; 示例: #include <iostream> #include <array> int main () { std::array<int,10> myarray; unsigned int i; for (i=0; i<10; i++) myarray=i; // assign some values: std::cout << "myarray contains:"; // print content for (i=0; i<10; i++) std::cout <<' '<< myarray; std::cout << '\n'; return 0;
}
输出结果:
myarray contains: 0 1 2 3 4 5 6 7 8 9
5.2 at()函数的用法
reference at(size_type off); const_reference at(size_type off) const;
用法:
#include <iostream> #include <array> int main () { std::array<int,10> myarray; for (int i=0; i<10; i++) myarray.at(i) = i+1; // assign some values: std::cout << "myarray contains:" // print content:; for (int i=0; i<10; i++) std::cout << ' ' << myarray.at(i); std::cout << '\n'; return 0; } 输出结果: myarray contains: 1 2 3 4 5 6 7 8 9 10 5.3 front函数的用法 reference front(); const_reference front() const; 示例: #include <iostream> #include <array> int main () { std::array<int,3> myarray = {2, 16, 77}; std::cout << "front is: " << myarray.front() << std::endl; // 2 std::cout << "back is: " << myarray.back() << std::endl; // 77 myarray.front() = 100; std::cout << "myarray now contains:"; for ( int& x : myarray ) std::cout << ' ' << x; std::cout << '\n'; return 0; }结果:front is: 2back is: 77myarray now contains: 100 16 77 5.4 back函数的用法reference back(); const_reference back() const; 关于例子,在5.3中的例子已经包含了5.5 data函数的用法Ty *data(); const Ty *data() const; 返回值指向第一个元素的指针,因为是顺序存储,所以知道了首元素的指针就可以知道所有元素了。 #include <iostream> #include <cstring> #include <array> int main () { const char* cstr = "Test string"; std::array<char,12> charray; std::memcpy (charray.data(),cstr,12); std::cout << charray.data() << '\n'; return 0; } 结果:Test string6 修改元素的函数
[L]Modifiers[/L]
[L]fill[/B] Fill arraywith value (public member function )[/L]
[L]swap[/B] Swap content (public member function )[/L]
6.1 fill函数的用法void fill(const Type& _Val); 函数将array中所有的元素替换成_val #include <iostream> #include <array> int main () { std::array<int,6> myarray; myarray.fill(5); std::cout << "myarray contains:"; for ( int& x : myarray) { std::cout << ' ' << x; } std::cout << '\n'; return 0; }结果:myarray contains: 5 5 5 5 5 5 6.2 swap函数的用法void swap(array& right); 交换两个具有相同长度的array,切记两个array的长度必须一致例子: #include <iostream> #include <array> int main () { std::array<int,5> first = {10, 20, 30, 40, 50}; std::array<int,5> second = {11, 22, 33, 44, 55}; first.swap (second); std::cout << "first:"; for (int& x : first) std::cout << ' ' << x; std::cout << '\n'; std::cout << "second:"; for (int& x : second) std::cout << ' ' << x; std::cout << '\n'; return 0; } 结果 :first contains: 11 22 33 44 55second contains: 10 20 30 40 50 参考网站: http://wenku.baidu.com/link?url=fVhoz8y-kXwhcBs1aq0-zcNe8q1lQY8NXFMI7_YWEOGlheZQBYWafojiy36qvnH_7hcrU8kDoBR5VAgnO-wm15nMtvX89C8Hq_1ghp64Cim
|