我继续在函数中从另一个数组修改的数组中获取垃圾值
I continue to get garbage values in an array that have been modified from another array, in a function
我对编程有点陌生,所以我的问题的答案还不是很明显,尽管我已经尝试了很多方法来完成任务。
问题是我试图取一个单词数组,从数组中删除任何标点符号,并将新单词放入单独的数组中。我尝试这样做,但当我输出新数组时,我一直得到垃圾值。
代码如下:
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;
}
}
完整的main函数是:
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;
}
你的代码应该可以工作,但是可能在main函数中有一个问题,比如你的数组和你必须使用两个数组的事实,所以我看到这种行为的一个原因可能是你的数组大小不匹配,存储原始字符串的那个比你复制到的那个大。
#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
的连接问题,但后来没有清除它。
你可以更简洁地解决这个问题,通过使用标准<algorithm>
的东西,并通过使用<vector>
将处理增长和调整大小。
#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';
}
}
不确定垃圾输出是什么意思?
如果我用这个(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
数组是固定大小的数组。如果你需要从"数组"中添加和删除元素,你应该使用列表或向量,它们是可变大小的序列。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 使用指针从C++中的数组中获取最大值
- 获取字符串的长度并将其分配给数组
- 数组长度,为什么从命令行获取时不能使用它?
- 如何在 nlohmann 的 json 库中获取数组长度?
- 在c++中获取两个大int,并将它们存储在数组中
- 如何在C++中获取二维数组中最少的一列数?
- 从 numpy 数组获取指针以将图像发送到C++
- 指针数组.获取指针元素的值
- 循环以使用 cstring 数组 c++ 获取用户输入
- 一种从元组/数组获取参数包的方法
- 如何使用数组获取输入,并将其存储在record.h类的主类中的变量中
- 如何使用数组获取命令行参数
- 从 JSON 数组获取天气
- 布尔数组获取意外的默认值
- 填充列表<>使用数组获取错误的数字
- 如何在C++中为矢量数据结构中的二维数组获取用户输入,而不使用行和列上的两个for循环
- 在自定义库中操作2d C++数组-获取行数
- 使用数组获取测试结果
- 数组c++获取值