用memset()和memcpy()函数

问题描述 投票:-1回答:1
// CPP Program to find all the common characters 
// in n strings 
#include <bits/stdc++.h> 
using namespace std; 

const int MAX_CHAR = 26; 

void commonCharacters(string str[], int n) 
{ 
  // primary array for common characters 
  // we assume all characters are seen before. 
  bool prim[MAX_CHAR] = {true}; 
  //memset(prim, true, sizeof(prim)); 

  // for each string 
  for (int i = 0; i < n; i++) { 

    // secondary array for common characters 
    // Initially marked false 
    bool sec[MAX_CHAR] = { false }; 

    // for every character of ith string 
    for (int j = 0; str[i][j]; j++) { 

      // if character is present in all 
      // strings before, mark it. 
      if (prim[str[i][j] - 'a']) 
        sec[str[i][j] - 'a'] = true; 
    } 

    // copy whole secondary array into primary 
    //memcpy(prim, sec, MAX_CHAR); 

    for(int k=0;k<n;k++)
      prim[k] = sec[k];
  } 


  // displaying common characters 
  for (int i = 0; i < 26; i++) 
    if (prim[i]) 
      printf("%c ", i + 'a'); 
} 

// Driver's Code 
int main() 
{ 
  string str[] = { "geeksforgeeks", 
                   "gemkstones", 
                   "acknowledges", 
                   "aguelikes" }; 
  int n = sizeof(str)/sizeof(str[0]); 
  commonCharacters(str, n); 
  return 0; 
}

我们使用尺寸26的两个哈希阵列(对于A-Z,其中0是一个,且z是25)。方法很简单,如果我们看到一个字符之前,我们将纪念它,如果我们没有那么忽略字符,因为它不是一个常见的一种。为什么会发生这种代码没有给出所需的输出?而如果我用memset(prim,true,sizeof(prim))代替bool prim[MAX_CHAR] = {true};的初始化和memcpy(prim,sec,MAX_CHAR)代替for(int k=0;k<n;k++) prim[k] = sec[k];的用于复制布尔数组秒[]在一本正经[]它工作得很好。

c++ memcpy memset
1个回答
4
投票

警告

bool prim[MAX_CHAR] = {true}; 

不等同于memset(prim, true, sizeof(prim));

MAX_CHAR是26,你只给1个值与{true},所以prim[0]初始化与真实,所有的25个条目0(假)初始化。真不使用到该阵列的端部。

bool prim[MAX_CHAR] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }

初始化26项(如果我算好)

当然memcpy(prim,sec,MAX_CHAR)for(int k=0;k<n;k++) prim[k] = sec[k];是不等效的,因为n是串(4)的数量和不重视MAX_CHAR(26)

执行与您的代码:

pi@raspberrypi:/tmp $ ./a.out
pi@raspberrypi:/tmp $

执行与memset的或{} 26属实,并通过for(int k=0;k<n;k++)更换for(int k=0; k<MAX_CHAR; k++)

pi@raspberrypi:/tmp $ ./a.out
e g k s pi@raspberrypi:/tmp $ 

从弗朗索瓦·安德里厄的提案(在下面一个移除的话),以除去该问题有关的整洁的初始化:扭转拘谨的布尔值,所以

void commonCharacters(string str[], int n) 
{ 
  // primary array for common characters 
  // we assume all characters are seen before. 
  // (false means seen before, reverse logic)
  bool prim[MAX_CHAR] = {false}; 

  // for each string 
  for (int i = 0; i < n; i++) { 

    // secondary array for common characters 
    // Initially marked false (standard logic)
    bool sec[MAX_CHAR] = { false }; 

    // for every character of ith string 
    for (int j = 0; str[i][j]; j++) { 

      // if character is present in all 
      // strings before, mark it. 
      if (!prim[str[i][j] - 'a']) 
        sec[str[i][j] - 'a'] = true; 
    } 

    // copy negation of whole secondary array into primary         
    for(int k=0; k<MAX_CHAR; k++)
      prim[k] = !sec[k];
  } 

  // displaying common characters 
  for (int i = 0; i < 26; i++) 
    if (!prim[i]) 
      printf("%c ", i + 'a'); 
} 

执行:

pi@raspberrypi:/tmp $ ./a.out
e g k s pi@raspberrypi:/tmp $ 

但对于整洁的反向逻辑和秒的标准逻辑可以是混乱

© www.soinside.com 2019 - 2024. All rights reserved.