Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文本文件中读取两列_C++ - Fatal编程技术网

C++ 从文本文件中读取两列

C++ 从文本文件中读取两列,c++,C++,注意:英语短语与法语译文之间用制表符分隔 我想知道的是,是否有可能将每一列读入两个不同的变量 我试过: you vous today aujourd'hui good bon good morning bonjour afternoon après-midi good evening bonsoir much beaucoup is est 英语>>法语; cout我想您应该使用类似于std::map的东西,其中您有一个

注意:英语短语与法语译文之间用制表符分隔

我想知道的是,是否有可能将每一列读入两个不同的变量

我试过:

you     vous
today       aujourd'hui
good        bon
good morning    bonjour
afternoon   après-midi
good evening    bonsoir
much        beaucoup
is      est
英语>>法语;

cout我想您应该使用类似于
std::map
的东西,其中您有一个单词作为特定语言的键(请阅读评论中的解释):

std::多地图字典;

我想您应该使用类似于
std::map
的东西,其中您有一个单词作为特定语言的键(请阅读注释中的解释):

std::多地图字典;
接受第三个参数-
delim
-分隔符字符,如果第一次调用将其指定为
'\t'
(制表符),则应获得所需的结果:

std::multi_map<std::string,std::string> dictionary;
接受第三个参数--
delim
--分隔符字符,如果在第一次调用中将其指定为
'\t'
(制表符),则应获得所需的结果:

std::multi_map<std::string,std::string> dictionary;

你需要一些东西来表示英语单词和法语单词的起始位置,否则很难解决你的3个单词的问题是的,您可以将每一列读取到不同的变量,但需要某种类型的分隔符来分隔英文和法文单词如果尚未这样做,您的循环条件可能会在以后给您带来麻烦,请参阅:您可以使用“tab”作为分隔符,读取不同变量中的英文和法文单词。如果我正确理解格式,最简单的方法可能是通读整行,搜索一行中的多个空格,然后将前面的所有内容都用作英语,后面的所有内容都用作法语。你需要一些东西来指示英语单词停止和法语单词开始的位置,否则很难解决你的3个单词问题Yes,您可以将每一列读取到不同的变量,但需要某种类型的分隔符来分隔英文和法文单词如果尚未这样做,您的循环条件可能会在以后给您带来麻烦,请参阅:您可以使用“tab”作为分隔符,读取不同变量中的英文和法文单词。如果我正确理解格式,最简单的方法可能是通读整行,在一行中搜索多个空格,然后将前面的所有内容都用作英语,后面的所有内容都用作法语。
std::map<std::string,std::string> dictionary;
ifstream in;
in.open("Q3.dic");

std::string line;
while(getline(in, line)) { // Read whole lines first ...
    std::istringstream iss(line); // Create an input stream to read your values
    std::string english; // Have variables to receive the input
    std::string french;  // ...
    if(iss >> english >> french) { // Read the input delimted by whitespace chars
        dictionary[english] = french; // Put the entry into the dictionary.
                                      // NOTE: This overwrites existing values 
                                      // for `english` key, if these were seen
                                      // before.
    }
    else {
       // Error ...
    }
}

for(std::map<std::string,std::string>::iterator it = dictionary.begin();
    it != dictionary.end();
    ++it) {
    std::cout << it->first << " = " << it->second << std::endl;
}
std::map<std::string,std::set<std::string>>> dictionary;
std::multi_map<std::string,std::string> dictionary;
std::ifstream in("Q3.dic");

for (std::string english, french;
    std::getline(in, english, '\t') && std::getline(in, french);
    )
{
    std::cout << "English: " << english << " - French: " << french
        << std::endl;
}
English: you - French:  vous
English: today - French:        aujourd'hui
English: good - French:         bon
English: good morning - French: bonjour
English: afternoon - French: après-midi
English: good evening - French: bonsoir
English: much - French:         beaucoup
English: is - French:   est