读取文本文件,然后添加字符到列表
read text file then add character to list?
所以我试图完成程序的这一部分,我必须从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++完全复制要字符串的字符列表,而不会忽略多个空格
- C++ 存储来自 char* 函数的字符列表
- 如何使用 Swig 将无符号字符* 转换为 Python 列表
- AT 命令列表作为字符 * 数组
- 可变参数字符列表的模板参数推导
- 如何将列表序列化为字符数组
- 拆分位于-(短划线)字符处的QString,列表项访问
- 获取列表<节点*>中的字符并返回
- 正在从链接列表中检索字符
- 如何将列表转换为<string>字符**?
- 我正在编写一个排序函数,以升序对字符类型列表进行排序,如果有任何交换,则返回计数次数
- 如何对列表c++中的字符串中的字符进行索引
- 将连字符作为元素包含在scanf()的扫描集中,如果它是列表中的第一个字符或最后一个字符
- 读取文本文件,然后添加字符到列表
- 在MS-DOS中使用长参数列表(超过128个字符)编译
- 读取字符列表并存储在数组中
- 添加两个字符列表
- 这是获取数字字符列表并使用它们创建长整数的有效方法吗