如何获取数组中已用空间的大小?(How to get the size of the used space in an array? (NOT sizeof); c++)

编程入门 行业动态 更新时间:2024-10-18 16:47:47
如何获取数组中已用空间的大小?(How to get the size of the used space in an array? (NOT sizeof); c++) #include<iostream> using namespace std; int main() { char arr[200]; while(1) { cin >> arr; int i = sizeof(arr); cout << "The arr input is "<< arr << " and the size of the array is "<< i << endl; } return 0; }

对于34的输入,此代码输出:arr输入为34,数组大小为200


而我希望它获得数组使用空间的大小。 所以对于我希望它输出的最后一个输入:arr输入是34,数组的大小是2


有人能告诉我怎么样?

#include<iostream> using namespace std; int main() { char arr[200]; while(1) { cin >> arr; int i = sizeof(arr); cout << "The arr input is "<< arr << " and the size of the array is "<< i << endl; } return 0; }

For the input of 34, This code outputs :The arr input is 34 and the size of the array is 200


while I want it to get the size of the used space of the array . So for The last input i want it to output :The arr input is 34 and the size of the array is 2


Can someone tell me how?

最满意答案

也许你想在这里使用strlen(arr) 。 它必须以null结尾,否则cout << arr将无效。

你需要#include <cstring>

Maybe you want strlen(arr) here. It must be null terminated, otherwise the cout << arr would not have worked.

You would need to #include <cstring>

更多推荐

本文发布于:2023-08-04 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   已用   大小   空间   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!