C++ STL之array
来源:互联网 发布:广州数控g72编程实例 编辑:程序博客网 时间:2024/04/29 13:00
Array 容器的相关知识,array是一个顺序容器,和其他标准容器相比它的特点是容器的大小固定,顺序存储。
1:array的构造函数
array();
array(const array &right);
2:array的成员变量
Type Definition
Description
array::const_iterator
The type of a constant iterator for the controlledsequence.
array::const_pointer
The type of a constant pointer to anelement.
array::const_reference
The type of a constant reference to anelement.
array::const_reverse_iterator
The type of a constant reverse iterator for thecontrolled sequence.
array::difference_type
The type of a signed distance between twoelements.
array::iterator
The type of an iterator for the controlledsequence.
array::pointer
The type of a pointer to an element.
array::reference
The type of a reference to an element.
array::reverse_iterator
The type of a reverse iterator for the controlledsequence.
array::size_type
The type of an unsigned distance between twoelements.
array::value_type
The type of an element.
3:array的关于迭代器的成员函数
Iterators
begin
end
rbegin
rend
cbegin
cend
crbegin
crend
这些东西和list中迭代器都类似,在list篇中已经做过大量介绍,这里就不再啰嗦了。
4:array中关于容量的函数
Capacity
size
max_size
empty
4.1 size()函数的用法,从结果中可以看出array容量的一些端倪来。
size_type size() const;
#include <iostream>
#include <array>
int main ()
{
int
std::array< ,5> myints;
"size of myints: "
std::cout << << myints.size() << std::endl;
"sizeof(myints): "
std::cout << <<
sizeof(myints) << std::endl;
return
0;
}
结果:
size of myints: 5
sizeof(myints): 20
4.2 max_size()函数的用法,说明这个函数和list中的max_size()的不同
size_type max_size() const;
#include <iostream>
#include <array>
int main ()
{
int
std::array< ,10> myints;
"size of myints: "
std::cout << << myints.size() <<
'\n';
"max_size of myints: "
std::cout << << myints.max_size() <<
'\n';
return
0;
}
结果:
size of myints: 10
max_size of myints: 10
4.3empty函数
bool empty() const;
5.array中关于元素操作的函数
Element access
operator[]
at
front
back
data
5.1operator[]操作符
reference operator[](size_type off);
const_reference operator[](size_type off) const;
示例:
#include <iostream>
#include <array>
int main ()
{
int
std::array< ,10> myarray;
unsigned
int
i;
for
(i=0; i<10; i++) myarray[i]=i;
// assign some values:
"myarray contains:"
std::cout << ;
// print content
for
(i=0; i<10; i++)
' '
std::cout << << myarray[i];
'\n'
std::cout << ;
return
0;
}
输出结果:
myarray contains: 0 1 2 3 4 5 6 7 8 9
5.2at()函数的用法
reference at(size_type off);
const_reference at(size_type off) const;
用法:
#include <iostream>
#include <array>
int main ()
{
int
std::array< ,10> myarray;
for
(
inti=0; i<10; i++)
myarray.at(i) = i+1;
// assign some values:
"myarray contains:" // print content:
std::cout << ;
for
(
inti=0; i<10; i++)
' '
std::cout << << myarray.at(i);
'\n'
std::cout << ;
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 ()
{
int
std::array< ,3> myarray = {2, 16, 77};
"front is: "
std::cout << << myarray.front() << std::endl;
// 2
"back is: "
std::cout << << myarray.back() << std::endl;
// 77
myarray.front() = 100;
"myarray now contains:"
std::cout << ;
for
(
int& x : myarray ) std::cout <<
' '<< x;
'\n'
std::cout << ;
return
0;
}
结果:
front is: 2
back is: 77
myarray 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";
char
std::array< ,12> charray;
std::memcpy (charray.data(),cstr,12);
'\n'
std::cout << charray.data() << ;
return
0;
}
结果:
Test string
6 修改元素的函数
Modifiers
fill
swap
6.1 fill函数的用法
void fill(const Type& _Val);
函数将array中所有的元素替换成_val
#include <iostream>
#include <array>
int main () {
int
std::array< ,6> myarray;
myarray.fill(5);
"myarray contains:"
std::cout << ;
for
(
int& x : myarray) { std::cout <<
' '<< x; }
'\n'
std::cout << ;
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 ()
{
int
std::array< ,5> first = {10, 20, 30, 40, 50};
int
std::array< ,5> second = {11, 22, 33, 44, 55};
first.swap (second);
"first:"
std::cout << ;
for
(
int& x : first) std::cout <<
' '<< x;
'\n'
std::cout << ;
"second:"
std::cout << ;
for
(
int& x : second) std::cout <<
' '<< x;
'\n'
std::cout << ;
return
0;
}
结果:
first contains: 11 22 33 44 55
second contains: 10 20 30 40 50
附加<array>头文件中其他跟array类无关的类以及函数
1:tuple_element 类
template<int Idx, class Ty, std::size_t N>
class tuple_element<Idx, <array<Ty, N> > {
typedef Ty type;
};
这个类就是获取array中第几个元素的值,示例如下:
#include <array>
#include <iostream>
typedef std::array<int, 4> Myarray;
int main()
{
Myarray c0 = {0, 1, 2, 3};
// display contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
// display first element " 0"
std::tuple_element<0, Myarray>::type val = c0.front();
std::cout << " " << val;
std::cout << std::endl;
return (0);
}
结果:
0 1 2 3
0
2:tuple_size类
template<class Ty, std::size_t N>
class tuple_size<array<Ty, N> > {
static const unsigned value = N;
};
这个类就是获取array数组的大小,示例
#include <array>
#include <iostream>
typedef std::array<int, 4> Myarray;
int main()
{
Myarray c0 = {0, 1, 2, 3};
// display contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
// display size " 4"
std::cout << " " << std::tuple_size<Myarray>::value;
std::cout << std::endl;
return (0);
}
结果:
0 1 2 3
4
3:get函数的用法
template<int Idx, class Ty, std::size_t N>
Ty& get(array<Ty, N>& arr);
template<int Idx, class Ty, std::size_t N>
const Ty& get(const array<Ty, N>& arr);
这个函数就是返回array[id]的索引,示例
#include <array>
#include <iostream>
typedef std::array<int, 4> Myarray;
int main()
{
Myarray c0 = {0, 1, 2, 3};
// display contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
// display odd elements " 1 3"
std::cout << " " << std::get<1>(c0);
std::cout << " " << std::get<3>(c0);
std::cout << std::endl;
return (0);
}
结果:
0 1 2 3
1 3
4.swap函数的用法
template<class Ty, std::size_t N>
void swap(
array<Ty, N>& left,
array<Ty, N>& right);
交换两个数组的值,示例
#include <array>
#include <iostream>
typedef std::array<int, 4> Myarray;
int main()
{
Myarray c0 = {0, 1, 2, 3};
// display contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
Myarray c1 = {4, 5, 6, 7};
c0.swap(c1);
// display swapped contents " 4 5 6 7"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
swap(c0, c1);
// display swapped contents " 0 1 2 3"
for (Myarray::const_iterator it = c0.begin();
it != c0.end(); ++it)
std::cout << " " << *it;
std::cout << std::endl;
return (0);
}
结果:
0 1 2 3
4 5 6 7
0 1 2 3
- C++ STL之array
- STL::array之数组容器
- C++STL之multimap
- C++STL之string
- C++STL之迭代器
- C STL 之算法
- C++STL之string
- C++STL 之 迭代器
- C++STL之string
- C++STL之string
- C++STL之迭代器
- c++STL之string
- c++STL之vector
- C++STL之组件
- C++STL之vector
- C++STL之迭代器
- C++STL之map
- C++STL之string
- listbox下拉清单控件程序注释
- NSBundle介绍
- 基于C#的最简单的音乐播放器
- C++ STL之list
- Qt下连接mysql
- C++ STL之array
- C++ STL之vector
- C++ STL之set
- 十大排序算法
- 计算机学论文
- 软件测试——boost单元测试 C++
- 将博客搬至CSDN
- 开始更新博客
- div 背景颜色透明度设置