简体   繁体   English

C ++通过引用传递字符串数组

[英]C++ passing a string array by reference

I'm new in C++, and I'm trying to write a program that using only function (no vector or template) should be able to read an extarnal file and save everything in one array. 我是C ++的新手,我正在尝试编写一个仅使用函数(不使用向量或模板)应该能够读取外部文件并将所有内容保存在一个数组中的程序。 To dimension my array i first read the total number of lines in the file, and based on taht I dimensioned my array. 为了确定数组的大小,我首先读取了文件中的总行数,然后根据taht确定了数组的大小。 Then I re-read all the lines in the file and I started to store them in the array. 然后,我重新读取文件中的所有行,并开始将它们存储在数组中。 Everything went well. 一切顺利。 But when I got the code and tried to make us a feature I got stuck. 但是,当我获得代码并试图为我们提供功能时,我陷入了困境。 I can not find any way to pass the array by reference. 我找不到任何通过引用传递数组的方法。 Each time I find a variety of errors. 每次我发现各种错误。 Can someone help me please? 有人能帮助我吗? Thank you 谢谢

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

} }

Assuming your array was declared as string allLines[lines_count] you should should declare the function as 假设您的数组声明为string allLines[lines_count] ,则应将函数声明为

void parseLine(string* allLines, int lines_count2, string line) or void parseLine(string* allLines, int lines_count2, string line)

void parseLine(string allLines[], int lines_count2, string line)

and call the function as parseLine(allLines, lines_count2, line) 并将函数调用为parseLine(allLines, lines_count2, line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM