gpt4 book ai didi

c++ - For-Loop - 即使代码为 'int i = 0' , i 的值也不是 0 。 i = 大数字,为什么?怎么修?

转载 作者:行者123 更新时间:2023-11-30 04:00:37 24 4
gpt4 key购买 nike

我正在设计一个程序来清理包含代码的文本文件。它删除注释、多余的空格/行,并为文件中具有多个分号的行创建一个新行。

我实际上让这个程序运行了,但它使用了数组。因为我正在开发另一个以此为基础的程序,除了数据输入的大小更多样化,我正在将它转换为使用 vector 而不是标准数组,这样我就可以重新调整程序的用途……这有点像观点。

我的问题是,在程序遍历第一个 for 循环后,其余的 for 循环将迭代器初始化为值“3435973836”,而不管是否正确声明 ('int i = 0', 'int k = 0' 等)。我声明它们是无符号的并且省略 singed/unsigned 仍然会错误地初始化值 (-858993460)。

这会做 2 件事中的 1 件事:

  1. unsigned 循环永远不会开始,因为值太高而无法启动循环。
  2. 省略会使循环运行很长时间。

有什么想法吗?我已经在下面发布了代码。请忽略除此之外我犯的任何其他错误,因为我无法通过此来调试其他任何内容。

编辑 --> 已解决:我按值传递 vector 的问题。但即使我将其更改为通过引用传递,该程序仍然无法运行。实际问题出在 Microsoft Visual Studio 2012 上。一旦我 PBV 一次,它就会破坏我的项目。我不得不开始一个新的 VS 项目并插入代码。如果执行此操作,请注意不要在 PBV 状态下运行程序,否则您将不得不再次执行此操作。我不知道为什么会这样。也许了解 MS Visual Studio 的人可以回答这个问题。再次感谢社区!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void removeComments(vector<string> row, ifstream & myfile);
void sortLines (vector<string> row);
//void cleanCode (vector<string> row);

int main()
{
vector<string> row;
ifstream myfile;

//Open txt file
myfile.open("newdata.txt");

if(myfile.is_open())
{
//Remove comments, create proper lines, and remove/add spaces.
removeComments(row, myfile);
sortLines(row);
//cleanCode(row);
}

else
{
cout<< "ERROR: No file was able to open. Check the file name or location and try again."<< endl << endl;
}


for (unsigned int i = 0; i < row.size(); i++)
{
cout<< row[i] << endl;
}

cout<< endl;
myfile.close();
system("PAUSE");
return 0;
}

//FUNCTIONS


//Removes all comments.
void removeComments(vector<string> row, ifstream & myfile)
{

string line;

while(getline(myfile, line))
{
string tempString;
for(unsigned int i = 0; i < line.length(); i++)
{
//Copy characters to row string array until "//".
//All character following and including "//" will be ignored.
if(line.at(i) == '/' && line.at(i+1) == '/')
{
break;
}

else
{
tempString += line.at(i);
}
}

row.push_back(tempString);
}



}

//Creates a new line after every semi-colon.
void sortLines (vector<string> row)
{
vector<string> tempRow;
string tempLine;
string tempString;

for (unsigned int i = 0; i < row.size(); i++)
{
tempLine = row [i];
for (unsigned int j = 0; j < tempLine.length(); j++)
{
tempString += tempLine[j];
if (tempLine[j] == ';')
{
tempRow.push_back(tempString);
}
}
}

//Revalue row array elements.

//DEBUGGING OUTPUT
for (unsigned int i = 0; i < tempRow.size(); i++)
{
cout<< tempRow[i] << endl;
}

row.clear();
row = tempRow;

}

好的,这是我的引用编辑:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void removeComments(vector<string> &row, ifstream & myfile);
void sortLines (vector<string> &row);
//void cleanCode (vector<string> &row);

int main()
{
vector<string> row;
ifstream myfile;

//Open txt file
myfile.open("newdata.txt");

if(myfile.is_open())
{
//Remove comments, create proper lines, and remove/add spaces.
removeComments(row, myfile);
sortLines(row);
//cleanCode(row);
}

else
{
cout<< "ERROR: No file was able to open. Check the file name or location and try again."<< endl << endl;
}


for (unsigned int i = 0; i < row.size(); i++)
{
cout<< row[i] << endl;
}

cout<< endl;
myfile.close();
system("PAUSE");
return 0;
}

//FUNCTIONS


//Removes all comments.
void removeComments(vector<string> &row, ifstream & myfile)
{

string line;
while(getline(myfile, line))
{
string tempString;
for(unsigned int i = 0; i < line.length(); i++)
{
//Copy characters to row string array until "//".
//All character following and including "//" will be ignored.
if(line.at(i) == '/' && line.at(i+1) == '/')
{
break;
}

else
{
tempString += line.at(i);
}
}

row.push_back(tempString);
}



}

//Creates a new line after every semi-colon.
void sortLines (vector<string> &row)
{
vector<string> tempRow;
string tempLine;
string tempString;

for (unsigned int i = 0; i < row.size(); i++)
{
tempLine = row [i];
for (unsigned int j = 0; j < tempLine.length(); j++)
{
tempString += tempLine[j];
if (tempLine[j] == ';')
{
tempRow.push_back(tempString);
}
}
}

//Revalue row array elements.

//DEBUGGING OUTPUT
for (unsigned int i = 0; i < tempRow.size(); i++)
{
cout<< tempRow[i] << endl;
}

row.clear();
row = tempRow;

}

最佳答案

正如其他人所说,您:

  • 按值传递 vector

  • 使用可能未初始化的超出我的范围的东西(这在您的问题中没有声明/定义)作为增量变量

    //Creates a new line after every semi-colon.
    void sortLines (vector<string> row)
    {
    vector<string> tempRow;
    string tempLine;
    string tempString;

    for (unsigned int i = 0; i < row.size(); k++) // <-- what is k??
    {

关于c++ - For-Loop - 即使代码为 'int i = 0' , i 的值也不是 0 。 i = 大数字,为什么?怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com