我正在使用数组。因此,要查找数组的大小,我需要使用 std::size(),它在一个程序中运行良好,但在另一个程序中无法运行。
\n有效的程序:
\n#include <iostream>\n#include <iterator>\n#include <algorithm>\n\nint main()\n{\n int array[]{ 30, 50, 20, 10, 40};\n std::sort(std::begin(array), std::end(array));\n\n for (int i{0}; i < static_cast<int>(std::size(array)); ++i)\n {\n std::cout << array[i] << '\\t';\n }\n\n std::cout << '\\n';\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n10 20 30 40 50\nRun Code Online (Sandbox Code Playgroud)\n无法运行的程序:
\n#include <iostream>\n#include <iterator> // for std::size() but not working yet\n\nvoid splitInteger(int integer, int components[])\n{ \n int count{0};\n\n do\n {\n int element{ integer % 10 };\n components[count] = element;\n\n integer = integer / 10;\n ++count;\n }\n while(integer != 0);\n\n}\nint main()\n{\n int num{}; \n std::cin >> num;\n\n int components[]{}; \n splitInteger(num, components);\n\n std::cout << std::size(components) << '\\n';\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n它给出如下编译错误:
\ntempCodeRunnerFile.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\ntempCodeRunnerFile.cpp:38:38: error: no matching function for call to \xe2\x80\x98size(int [0])\xe2\x80\x99\n 38 | std::cout << std::size(components) << '\\n';\n | ^\nIn file included from /usr/include/c++/9/string:54,\n from /usr/include/c++/9/bits/locale_classes.h:40,\n from /usr/include/c++/9/bits/ios_base.h:41,\n from /usr/include/c++/9/ios:42,\n from /usr/include/c++/9/ostream:38,\n from /usr/include/c++/9/iostream:39,\n from tempCodeRunnerFile.cpp:1:\n/usr/include/c++/9/bits/range_access.h:242:5: note: candidate: \xe2\x80\x98template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)\xe2\x80\x99\n 242 | size(const _Container& __cont) noexcept(noexcept(__cont.size()))\n | ^~~~\n/usr/include/c++/9/bits/range_access.h:242:5: note: template argument deduction/substitution failed:\n/usr/include/c++/9/bits/range_access.h: In substitution of \xe2\x80\x98template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&) [with _Container = int [0]]\xe2\x80\x99:\ntempCodeRunnerFile.cpp:38:38: required from here\n/usr/include/c++/9/bits/range_access.h:243:24: error: request for member \xe2\x80\x98size\xe2\x80\x99 in \xe2\x80\x98__cont\xe2\x80\x99, which is of non-class type \xe2\x80\x98const int [0]\xe2\x80\x99\n 243 | -> decltype(__cont.size())\n | ~~~~~~~^~~~\n/usr/include/c++/9/bits/range_access.h:252:5: note: candidate: \xe2\x80\x98template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])\xe2\x80\x99\n 252 | size(const _Tp (&/*__array*/)[_Nm]) noexcept\n | ^~~~\n/usr/include/c++/9/bits/range_access.h:252:5: note: template argument deduction/substitution failed:\nRun Code Online (Sandbox Code Playgroud)\n我不知道为什么会发生这种情况。任何帮助,将不胜感激。
\n另外,我将 g++ 与这些标志一起使用:
\ng++ -Wall -Weffc++ -Wextra -Wsign-conversion -Werror -std=c++17\nRun Code Online (Sandbox Code Playgroud)\n
首先,这个定义:
int components[]{};
Run Code Online (Sandbox Code Playgroud)
是完全错误的。如果您确实需要旧的 c 样式数组,则需要使用 new 定义它们的大小:
int *components = new int[size];
Run Code Online (Sandbox Code Playgroud)
C 风格的数组必须在编译期间知道它们的大小,除非您使用关键字 new。如果您想使用未定义大小(可以修改)的集合,请阅读有关std::vector. 当你想知道它的大小时,使用这个调用:
int size_of_v = std::size(v);
Run Code Online (Sandbox Code Playgroud)
第一个代码之所以有效只是因为一个事实 - 编译器可以知道数组有多大。
| 归档时间: |
|
| 查看次数: |
1508 次 |
| 最近记录: |