0

因此,我正在尝试完成程序的这一部分,我必须从 Stdin 读取文本文件并将其添加到“单词列表”wl 中。我知道如何从文本文件中读取,但如果有意义的话,我不知道如何将“单词”添加到列表中。所以这就是我得到的:

string getWord(){
    string word;
    while (cin >> word){
        getline(cin, word);
    }
    return word;
}

void fillWordList(string source[], int &sourceLength){
    ifstream in.file;
    sourceLength = 50;
    source[sourceLength]; ///this is the part I'm having trouble on

Source 是一个数组,用于确定从文本中读取的单词数量,length 是屏幕上打印的数量。

关于我应该从什么开始的任何想法?

编辑:这是我正在为其编写实现的程序:

#include <iostream>
#include <string>
#include <vector>
#include "ngrams.h"
void help(char * cmd) {
  cout << "Usage: " << cmd << " [OPTIONS] < INPUTFILE" << endl;
  cout << "Options:" << endl;
  cout << "  --seed RANDOMSEED" << endl;
  cout << "  --ngram NGRAMCOUNT" << endl;
  cout << "  --out OUTPUTWORDCOUNT" << endl;
}
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

1

声明原始数组需要数组的大小是编译时常量。使用 std::vector或至少std::array代替。source如果您想填写它,请通过引用传递。

于 2013-06-03T20:20:01.020 回答