c++的STL的array

#include <iostream>
#include <array>
using namespace std;


int main(){
// array<int, 12> arr = { 1, 2, 3, 4, 5, 6, 10, 11, 15, 78, 52, 110};  //type ,size
// /***********************Member functions**********************/
// //1 get the data----begin()
// //Returns an iterator which points to the start of the array.
// auto itr = arr.begin();
// while (itr != arr.end()){
// cout << *itr << " ";
// ++itr;
// }
// cout << "==begin()\n";
//
// //2 get one element----at()
// //Returns a reference to the element present at location N in given array container.
// for (int i = 0; i < 5; i++){
// cout << arr.at(i) << " ";
// }
// cout << "==at\n";
// try{
// arr.at(10);
// }
// catch (out_of_range e){
// cout << "out of range" << endl;
// }
//
// //3 get the last element---back()
// //Returns a reference to the last element of the array container.
// cout << "last element of arr: " << arr.back() << endl;
// arr.back() = 50;
// cout << "last element of arr: " << arr.back() << endl;
//
// //4 cbegin()----Returns a constant iterator which points to the start of the array.
// auto itr2 = arr.cbegin();
// while (itr2 < arr.end()){
// cout << *itr2 << " ";
// ++itr2;
// }
// cout << "==cbegin()\n";
// auto itr3 = arr.cbegin();
// //*itr3=100; //show the wrong infromation
//
// //5 crbegin() returns a constant reverse iterator pointing to the last element of the array.
// for (auto itr4 = arr.crbegin(); itr4 != arr.crend(); ++itr4)
// cout << *itr4 << " ";
// cout << "==crbegin()\n";
//
// //6 crend() returns a constant reverse iterator which points to the past-end element of array. 
// auto s = arr.crbegin();
// auto e = arr.crend();
// while (s < e){
// cout << *s << " ";
// ++s;
// }
// cout << "==crend()\n";
//
// //7 data() return a pointer pointing to the first element of the array container.
// array<char, 128> arrChar = { "C++ standard library from tutorialspoint.com" };
// char *p, *q;
// /* pointer to the first element of character array. */
// p = arrChar.data();
// /* print string contents */
// cout <<"p: "<< p << endl;
// q = p;
// /* print string using pointer arithmatic */
// while (*q){
// cout << *q;
// ++q;
// }
// cout << "==data()\n";
//
// //8 empty() tests whether size of array is zero or not.
// array<int, 0>arr1;
// array<int, 10>arr2;
// if (arr1.empty())
// cout << "arr1 is empty" << endl;
// else
// cout << "arr1 is not empty " <<arr1.size()<< endl;
// if (arr2.empty())
// cout << "arr2 is empty" << endl;
// else
// cout << "arr2 is not empty "<<arr2.size()<<" ==empty()\n";
//
// //9 end() returns an iterator which points to the past-end element of array.
// auto start = arr.begin();
// auto end = arr.end();
// while (start < end){
// cout << *start << " ";
// ++start;
// }
// cout << "end() ==\n";
//
// //10 fill() sets given value to all elements of array.
// array<int, 5> arr10;
// for (int i = 0; i < 5; i++)
// arr10[i] = i + 1;
// cout << "original array\n";
// for (int i = 0; i < 5; ++i)
// cout << arr10[i] << " ";
// cout << endl;
// arr10.fill(100);
// cout << "modified array\n";
// for (int i = 0; i < 5; i++)
// cout << arr10[i] << " ";
// cout << "fill() \n";
//
// //11 front() returns reference to the first element of the array container.
// cout << "first element of arr " <<arr.front()<<endl;
// arr.front() = 1;
// cout << "after modificiton first element of arr " << arr.front();
// cout << "===front()\n ";
//
// //12 max_size() is used to get the maximum number of elements that can be held by array container.
// array<int, 10>arr12;
// cout << "maximum size of arr12 " << arr12.max_size() << endl;
// cout << "size of arr12 " << arr12.size() ;
// cout << "==max_size() \n";
//
// //13 operator[] returns reference to the element present at location n in a given array container. 
// //typedef unsigned int size_t
// //size_t是无符号的,并且是平台无关的,表示0 - MAXINT的范围
// //size_t是无符号, int是有符号的。
// //size_t是一个独立于平台的定义,长度是唯一的,而int却是和平台相关的,所以究竟是使用哪种类型,还是有一些讲究的。
// for (size_t i = 0; i < 5; i++)
// cout <<"before modified: "<< arr[i] << " ";
// cout << "[]\n";
// arr[0] = 100;
// for (size_t i = 0; i < 5; i++)
// cout << arr[i] << " ";
// cout << "modified \n";


// 14 rbegin() returns an reverse iterator pointing to the last element of the array.
    /* reverse iterator points to the last element of the array */
    array<int, 5> arr14 = { 10, 20, 30, 40, 50 };
    /* reverse iterator points to the last element of the array 从尾到头遍历 */
    auto rev_begin = arr14.rbegin();


    /* iterator array in reverse order */
    while (rev_begin < arr14.rend()) {
   cout << *rev_begin << " ";
   ++rev_begin;
    }
cout << "==rbegin() \n";


//auto end1 = arr.end();
//while (end1 > arr.begin()){
// cout << *end1 << " ";
// end1--;
//}


//15 rend() returns a reverse iterator which points to the theoretical element preceding to first element of the array.
array<int, 5>arr15 = { 10, 20, 30, 40, 50 };
auto rb = arr15.rbegin();
auto re = arr15.rend();
while (rb < re){
cout << *rb << " ";
++rb;
}
cout << "==rend()\n";


//16 size() is used to get the number of elements present in the array.
array<int, 5>int_arr;
array<float, 0>float_arr;
cout << "number of elements in int_arr " << int_arr.size() << endl;
cout << "number of elements in int_arr " << float_arr.size() << endl;


//17 swaps() swap contents of the array. 
array<int, 3>arr171 = { 1, 2, 3 };
array<int, 3>arr172={ 1, 5, 0 };
auto arr171B = arr171.begin();
while (arr171B < arr171.end()){
cout << *arr171B<<" ";
++arr171B;
}
cout << "==arr171\n";
auto arr172B = arr172.begin();
while (arr172B < arr172.end()){
cout << *arr172B<<" ";
++arr172B;
}
cout << "==arr172\n";
arr171.swap(arr172);
auto arr171B2 = arr171.begin();
while (arr171B2 < arr171.end()){
cout << *arr171B2 << " ";
++arr171B2;
}
cout << "modified==arr171\n";
auto arr172B2 = arr172.begin();
while (arr172B2 < arr172.end()){
cout << *arr172B2 << " ";
++arr172B2;
}
cout << "modified==arr172\n";


system("pause");
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值