C++通过引用传递字符串数组
C++ passing a string array by reference
我是C++的新手,我正在尝试编写一个程序,只使用函数(没有向量或模板)就可以读取extarnal文件并将所有内容保存在一个数组中。为了给我的数组定尺寸,我首先读取文件中的总行数,然后根据它来给数组定尺寸。然后我重新读取文件中的所有行,并开始将它们存储在数组中。一切顺利。但当我得到代码并试图让我们成为一个功能时,我陷入了困境。我找不到任何通过引用传递数组的方法。每次我都会发现各种各样的错误。有人能帮帮我吗?谢谢
while (!infile.eof())
{
// read the file line by line
getline (infile,line);
++lines_count2;
// call a function that take out the tags from the line
parseLine(&allLines[lines_count], lines_count2, line);
} // end while
// FUNCTION PARSE
// Within parseHTML, strip out all of the html tags leaving a pure text line.
void parseLine(string (&allLines)[lines_count], int lines_count2, string line)
{
// I don-t take empty lines
if (line!="")
{
// local var
string del1="<!DOCTYPE", del2="<script";
int eraStart=0, eraEnd=0;
// I don't take line that start with <!DOCTYPE
eraStart = line.find(del1);
if (eraStart!=std::string::npos)
line.clear();
// I don't take line that start with <script
eraStart = line.find(del2);
if (eraStart!=std::string::npos)
line.clear();
//out
cout << "Starting situation: " << line << "n n";
// A. counting the occourence of the character ">" in the line
size_t eraOccur = count(line.begin(), line.end(), '<');
cout << "numero occorenze" << eraOccur << "n n";
// declaring the local var
string str2 ("<"), str3 (">");
// B. loop in the line for each occurence...looking for < and > position
for (int i=0; i<eraOccur; i++)
{
// looking for position char "<"
eraStart = line.find(str2);
if (eraStart!=string::npos)
{
cout << "first 'needle' found at: " << eraStart << 'n';
// looking for position char ">"
eraEnd = line.find(str3);
if (eraEnd!=string::npos)
cout << "second 'needle' found at: " << eraEnd << 'n';
eraEnd=eraEnd-eraStart+1;
//delete everything between < and >
cout << "start " << eraStart << " end " << eraEnd << "n n";
line.erase(eraStart, eraEnd);
}
}
cout << "memo situation: " << line << "n n";
// C. memorize the result into an array
allLines[lines_count2] = line;
} // end if
}
假设您的数组被声明为string allLines[lines_count]
,则应该将函数声明为
void parseLine(string* allLines, int lines_count2, string line)
或
void parseLine(string allLines[], int lines_count2, string line)
并将函数调用为parseLine(allLines, lines_count2, line)
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组