【问题标题】:I continue to get garbage values in an array that have been modified from another array, in a function我继续在一个函数中从另一个数组修改的数组中获取垃圾值
【发布时间】:2011-04-09 13:20:32
【问题描述】:

我对编程有点陌生,所以我的问题的答案并不明显,尽管我尝试了多种方式来完成这项任务。

问题是我试图获取一个单词数组,从数组中删除任何标点符号,然后将新单词放入一个单独的数组中。我试图这样做,但是当我输出新数组时,我不断收到垃圾值。

代码如下:

norm(sepwords1,sepwords2,numwords);      <- where I called it in main

void norm(string words[], string wordz[],int count)       
{

        int i;
        int x;

        string newstring="";
        char current;


    for(i=0; i<count; i++)
        {
        for(x=0; x<words[i].length();x++)
        {        
          current= words[i].at(x);
                if(ispunct(current)==0)
                {
                newstring += current;
                }

        }        
                wordz[i]= newstring;
        }

}

完整的主要功能是:

int main (int argc, char* argv[])
{



int count = argc;
int i;
string filename[count];
ifstream infile;
string fromfile[1000];
int numdata;
int pass;
char current;
int sum;
string masterstring="";
int x;
string sepwords[2000];
int sum1;
string temp="";
int start;
int fin;
string newstring="";
string newfile[1000];
int place;
int numwords;
string sepwords1[2000];
string newmaster="";
int j=0;
string currentz;
string highmark;
int index[2000];
string sepwords2[2000];
int counta=0;

for(i=0; i < count-1; i++) 
{
filename[i] = argv[i+1];
}

for( i=0; i < count-1; i++)
    {
    infile.open(filename[i].c_str());

    numdata=0;  

    while(!infile.eof())
    {

    getline(infile, fromfile[numdata], '\n');
    numdata++;

    }





    for(i=0; i<numdata; i++)
    {
    cout<<fromfile[i]<<endl;
    masterstring += fromfile[i] + " ";                                      //NUMBER ONE
    }



    numwords = split(masterstring, sepwords);
    cout<<numwords<<endl;                                                       //NUMBER TWO


    }

    for(i=0;i<numwords;i++)
    {
        newstring = toupper(sepwords[i].at(0));         
        newstring += sepwords[i].substr(1);
        sepwords1[i] = newstring;
        newstring="";
    }

    for(i=0;i<numwords;i++)
    {


    newmaster += sepwords1[i] + " ";
       j++;
          if(j > 10)
          {
           newmaster+= '\n';
           j=0;
          }

    }
    cout<<newmaster<<endl;                                              //NUMBER THREE



    norm(sepwords1,sepwords2,numwords);

        for(i=0;i<numwords;i++)
    {
    cout<<sepwords2<<endl;
    }

return 0;
}

【问题讨论】:

  • 如何声明传递给norm 的原始数组?你有什么理由使用数组而不是std::vector,因为我认为这个字符串列表可能是可变的?
  • 我想你想 newstring="" 为外循环的每次迭代。
  • 我没有使用矢量只是因为我还没有知道如何实现一个:P
  • @Sam, &lt;vector&gt; 是 C++ 标准库的一部分。如果这是家庭作业,那么我可以理解……您能否也发布完整的main 功能?
  • 这一行是错误的“cout

标签: c++ arrays function


【解决方案1】:

您的代码应该可以工作,但主函数中可能存在问题,例如您的数组以及您必须使用两个的事实,所以我看到这种行为的一个原因可能是您的数组大小不匹配彼此,并且存储原始字符串的那个大于您要复制到的那个。

#include <string>
#include <iostream>

int main() {
   const int SIZE = 5;
   string oldArray[SIZE] = {"He,llo", "Wor,ld", "H,ow", "Ar,e.", "Y,O,U"};
   string newArray[SIZE];

   for (int i = 0; i < 5; ++i) {
      // Moved this into the loop for ease, otherwise your
      // original code would have kept appending to this
      // newString variable unless you cleared it later
      std::string newString = "";
      for (int x = 0; x < oldArray[i].length(); ++x) {
            char current = oldArray[i].at(x);
            if (ispunct(current) == 0)
            {
               newString += current;
            }
      }
      newArray[i] = newString;
   }

   for (int i = 0; i < 5; ++i) {
      std::cout << newArray[i] << '\n';
   }
}

