大堆 类通常比C样式的数组更高效、更轻、更可靠。C++11中引入的数组类为C风格的数组提供了更好的选择。
null
数组::fill()
此函数用于为数组容器的所有元素设置公共值。
语法:
arrayname.fill(value) Parameters : The value to be set for all the elements of the container is passed as parameter. Result : All the elements of the container are set to be equal to the parameter passed.
例如:
Input : myarray = {1, 2, 3, 4} myarray.fill(5); Output : myarray = {5, 5, 5, 5} Input : myarray = {1, 2, 3, 4, 5, 6, 7} myarray.fill(2); Output : myarray = {2, 2, 2, 2, 2, 2, 2}
错误和异常
1.如果赋值操作引发错误,则会引发错误。 2.它有一个基本的无例外抛出保证。
// CPP program to illustrate // Implementation of fill() function #include <array> #include <iostream> using namespace std; int main() { // array container declaration array< int , 4> myarray{ 1, 2, 3, 4 }; // Using fill() function to myarray.fill(5); // printing the array for ( auto it=myarray.begin(); it<myarray.end(); ++it) cout<<*it<< " " ; return 0; } |
输出:
5 5 5 5
数组::swap()
此函数用于将一个数组的内容与相同类型和大小的另一个数组交换。
语法:
arrayname1.swap(arrayname2) Parameters : The name of the array with which the contents have to be swapped. Result : All the elements of the 2 array are swapped.
例如:
Input : myarray1 = {1, 2, 3, 4} myarray2 = {3, 5, 7, 9} myarray1.swap(myarray2); Output : myarray1 = {3, 5, 7, 9} myarray2 = {1, 2, 3, 4} Input : myarray1 = {1, 3, 5, 7} myarray2 = {2, 4, 6, 8} myarray1.swap(myarray2); Output : myarray1 = {2, 4, 6, 8} myarray2 = {1, 3, 5, 7}
错误和异常
1.如果数组不是同一类型,则抛出错误。 2.如果数组大小不同,则会抛出错误。 2.它有一个基本的无例外抛出保证。
// CPP program to illustrate // Implementation of swap() function #include <array> #include <iostream> using namespace std; int main() { // array container declaration array< int , 4> myarray1{ 1, 2, 3, 4 }; array< int , 4> myarray2{ 3, 5, 7, 9 }; // using swap() function to swap elements of arrays myarray1.swap(myarray2); // printing the first array cout<< "myarray1 = " ; for ( auto it=myarray1.begin(); it<myarray1.end(); ++it) cout<<*it<< " " ; // printing the second array cout<<endl<< "myarray2 = " ; for ( auto it=myarray2.begin(); it<myarray2.end(); ++it) cout<<*it<< " " ; return 0; } |
输出:
myarray1 = 3 5 7 9 myarray2 = 1 2 3 4
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END