读取文本文件,然后添加字符到列表

read text file then add character to list?

本文关键字:字符 列表 添加 然后 取文本 文件 读取      更新时间:2023-10-16

所以我试图完成程序的这一部分,我必须从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);
} }

我应该用这个作为参考,但它没有帮助我。

声明原始数组要求数组的大小为编译时常数。使用std::vector或至少std::array代替。并通过引用传递source,如果你想填充它。