cha*_*rre 0 c arrays comparison
假设我有一个数组
bool string[N]={false};
Run Code Online (Sandbox Code Playgroud)
在执行某些操作之后,数组字符串的所有元素都变为true.我想在if语句中检查这种情况,如下所示: -
伪代码 -
if(all the elements of string are same or equal)
then do this
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?我不应该使用像这样的计数器
for(int i=0;i<N;i++) //or something else like this
Run Code Online (Sandbox Code Playgroud)
PP只需稍微改变他的代码,他所暗示的答案是: -
if (memcmp (&string [0], &string [1], sizeof string [0] * (N - 1)) == 0)
{
/* all elements the same */
}
Run Code Online (Sandbox Code Playgroud)
N-1停止超越缓冲区的末尾.
memcmp将字符串[0]与字符串[1]进行比较,然后将字符串[1]与字符串[2]进行比较,然后将字符串[2]与字符串[3]进行比较,依此类推,直至字符串[n-2]和字符串[n- 1].
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |