cpp-array

website

固定长度的数组,传递给函数时不会无效

#include <array>

std::array<int, 3> myArray; // declare an integer array with length 3

std::array<int, 5> myArray = { 9, 7, 5, 3, 1 }; // initializer list
std::array<int, 5> myArray2 { 9, 7, 5, 3, 1 }; // uniform initialization

// c++ 17 可以忽略类型和长度
std::array myArray { 9, 7, 5, 3, 1 }; // The type is deduced to std::array<int, 5>
std::array myArray { 9.7, 7.31 }; // The type is deduced to std::array<double, 2>

//You can also assign values to the array using an initializer list
std::array<int, 5> myArray;
myArray = { 0, 1, 2, 3, 4 }; // okay
myArray = { 9, 8, 7 }; // okay, elements 3 and 4 are set to zero!
myArray = { 0, 1, 2, 3, 4, 5 }; // not allowed, too many elements in initializer list!

//Accessing std::array values using the subscript operator works just like you would expect:
std::cout << myArray[1] << '\n';
myArray[2] = 6;

//std::array supports a second form of array element access (the at() function) that does bounds checking:
std::array myArray { 9, 7, 5, 3, 1 };
myArray.at(1) = 6; // array element 1 valid, sets array element 1 to value 6
myArray.at(9) = 10; // array element 9 is invalid, will throw an error

/*
In the above example, the call to array.at(1) checks to ensure array element 1 is valid, and because it is, it returns a reference to array element 1. We then assign the value of 6 to this. However, the call to array.at(9) fails because array element 9 is out of bounds for the array. Instead of returning a reference, the at() function throws an error that terminates the program (note: It’s actually throwing an exception of type std::out_of_range -- we cover exceptions in chapter 15). Because it does bounds checking, at() is slower (but safer) than operator[].
*/

//std::array will clean up after itself when it goes out of scope, so there’s no need to do any kind of manual cleanup.

std::array myArray { 9.0, 7.2, 5.4, 3.6, 1.8 };
std::cout << "length: " << myArray.size() << '\n';

//
for (int element : myArray)
std::cout << element << ' ';

use as parameter

// use as parameter
#include <array>
#include <iostream>

void printLength(const std::array<double, 5> &myArray)
{
std::cout << "length: " << myArray.size() << '\n';
}

int main()
{
std::array myArray { 9.0, 7.2, 5.4, 3.6, 1.8 };

printLength(myArray);

return 0;
}

sort

#include <algorithm> // for std::sort
#include <array>
#include <iostream>

int main()
{
std::array myArray { 7, 3, 1, 9, 5 };
std::sort(myArray.begin(), myArray.end()); // sort the array forwards
// std::sort(myArray.rbegin(), myArray.rend()); // sort the array backwards

for (int element : myArray)
std::cout << element << ' ';

std::cout << '\n';

return 0;
}

Array of struct

#include <array>
#include <iostream>

struct House
{
int number{};
int stories{};
int roomsPerStory{};
};

int main()
{
std::array<House, 3> houses{};

houses[0] = { 13, 4, 30 };
houses[1] = { 14, 3, 10 };
houses[2] = { 15, 3, 40 };

for (const auto& house : houses)
{
std::cout << "House number " << house.number
<< " has " << (house.stories * house.roomsPerStory)
<< " rooms\n";
}

return 0;
}
#include <iostream>

struct House
{
int number{};
int stories{};
int roomsPerStory{};
};

struct Array
{
House value[3]{};
};

int main()
{
// With braces, this works.
Array houses{
{ { 13, 4, 30 }, { 14, 3, 10 }, { 15, 3, 40 } }
};

for (const auto& house : houses.value)
{
std::cout << "House number " << house.number
<< " has " << (house.stories * house.roomsPerStory)
<< " rooms\n";
}

return 0;
}
#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>

int main()
{
// construction uses aggregate initialization
std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to the CWG 1270 revision
// (not needed in C++11 after the revision and in C++14 and beyond)
std::array<int, 3> a2 = {1, 2, 3}; // never required after =
std::array<std::string, 2> a3 = { std::string("a"), "b" };

// container operations are supported
std::sort(a1.begin(), a1.end());
std::reverse_copy(a2.begin(), a2.end(),
std::ostream_iterator<int>(std::cout, " "));

std::cout << '\n';

// ranged for loop is supported
for(const auto& s: a3)
std::cout << s << ' ';
}