Array是一個(gè)順序容器,和其他標(biāo)準(zhǔn)容器相比它的特點(diǎn)是容器的大小固定,順序存儲(chǔ)。
1:array的構(gòu)造函數(shù)
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的關(guān)于迭代器的成員函數(shù)
[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中關(guān)于容量的函數(shù)
[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()函數(shù)的用法,從結(jié)果中可以看出array容量的一些端倪來(lái)。 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; }結(jié)果 :size of myints: 5sizeof(myints): 20 4.2 max_size()函數(shù)的用法,說(shuō)明這個(gè)函數(shù)和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; } 結(jié)果:size of myints: 10max_size of myints: 10
4.3 empty函數(shù)
bool empty() const;
5.array中關(guān)于元素操作的函數(shù)
[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;
}
輸出結(jié)果:
myarray contains: 0 1 2 3 4 5 6 7 8 9
5.2 at()函數(shù)的用法
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; } 輸出結(jié)果: myarray contains: 1 2 3 4 5 6 7 8 9 10 5.3 front函數(shù)的用法 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; }結(jié)果:front is: 2back is: 77myarray now contains: 100 16 77 5.4 back函數(shù)的用法reference back(); const_reference back() const; 關(guān)于例子,在5.3中的例子已經(jīng)包含了5.5 data函數(shù)的用法Ty *data(); const Ty *data() const; 返回值指向第一個(gè)元素的指針,因?yàn)槭琼樞虼鎯?chǔ),所以知道了首元素的指針就可以知道所有元素了。 #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; } 結(jié)果:Test string6 修改元素的函數(shù)
[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函數(shù)的用法void fill(const Type& _Val); 函數(shù)將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; }結(jié)果:myarray contains: 5 5 5 5 5 5 6.2 swap函數(shù)的用法void swap(array& right); 交換兩個(gè)具有相同長(zhǎng)度的array,切記兩個(gè)array的長(zhǎng)度必須一致例子: #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; } 結(jié)果 :first contains: 11 22 33 44 55second contains: 10 20 30 40 50 參考網(wǎng)站: http://wenku.baidu.com/link?url=fVhoz8y-kXwhcBs1aq0-zcNe8q1lQY8NXFMI7_YWEOGlheZQBYWafojiy36qvnH_7hcrU8kDoBR5VAgnO-wm15nMtvX89C8Hq_1ghp64Cim
|