动态创建数组 [英] dynamically created arrays

查看:169
本文介绍了动态创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务由两部分组成。首先我必须创建100个元素的globbal char数组,并使用cin插入一些文本。然后计算字符的数量,并创建具有输入文本长度的专用数组。我正在考虑以下解决方案:

  char [100] inputData; 

int main()
{

cin>> inputData>> ENDL;

int length = 0; (int i = 0; i <100; i ++)
{
while(inputData [i] ==\0)
{
+ +计数;
}
}
char c = new char [count];

我觉得好吗?



第二任务的一部分是在第一个程序中引入动态创建的数组,指向所有插入的单词。添加一个新单词应该打印所有以前的单词,如果下一个单词没有空格,则inputData数组的大小应该增加两倍。说实话,这对我来说有点太多了。我怎么能专门创建指针的指针?而如何在不放弃内容的情况下增加全局数组的大小?有一些临时数组?

解决方案

无论您的其他问题如何,您似乎对 while 循环。我们来看看这个代码。

  for(int i = 0; i< 100; i ++){
while inputData [i] ==\0){
++ count;
}
}

首先,\ 0不是NUL字符。它是一个指向仅包含终止NUL字节的字符串的指针。您需要'\0'。假设这个变化,还有问题。让我们努力解决将要发生的事情:




  • while 循环如何工作?只要条件为真,它就会重复执行机构。

  • while 循环是否完成?当条件终于通过执行身体而变为虚假。

  • 你的循环是什么状况? inputData [i] =='\0',更正后。

  • 身体是什么? ++ count

  • 可以 ++ count 条件?不,因为它不会改变

  • 所以,如果 inputData [i] 不是NUL字节, while 循环永远不会执行。

  • 但是,如果 inputData [i] 是NUL字节, while 循环永远执行。

  • 假设你已经阅读一个适当的字符串到 inputData ,然后在某些点 inputData [i] 将是NUL,你会有一个无限循环



要计算标准C字符串的长度,只需执行此操作

  count = strlen(inputData); 

如果由于某种原因你真的必须写一个循环,那么以下作品:

  int len = 0,
while(inputData [len]!='\0') {
len ++;
}

循环后, len 保存字符串的长度。


My task consists of two parts. First I have to create globbal char array of 100 elements, and insert some text to it using cin. Afterwards calculate amount of chars, and create dedicated array with the length of the inputted text. I was thinking about following solution :

char[100]inputData;

int main()
{  

   cin >> inputData >> endl;

   int length=0;
   for(int i=0; i<100; i++)
   {
           while(inputData[i] == "\0")
           {
               ++count;
           }
   }
char c = new char[count];

Am I thinking good ?

Second part of the task is to introduce in the first program dynamically created array of pointers to all inserted words. Adding a new word should print all the previous words and if there is no space for next words, size of the inputData array should be increased twice. And to be honest this is a bit too much for me. How I can create pointers to words specifically ? And how can I increase the size of global array without loosing its content ? With some temporary array ?

解决方案

Regardless of the rest of your question, you appear to have some incorrect ideas about while loops. Let's look at this code.

for(int i=0; i<100; i++) {
    while(inputData[i] == "\0") {
        ++count;
    }
}

First, "\0" is not the NUL character. It is a pointer to a string containing only the terminating NUL byte. You want '\0' instead. Assuming this change, there are still problems. Let's work through what will happen:

  • How does a while loop work? It executes the body repeatedly, as long as the condition is true.
  • When does a while loop finish? When the condition is finally made false by executing the body.
  • What's the condition of your loop? inputData[i] == '\0', after correction.
  • What's the body? ++count.
  • Can ++count ever change the value of the condition? No, because it doesn't change i.
  • So, if inputData[i] is not the NUL byte, the while loop never executes.
  • But, if inputData[i] is the NUL byte, the while loop executes forever.
  • Assuming you've read a proper string into inputData, then at some point inputData[i] will be NUL, and you'll have an infinite loop.

To count the length of a standard C string, just do this

count = strlen(inputData);

If for some reason you really have to write a loop, then the following works:

int len = 0, 
while (inputData[len] != '\0') {
    len++;
}

After the loop, len holds the length of the string.

这篇关于动态创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