0

所以我试图完成程序的一部分,我必须在向量字符串中找到最后 n-1 个单词,并将源中的下一个单词移到向量字符串的末尾。在这里,我正在尝试为“findAndShift”编写函数并使用来自另一个程序的参数来使用它。

这是我的程序的一部分。

void findAndShift(vector<string>& ngram, string source[],int sourceLength) {
    if (argc == 2)///default command prompt...not declared?
    {
        ifstream infile;
        infile.open(argv[1], ios::in);
        if (infile.fail())
        {
            cout << argv[0] << ": "<< argv[i] << "I'm afraid I can't let you do that, Dave." << endl;
        }
        else
        {
            //get length of n
            infile.seekg(0, ios::end);
            const int sourceLength = infile.tellg();

            int n = 0;
            string word;
            ngram = rand() % sourceLength;
            while (!infile.eof())
            {
                infile >> source;
                ++n;
                if(counter == ngram);
                    word = n;
            }
        }
    }
    return;
}

这是我必须用于我的程序。

 string source[250000];
 vector<string> ngram;
 int main(int argc, char* argv[]) {
     int n, outputN, sl;
     n = 3;
     outputN = 100;
     for (int i = 0; i < argc; i++) {
         if (string(argv[i]) == "--seed") {
             srand(atoi(argv[i+1]));
         } else if (string(argv[i]) == "--ngram") {
             n = 1 + atoi(argv[i+1]);
         } else if (string(argv[i]) == "--out") {
             outputN = atoi(argv[i+1]);
         } else if (string(argv[i]) == "--help") {
            help(argv[0]);
            return 0; }
     }
     fillWordList(source,sl);
     cout << sl << " words found." << endl;
     cout << "First word: " << source[0] << endl;
     cout << "Last word:  " << source[sl-1] << endl;
     for (int i = 0; i < n; i++) {
         ngram.push_back(source[i]);
     }
     cout << "Initial ngram: ";
     put(ngram);
     cout << endl;
     for (int i = 0; i < outputN; i++) {
         if (i % 10 == 0) {
             cout << endl;
         }
         //put(ngram);
         //cout << endl;
         cout << ngram[0] << " ";
         findAndShift(ngram, source, sl);
     } }

有任何想法吗?

4

1 回答 1

0

您必须在程序 argc 和 argv[] 中传递更多参数,因为 argc 只是主函数的参数,因此在其他函数中不可见。

这是您修改后的代码。

void findAndShift(vector<string>& ngram, string source[],int sourceLength, int argc, char argv[0], argv[1],argv[i]){
    if (argc == 2)///default command prompt...not declared?
    {
        ifstream infile;
        infile.open(argv[1], ios::in);
        if (infile.fail())
        {
            cout << argv[0] << ": "<< argv[i] << "I'm afraid I can't let you do that, Dave." << endl;
        }
        else
        {
            //get length of n
            infile.seekg(0, ios::end);
            const int sourceLength = infile.tellg();

            int n = 0;
            string word;
            ngram = rand() % sourceLength;
            while (!infile.eof())
            {
            infile >> source;
            ++n;
            if(counter == ngram);
             word = n;
            }
        }
    }
    return;
}

你的主要代码

string source[250000];
 vector<string> ngram;
 int main(int argc, char* argv[]) {
   int n, outputN, sl;
   n = 3;
   outputN = 100;
   for (int i = 0; i < argc; i++) {
     if (string(argv[i]) == "--seed") {
       srand(atoi(argv[i+1]));
     } else if (string(argv[i]) == "--ngram") {
       n = 1 + atoi(argv[i+1]);
     } else if (string(argv[i]) == "--out") {
       outputN = atoi(argv[i+1]);
     } else if (string(argv[i]) == "--help") {
       help(argv[0]);
 return 0; }
   }
   fillWordList(source,sl);
   cout << sl << " words found." << endl;
   cout << "First word: " << source[0] << endl;
   cout << "Last word:  " << source[sl-1] << endl;
   for (int i = 0; i < n; i++) {
     ngram.push_back(source[i]);
   }
   cout << "Initial ngram: ";
   put(ngram);
   cout << endl;
   for (int i = 0; i < outputN; i++) {
if (i % 10 == 0) {
  cout << endl;
}
//put(ngram);
//cout << endl;
cout << ngram[0] << " ";
findAndShift(ngram, source, sl, argc,argv[0],argv[1],argv[i]);
 } }

我没有测试过。如果它仍然不起作用,请尝试在 findAndShift 的参数列表中使用 char *

希望能帮助到你....

于 2013-06-05T02:37:33.133 回答