c++ - 读取文本文件然后将字符添加到列表?

标签 c++ ifstream void words

所以我正在尝试完成程序的这一部分,我必须从 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

关于c++ - 读取文本文件然后将字符添加到列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905006/

相关文章:

c - 最终程序从 C 函数返回

c - 当我们将返回类型为 void 的函数调用返回值分配给 int 变量时会发生什么?

c++ - 如何在 Qt3D 中绘制一条简单的线?

c++ - 无法理解 [basic.link]/6 C++14 示例中的声明 #3

c++ - 什么是内部字段计数以及 SetInternalFieldCount 用于什么?

c++ - 从输入文件中读取多行

C++ ifstream 函数和字段分隔符

c++ - 二维插值

c++ - 使用 C++ 从非指定符号中清除 CSV 文件

c++ - 转换为无效*