如何将二维数组分配给另一个?

问题描述:

组件如何将二维数组分配给另一个?

我有一个字符串,例如

char block[4][256] = "";

和我有句

char sentence[256] = "Bob walked his dog";

我也有可变迭代器

int pos = 0;

我试图实现

我试图每个字阵列sentence在分配到在序列中的2-d块阵列block

例如,

比方说,我有这样的代码(我自己写了 - 因为我的计划没有工作),我怎么看它

for (int x=0; x < ((int)strlen(sentence)); x++) 
{ 
    if (sentence[x] == ' ') // not using strcmp at the moment to be more clear 
    { 
     ++pos; // move to the next word after space 
     for (int y=0; y < pos; y++) // scan through the word 
     { 
      for (int z=0; z < x; z++) // add the word to the array 
      { 
       block[y][z] = sentence[z]; // assign the block (!!confusion here!!) 
      } 
     } 
    } 
} 

我通过解决这个问题的方式来看待它,我需要先扫描句子,直到遇到空格字符为止。遇到这种情况后,我必须重新扫描句子,并开始将所有字符添加到该空白位置''到我的块数组的第一部分block[y][z],z是上述for语句中的迭代器,y为遇到的每个空间的位置+1。我相信我的主要问题在于理解如何分配二维数组。如果有人看到更好的方法来解决这个问题,我很乐意听到它,谢谢!

输出欲

,我扫描例如阵列中的每个字打印希望每个x的block[x][256]内容输出之后。如果我有这样的事情。

for (int a=0; a < 4; a++) 
{ 
    for (int b=0; b < strlen(block[a][]); b++) 
    { 
     printf("%s\n", block[a][b]); 
    } 
} 

我所要的输出是:

block[0][]: Bob 
block[1][]: walked 
block[2][]: his 
block[3][]: dog 

灿我怎么能解决这个问题任何人的帮助?谢谢!

+0

什么是CMD这里? – Adarsh 2014-09-03 03:16:12

+0

在这方面 - 我很抱歉,我删除了这段代码,因为它是我项目早期的一部分。 – user3251225 2014-09-03 03:18:54

+0

如果它是C++,为什么不使用'std :: string','std :: vector <:string>',以及'cout'等?此外,可以使用'std :: istringstream'将输入分隔为单词。换句话说,你的问题可以简化为几行“C++”代码。 – PaulMcKenzie 2014-09-03 03:19:50

我认为这是你要去的。

int word_start = 0, word_end = 0, current_word = 0; 

for (int x = 0; x < strlen(sentence) + 1; x++) 
{ 
    if (sentence[x] == ' ' || sentence[x] == '\0') 
    { 
     word_end = x; 
     int y, z; 
     for (y = 0, z = word_start; z < word_end; y++, z++) 
     { 
      block[current_word][y] = sentence[z]; 
     } 
     word_start = x + 1; 
     current_word++; 
    } 
} 

这是我用来测试它的程序,如果它不适合你,你想看看我如何解释你的问题。

#include <stdio.h> 
#include <string.h> 

int main (const int argc, char * const argv[]) 
{ 
    char block[4][256] = {0}; 
    char sentence[256] = "Bob walked his dog"; 

    int word_start = 0, word_end = 0, current_word = 0; 

    for (int x = 0; x < strlen(sentence) + 1; x++) 
    { 
     if (sentence[x] == ' ' || sentence[x] == '\0') 
     { 
      word_end = x; 
      int y, z; 
      for (y = 0, z = word_start; z < word_end; y++, z++) 
      { 
       block[current_word][y] = sentence[z]; 
      } 
      word_start = x + 1; 
      current_word++; 
     } 
    } 

    for (int x = 0; x < 4; x++) 
    { 
     printf("%s\n", block[x]); 
    } 
} 
+1

为什么不使用'memcpy'而不是循环? – PaulMcKenzie 2014-09-03 03:27:18

+4

我会说'memcpy'是OP可以利用的优化,一旦他理解了所涉及的原则。 – 2014-09-03 03:29:12

+0

我会用输出缓冲区和'strlcpy'的动态分配来显示正确的解决方案,但这更符合问题的要点和风格。 – OregonTrail 2014-09-03 03:29:45

虽然存入块,

for (int i=0 ; i < 4 ; i++) 
{ 
    for (int j=0 ; j < 256 ; j++) 
    { 
     if (sentence[j] == ' ') 
     { 
      block[i][j] = '\0'; 
      break; 
     } 
     block[i][j]=sentence[j]; 
    } 
} 

打印时,

for (int i=0 ; i<4 ; i++) 
{ 
    printf ("block[%d][]: %s\n", i, block[i]); 
} 
+0

感谢您的回答。我很欣赏你写这篇文章的时间,尽管我正在寻找一个更普遍的答案,比如俄勒冈。它通过编辑它不会限制我一个4字的句子。为你的努力+1。谢谢!更多的C++ - 就像将'std :: copy(s.begin(),s.end(),&block [curWord] [0]);' – user3251225 2014-09-03 06:19:06

接着块工程..你需要,如果有在句子中采取多个空格的照顾。

unsigned int x = 0; 
size_t length = strlen(sentence); 
unsigned int word=0 ; 
while (x < length) 
{ 
     // copy this word to block 
     unsigned charactercount = 0; 
     char character = sentence[x++] ; 
     while (x<=length && ' ' != character) 
     { 
      block[word][charactercount++] = character; 
      character = sentence[x++] ; 
     } 
     if (' ' == character || 0 == character) 
     { 
      block[word][charactercount++] = 0; 
      word++; 
     } 
} 

首先,就像一个音符 - 你的问题会更大,如果你需要存储上述4个字什么,尤其是如果你正在写的东西在C而不是使用C++和各种容器在C++中可用。

因为答案(到目前为止)有 'C' 的解决方案,这里是一个C++的解决方案使用std::istringstreamstd::string

#include <sstream> 
#include <string> 
#include <cstdlib> 

int main() 
{ 
    char block[4][256] = { 0 }; 
    char sentence[] = "Bob walked his dog"; 

    std::istringstream sstrm(sentence); 
    int curWord = 0; 
    std::string s; 
    while (sstrm >> s) 
    { 
     memcpy(&block[curWord][0], s.c_str(), s.size()); 
     ++curWord; 
    } 
} 
+0

当然,如果这用于“真正的代码”,那么你应该检查'curWord 2014-09-03 06:00:01

+0

尽管我正在寻找一个更'C'的解决方案,我很欣赏独特的C++透视它。我相信它可能会帮助有人在未来寻找类似的答案。谢谢! +1 – user3251225 2014-09-03 06:16:57