Anonymous user
Array length: Difference between revisions
m
→{{header|C++}}: formatting
m (→{{header|C++}}: formatting) |
|||
Line 612:
C++ follows the same rules as C regarding static and dynamic arrays.
However, C++ has an additional <code>std::array</code> type (amongst other collections) in its standard library:
<lang cpp>#include <array>
#include <iostream>
#include <string>
Line 624 ⟶ 623:
std::cout << fruit.size();
return 0;
}</lang>▼
▲</lang>
Note that <code>char*</code> or <code>const char*</code> could have been used instead of <code>std::string</code>.
In addition to the <code>std::array</code> type, the C++ standard library also provides dynamically-sized containers to hold arbitrary objects.
These all support similar interfaces, though their implementations have different performance characteristics.
<lang cpp> std::vector<std::string> fruitV({ "apples", "oranges" });▼
▲ std::vector<std::string> fruitV({ "apples", "oranges" });
std::list<std::string> fruitL({ "apples", "oranges" });
std::deque<std::string> fruitD({ "apples", "oranges" });
std::cout << fruitV.size() << fruitL.size() << fruitD.size() << std::endl;</lang>
Of these, vector is probably the most widely used.
|