C++ 整数数组长度C++;
我必须在我的程序中使用动态长度int数组,并希望能够在代码中的不同点获得其中的对象数。我不是很熟悉C++,但这里是我所拥有的。为什么它没有给我正确的长度?谢谢C++ 整数数组长度C++;,c++,arrays,C++,Arrays,我必须在我的程序中使用动态长度int数组,并希望能够在代码中的不同点获得其中的对象数。我不是很熟悉C++,但这里是我所拥有的。为什么它没有给我正确的长度?谢谢 <#include <iostream> Using Namespace std; int length(int*); void main() { int temp[0]; temp[0] = 7; temp [1] = 10; temp[2] = '\0'; cout << len
<#include <iostream>
Using Namespace std;
int length(int*);
void main()
{
int temp[0];
temp[0] = 7;
temp [1] = 10;
temp[2] = '\0';
cout << length(temp) << endl;
}
int length(int* temp)
{
int i = 0;
int count = 0;
while (*temp + i != '\0')
{
count++;
i++;
}
return count;
}
您可以尝试:
while (*(temp + i) != '\0')
您当前的解决方案是计算<代码> TEMP(0)+I <代码>(等于<代码> 7 + i <代码> >,这显然不是您想要的。C++中的
< P>不是动态的。临时数组的长度为零,尝试写入超出其长度的成员是未定义的行为。它很可能无法工作,因为它将在堆栈的某个部分上写入
创建一个固定大小的数组,该数组有足够的空间来放置所有需要的内容,或者使用一个动态数据结构std::vector
#include <iostream>
#include <vector>
using namespace std;
int length(int*);
int main () // error: ‘::main’ must return ‘int’
{
int temp[3];
temp[0] = 7;
temp[1] = 10;
// don't use char constants for int values without reason
temp[2] = 0;
cout << length(temp) << endl;
vector<int> vec_temp;
vec_temp.push_back(7);
vec_temp.push_back(10);
cout << vec_temp.size() << endl;
}
int length(int* temp)
{
int i = 0;
int count = 0;
while (*(temp + i) != 0) // *temp + i == (*temp) + i
{
count++;
i++; // don't really need both i and count
}
return count;
}
或使用计数索引温度:
int length(int* temp)
{
int count = 0;
while (temp[count] != 0)
++count;
return count;
}
要获得数组中的动态行为,请使用std::vector
,或者使用带有手动内存分配的int*
(new
和delete
)的老式c样式[*]
[*]字符实现(在字符数组AS中讨论)使用了<代码> Malc , ReLoCU ,和自由> /COD>,但是在C++代码中应该避免这些。
< P>这是一个坏主意,原因有两个,但首先这里有一些问题:
int temp[0];
这是一个包含0项的数组,我甚至认为堆栈元素不允许使用该数组。在声明这样的数组时,必须指定将使用的最大值数:例如int temp[10]代码>
这非常重要-如果您确实指定了一个更少的数字(例如[10]并且您使用[11]),那么您将导致内存覆盖,充其量它会崩溃,最坏的情况下会导致奇怪的bug,这是一场噩梦
下一个问题是这一行:
while (*temp + i != '\0')
此行的作用是获取存储在“temp”指定的地址中的值并添加i。您需要的是在temp指定的地址的第n个元素处获取值,如下所示:
while (*(temp + i) != '\0')
这就是问题所在,但是你应该花五分钟来考虑一个更好的方法
我提到这是个坏主意的原因是:
- 您需要在需要整个数组的长度时对其进行迭代
- 永远不能在数组中存储终止元素(在本例中为0)
相反,我建议您维护一个单独的值来存储数组中的元素数。一种非常常见的方法是创建一个封装这个概念的类(元素块和当前大小)
C++标准库附带一个模板类,名为“vector”,可以用于此目的。它与数组不完全相同(必须在索引之前先添加项),但非常相似。它还支持复制/调整大小,这也很方便
下面是使用std::vector编写的程序。我添加了一些内容来打印值,而不是“length”函数:
#include <vector>
#include <iostream>
void print(std::vector<int> const& vec)
{
using namespace std;
for (size_t i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
}
int main()
{
std::vector<int> temp;
temp.push_back(7);
temp.push_back(10);
print(temp);
return 0;
}
#包括
#包括
无效打印(标准::矢量常量和矢量)
{
使用名称空间std;
对于(size_t i=0;i CUT< P>不仅C++的数组不是动态的,Pete指出,但是只有字符串(char)以“0”结尾。(这并不是说你不能对其他类型使用类似的约定,但是它是非常不寻常的,并且有充分的理由:特别是,依赖于终结符符号需要你循环通过数组来找到它的大小)
在像您这样的情况下,最好使用标准库
#include <vector>
#include <iostream>
int main()
{
std::vector<int> v;
v.push_back(7);
v.push_back(10);
std::cout << v.size() << std::endl;
return 0;
}
#包括
#包括
int main()
{
std::向量v;
v、 推回(7);
v、 推回(10);
std::cout因为您只为零元素数组分配空间。
下面几行
temp [1] = 10;
temp[2] = '\0';
不要分配更多内存或调整数组大小。您只是在数组外写入数据,破坏应用程序状态的其他部分。不要这样做。;)
如果需要可调整大小的数组,可以使用std::vector(并使用push_back成员函数插入新值)
向量还有size()成员函数,它告诉您当前大小
如果要使用基元数组,您必须自己跟踪大小。(并且,当需要调整数组大小时,请将所有元素从旧数组复制到新的较大数组)获取固定长度数组大小的最常见方法如下:
int temp[256];
int len = sizeof (temp) / sizeof (temp[0]);
// len == 256 * 4 / 4 == 256 on many platforms.
这不适用于动态数组,因为它们实际上是指针
int* temp = new int[256];
int len = sizeof (temp) / sizeof (temp[0]);
// len == 4 / 4 == 1 on many platforms.
对于动态长度数组,如果您关心大小,最好在分配数组时将其存储在某个位置
正如许多人指出的那样,循环的问题在于这里存在运算符优先级问题:
*temp + i
应该是:
*(temp + i)
但上面也指出了一个更大的问题,那就是你似乎不理解指针和固定长度数组的区别,并且正在注销数组的结尾。如果你想正确使用数组,你必须分配足够的内存来存储值。一旦你指定了数组的长度,你就不能更改它。要知道数组的大小,你应该将其存储在变量中,例如:
int n;
cin>>n;
int array = new int[n];
int array_length=n;
如果要更改数组的长度,最好的方法是使用std容器,例如std::vector。如果不想使用std::vector,请尝试以下操作:
#include <iostream>
using namespace std;
int main () {
int vet[] = {1,2,3,4,5,6};
cout << (sizeof (vet) / sizeof *(vet)) << endl;
return 0;
}
#包括
使用名称空间std;
int main(){
int-vet[]={1,2,3,4,5,6};
cout试试这个:
int length(int* temp)
{
int count = 0;
while (*temp != 0 && *temp != -858993460)
{
++count;
++temp;
}
return count;
}
这是你问题的答案
int myarr [] = {1, 2, 3, 4, 5};
int length = sizeof(myarr) / sizeof(myarr[0]);
cout << length;
int myarr[]={1,2,3,4,5};
int length=sizeof(myarr)/sizeof(myarr[0]);
为什么他想使用MalCal& ReLoC(而可能是免费的)而不是NeX& Delphi?@尼尔:好点。我在想C。但是如果他做C++风格,那么很少有一种选择在STD上选择int的方法::vector…为什么你不能决定使用“0”作为其他数据类型的哨兵?(即使它不是常用的)(即使将“\0”分配给in
int myarr [] = {1, 2, 3, 4, 5};
int length = sizeof(myarr) / sizeof(myarr[0]);
cout << length;