c++ - 如何获取元组的元素

标签 c++ c++11 stdtuple

我正在创建一个拼字游戏,我需要对字典中的单词进行基本评分。

我使用了 make_tuple 并将其存储在我的元组中。有没有办法像访问 vector 一样访问元组中的元素?

#include <iostream>
#include <tuple>
#include <string>
#include <fstream>

void parseTextFile()
{
    std::ifstream words_file("scrabble_words.txt"); //File containing the words in the dictionary (english) with words that do not exist
    std::ofstream new_words_file("test.txt"); //File where only existing words will be saved
    std::string word_input;
    std::tuple<std::string, int> tupleList;

    unsigned int check_integrity;
    int counter = 0;

    while(words_file >> word_input)
    {
        check_integrity = 0;
        for (unsigned int i = 0; i < word_input.length(); i++)
        {
            if((int)word_input[i] >= 97 && (int)word_input[i] <= 123) //if the letter of the word belongs to the alphabet
            {
                check_integrity++;
            }
        }

        if(word_input.length() == check_integrity)
        {
            new_words_file << word_input << std::endl; //add the word to the new file
            tupleList = std::make_tuple(word_input, getScore(word_input)); //make tuple with the basic score and the word
            counter++; //to check if the amount of words in the new file are correct
            std::cout << std::get<0>(tupleList) << ": " << std::get<1>(tupleList) << std::endl;
        }
    }

    std::cout << counter << std::endl;
}

最佳答案

人们通常会使用 tuple当要存储两个以上不同类型的值时。对于两个值 a pair是更好的选择。

在您的情况下,您想要实现的似乎是一个词值对列表。您可以将它们存储在容器中,例如 vector ,但您也可以将它们作为键值对存储在 map 中。 .正如您在点击链接时看到的那样,std::map 实际上是 std::pair 对象的集合,元组是对的概括。

为了完整起见,如果我对您的代码目的的理解是正确的,那么这些是对您的代码的补充,用于将每个元组存储在一个 vector 中 - 声明,

   std::tuple<std::string, int> correct_word = {};
   std::vector<std::tuple<std::string, int>> existing_words = {};

保存现有单词的循环中的更改 - 在这里您要将每个单词值元组添加到 vector 中,

 if(word_input.length() == check_integrity)
 {
    // ...
    correct_word = std::make_tuple(word_input, getScore(word_input)); 
    existing_words.push_back(correct_word);
    // ...
 }

..最后是构造循环外的用法示例:

for (size_t iv=0; iv<existing_words.size(); ++iv)
{
    correct_word = existing_words[iv];
    std::cout << std::get<0>(correct_word) << ": " << std::get<1>(correct_word) << std::endl;
}
std::cout << counter << std::endl;

带有 map 的相同代码如下所示:

唯一的声明是从字符串到值的映射(而不是元组和元组 vector ),

std::map<std::string, int> existing_words = {};

在构造循环中,您将像这样在一行中创建 map 对,

    if(word_input.length() == check_integrity)
    {
        // ...
        existing_words[word_input] = getScore(word_input);
        // ...
    }

在构建之后,您将使用 .first 作为单词访问 map 元素,使用 .second 作为计数器访问 map 元素。下面是一个同样使用 for 自动循环的打印示例:

for (const auto& correct_word : existing_words)
    std::cout << correct_word.first << ": " << correct_word.second << std::endl;

    std::cout << counter << std::endl;

请注意 map 默认按字母顺序排列,您可以提供自己的排序规则并使用 unordered map如果您不想进行任何排序/排序。

关于c++ - 如何获取元组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375722/

相关文章:

C++蜂鸣声不起作用

c++ - 如何遍历类似树结构的Windows资源管理器

c++ - 处理多线程清理的最佳方式

c++ - 将 lambda 应用于模板参数包;未捕获 lambda 参数

C++:将元组转换为类型 T

c++ - 如何使映射键具有两种不同的数据类型?

c++ - 如何解决 N-Api Addon C 中的 Node.js Promise

c++ - QT中如何从json中加载base64图片数据

c++ - 对象、右值引用、常量引用之间的重载解析

c++ - 元组统一初始化