如何将行的第一个单词作为一个变量,将行的其余部分作为另一个变量进行归档/存储
How to infile/store first word of line as one variable, rest of line as another
我有一个文本文件,我试图读取并存储在这里:
5
chrestomathy A selection of passages from an author or authors, designed to help in learning a language
detectable Able to be discovered or identified
feldspar An abundant rock-forming mineral typically occurring as colorless or pale-colored crystals
haricot A bean of a variety with small white seeds, especially the kidney bean
pluripotent Capable of giving rise to several different cell types
每一行都是一个单词,后跟其定义,其中大写字母开始定义。我不知道如何将单词和定义归档/存储在单独的变量中,因为它们都是字符串。
我在下面创建了一个模板化地图类:
template <typename Domain, typename Range>
class Map
{
public:
Map(int n); // number of entries in the table
~Map();
void add(Domain d, Range r); // add an entry to the table
bool lookup(Domain d, Range& r);
private:
int numEntries;
Domain* dArray;
Range* rArray;
};
这个"字典"应该是这个地图的实例化,其中单词是域,范围是定义。
Executive::Executive(string file1)
{
int n;
Map<string, string> Dictionary;
ifstream inFile1;
inFile1.open("Dictionary0.txt");
inFile1>>n;
for(int i=0; i<n; i++)
{
//Dictionary.add(word, def);
//Feel like i need to use something like this too but not sure how
}
}
我想读取和存储这些值,以便以后可以使用查找函数检查单词是否在"字典"(文件(中,然后打印其定义。我创建了一个执行类来阅读和存储。
第 1 步:使用>>
将第一个单词读入std::string
:
std::string word;
inFile >> word;
第 2 步:跳过单词后面的空格:
inFile >> std::skipws;
第 3 步:使用std::getline
阅读该行的其余部分:
std::string definition;
std::getline(std::cin, definition);
相关文章:
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 如何将变量存储为无
- 有没有办法强制C++编译器将变量存储在寄存器中?
- C++ - thread_local变量存储在哪里?
- 如何使用 c++ 将多个变量存储在一个变量中?例如,双精度 x,y,z 包含在可变点 p 中
- 如何将文件从文件作为变量存储
- 引用变量存储在哪里
- 尝试在C 程序中设置简单的键盘输入.使用变量存储True或false
- 如何将变量存储在另一个文件中
- 编译时局部变量存储在哪里
- lambda 捕获的变量存储在哪里
- 闭包捕获的变量存储在哪里?
- 静态和全局变量存储说明
- 模式名称,将值作为静态变量存储在函数中
- 任何人都能解释常量或常量变量存储在哪里吗
- 将变量存储在将被多次使用的函数中的最佳方式(c++)
- 如何将字符变量存储为二维字符变量
- 将环境变量存储在动态分配的数组中
- 从矢量中获取选定的数据,然后作为变量存储
- DLL的全局变量存储在内存中