如果你在 C 中有一个数组,如何确定其中有多少内容已被填充?
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
如果你在 C 中有一个数组,如何确定其中有多少内容已被填充?
在C阵列中,任何成分都是物体。 在 Java,你没有提及首先必须指定用于点物体。 在 C,C区的任何行为都像原始类型一样。
如果您在C语言中有一个指针数组,您可以将其视为与Java中的事物类似。您可以使用空指针来指示“未填充指向对象”:
// creates an array of 10 pointers, and initializes all of
// them to null pointers. If you leave off "{ 0 }", you
// have to manually initialize them!
struct foo *array[10] = { 0 };
然后您可以简单测试一下。
if(array[i] == 0) {
printf("Position %d does not point to an object!
", i);
}
你需要自行跟踪这个。没有所谓的“完整”(或其他任何介于其间的东西)的概念:你必须自己定义。
当然,如果元素在数组中是连续的,您可以使用NULL
元素来表示数组的“结束”,从而同时定义“完整”状态。
它都已填满了,因此答案就是您的数组大小。数组是一个连续的内存段,因此默认情况下会用以前位于该内存位置的任何内容来填充它。
但是,你可能想知道,其中有多少人填写了你所关注的数据,而不是随机的数据。 在此情况下,除非你继续跟踪此事,否则就无法知道。
我同意其他答案,但我可以建议您一种使您的工作更轻松的方法。您可以像管理对象一样管理数组,并控制添加和删除数据。如果您实现两个函数,一个用于添加元素,一个用于删除元素,并采用适当的逻辑来管理碎片化和多线程,您可以通过读取仅由添加和删除函数编写的计数器来跟踪数组中的元素数量。因此,您不必每次需要计算元素数量时执行循环。
从C语言的角度来看,没有“填充”的概念。一旦数组被定义,就会分配内存。对于像array1这样的数组(见下面的示例),元素将初始化为0。但是,对于array2这样的数组,元素可能具有随机值。
So, the notion of "filled" has to be supplied by the program. One possible to "in-band" way is to: (a) Choose one specific value of the element type (e.g. 0xFFFFFFFF) and use it to detect fill/empty property of each array element (However, realize that this approach takes away one otherwise valid value from the element set.), and (b) "initialize" all the elements of the array to that disallowed value at suitable position in the program scope. (c) To find array fill level, count the number of valid elements.
$ cat t2.c
#include <stdio.h>
#define N 10
typedef unsigned long int T;
static const T EmptyElementValue = 0xFFFFFFFF;
// Choose any suitable value above. However, the chosen value
// would not be counted as an "empty" element in the array.
static T array1[ N ];
void
printArray( T a[], size_t length )
{
size_t i;
for( i = 0; i < length; ++i )
{
printf( "%lu, ", a[ i ] );
}
printf( "
" );
}
size_t
numFilledElements( T a[], size_t length )
{
size_t fillCount = 0;
size_t i;
for( i = 0; i < length; ++i )
{
if( a[ i ] != EmptyElementValue )
{
fillCount += 1;
}
}
return fillCount;
}
int main()
{
T array2[ N ];
size_t i;
printArray( array1, N );
printArray( array2, N );
//------------------------------------------//
// Make array2 empty
for( i = 0; i < N; ++i )
{
array2[ i ] = EmptyElementValue;
}
// Use some elements in array2
array2[ 2 ] = 20;
array2[ 3 ] = 30;
array2[ 7 ] = 70;
array2[ 8 ] = 80;
printf( "Number of elements "filled" in array2 = %u
",
numFilledElements( array2, N ));
// Stop using some elements in array2
array2[ 3 ] = EmptyElementValue;
printf( "Number of elements "filled" in array2 = %u
",
numFilledElements( array2, N ) );
return 0;
}
$ gcc -Wall t2.c -o t2
$ ./t2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 60225, 2280452, 1627469039, 1628881817, 2281060, 2280680, 1628304199, 1628881818, 47,
Number of elements "filled" in array2 = 4
Number of elements "filled" in array2 = 3
$
从数组的大小中减去空元素的数量。;-)
很抱歉,除非你自己去追踪,否则无法知道数组元素是否已被修改。
在 C 语言中,没有内置的方式来知道有多少个元素被填充了您关心的数据。您需要自己构建。正如先前所说,如果您可以有一个不代表任何东西的值(例如 0),您可以:
另一方面,如果您需要表示数据的范围,您将需要一个标志数组来跟踪已设置和未设置的元素:
For example, if you have an array of 32 elements or less, you only need an unsigned integer to keep track of your array: 1100010 ...
价值观:
1 -> 设置
2 -> 设置
3 -> 没有集合
4 -> 未设置
5 -> 未设置
6-> set
等等。
因此,每当您填充元素时,您都会调用设置正确位的函数,而当您“取消填充”数据时,您会取消相应位的设置。
一旦完成, 你们都需要做的是简单地说一个。 超过国旗阵列。
你可以使用一个while(yourArray != NULL)
循环,然后在循环中仅增加一个整数值,这样就能告诉你了。
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
最好、最小、最快、开放的来源、C/C++ 3d 提供方(在3ds max模型的支持下),而不是通用公平市价,
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...
Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...