这主要是您的代码,并进行了一些调整以解决保持newString 存在但以后不清除它的串联问题。

您可以通过使用 std &lt;algorithm&gt; 的东西和使用 &lt;vector&gt; 来更简洁地解决这个问题,它将为您处理增长和调整大小。

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

int main() {
    std::vector<std::string> stringsToCopy;
    stringsToCopy.push_back("Hel,lo,");
    stringsToCopy.push_back("th,ere.");

    // Make a copy of the other vector, since it seems like you want to keep
    // the original data. This will copy all the elements from the stringsToCopy
    // vector.
    std::vector<std::string> newStrings = stringsToCopy;

    // simplicity, but you could use an iterator as well, which would be
    // more verbose
    for (int i = 0; i < newStrings.size(); ++i) {
        // get a reference to the current string in the
        // vector for convenience, so we can use a shorter
        // name for it
        std::string& s = newStrings[i];

        // because remove_if doesn't actually delete things from a 
        // container, we should also call the string's erase method
        s.erase(std::remove_if(s.begin(), s.end(), ispunct), s.end());
    }


    for (int i = 0; i < newStrings.size(); ++i) {
        std::cout << newStrings[i] << '\n';
    }
}

【讨论】:

  • 感谢您的回复。我将尝试学习如何更熟练地使用向量,因为我目前还没有真正的线索。我想我会补充一点,两个数组都在 main 中声明,大小为 2000,所以我不确定这是否是确切的错误。 (或者我可能误解了你在说什么。)
  • @Sam - 2000 个元素很好 - 你传递给 normcount 是什么?是 2000 还是 words 包含的实际字符串数?
  • 我贴出了完整的main函数,count应该是原始数组中元素的个数。哦,我忘了补充一下,还有另一个函数正在使用来确定数组中的单词数,但到目前为止它一直在工作,所以我怀疑这是问题
【解决方案2】:

不确定垃圾输出是什么意思?

如果我用这个 (g++ 4.4.5) 调用你的函数

#include <string>
#include <iostream>
using namespace std;
int
main  (int ac, char **av)
{
  int numwords = 3;
  string sepwords1[] = {"one,", "two", "three"};
  string sepwords2[numwords];
  norm(sepwords1,sepwords2,numwords);  
  for(size_t i=0;i<numwords;++i){
    std::cout<<"sepwords2["<<i<<"] = "<<sepwords2[i]<<std::endl;
  }

}

然后我得到输出

sepwords2[0] = one
sepwords2[1] = onetwo
sepwords2[2] = onetwothree

这不是你想要的吗?

如果你不想串联,那么你需要重置newword变量,

  wordz[i]= newstring;  //this is in your norm function 
  newstring="";         //this is the line I added. 

那么输出就是

sepwords2[0] = one
sepwords2[1] = two
sepwords2[2] = three

【讨论】:

  • 有点像。我只需要从每个单词中删除标点符号(如果它有标点符号)并将其存储在辅助数组中。使用我提供的代码,当我从 sepwords2[] 中计算出来时,我得到每个元素的“0xffbf5828”。我错过了什么吗?另外,我不明白你做了什么来删除逗号
  • @sam 删除角是因为在连接到字符串之前有“if(ispunct(current)==0)”检查 - 我根本没有改变你的函数,除了添加我指出的行。
  • 哦,是的,我知道 ispunct 函数是做什么的,我想你可能会以不同的方式完成它。但我确实添加了你的建议,我明白为什么它是有道理的,但我一直得到值 0xffbf5828。它可能是代码中的其他地方吗?在那之前一切正常,我在 main 中将它们声明为:“sepwords1[2000]”和“sepwords2[2000]”
  • @Sam - 尝试让代码在 main 内部运行,使用 Tom 或我自己的代码。
【解决方案3】:

数组是固定大小的数组。如果您需要从“数组”中添加和删除元素,您应该使用列表或向量,它们是可变大小的序列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    相关资源
    最近更新 更多