gpt4 book ai didi

c++ - 如何将行的第一个字作为一个变量输入/存储,将行的其余部分作为另一个变量

转载 作者:行者123 更新时间:2023-11-28 04:10:08 25 4
gpt4 key购买 nike

我有一个文本文件,我想读入并存储在这里:

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

每一行都是一个单词,后面是它的定义,大写字母开始定义。我不知道如何在单独的变量中输入/存储单词和定义,因为它们都是字符串。

我在下面创建了一个模板化 map 类:

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;


};

这个“词典”应该是这个 map 的一个实例,其中单词是域,范围是定义。

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

}

}

我想读取并存储这些值,以便稍后可以使用查找函数来检查单词是否在“字典”(文件)中,然后打印其定义。我创建了一个 Executive 类来读取和存储。

最佳答案

第 1 步:使用 >>> 将第一个单词读入 std::string:

std::string word;
inFile >> word;

第 2 步:跳过单词后的空格:

inFile >> std::skipws;

第 3 步:使用 std::getline 读取该行的其余部分:

std::string definition;
std::getline(std::cin, definition);

关于c++ - 如何将行的第一个字作为一个变量输入/存储,将行的其余部分作为另一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58003339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com