C++ 如何获取元组的元素
我正在创建一个拼字游戏,我需要对字典上的单词有一个基本分数 我使用make_tuple并将其存储在我的tuple中。有没有一种方法可以像访问向量一样访问元组中的元素C++ 如何获取元组的元素,c++,c++11,stdtuple,C++,C++11,Stdtuple,我正在创建一个拼字游戏,我需要对字典上的单词有一个基本分数 我使用make_tuple并将其存储在我的tuple中。有没有一种方法可以像访问向量一样访问元组中的元素 #include <iostream> #include <tuple> #include <string> #include <fstream> void parseTextFile() { std::ifstream words_file("scrabble_words.
#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;
}
#包括
#包括
#包括
#包括
void parseTextFile()
{
std::ifstream words_file(“scrabble_words.txt”);//包含字典(英语)中不存在的单词的文件
std::ofstream new_words_file(“test.txt”);//只保存现有单词的文件
std::字符串输入;
std::tuple-tupleList;
无符号整数检查\u完整性;
int计数器=0;
while(words\u文件>>word\u输入)
{
检查_完整性=0;
for(无符号整数i=0;i=97&&(int)word_input[i]当有两个以上不同类型的值要存储时,通常会使用a。对于两个值,a是更好的选择
在您的例子中,您想要实现的似乎是一个单词-值对列表。您可以将它们存储在像向量一样的容器中,但也可以将它们作为键值对存储在中。正如您在下面的链接中看到的那样,std::map
实际上是std::pair
对象的集合,元组是对的泛化
为了完整性,如果我对代码用途的理解是正确的,那么这些是对代码的补充,用于将每个元组存储在向量声明中
std::tuple<std::string, int> correct_word = {};
std::vector<std::tuple<std::string, int>> existing_words = {};
..最后是构造循环之外的使用示例:
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;
在构造之后,您将使用.first
访问地图元素,并使用.second
访问计数器。下面是一个也使用for auto循环的打印示例:
for (const auto& correct_word : existing_words)
std::cout << correct_word.first << ": " << correct_word.second << std::endl;
std::cout << counter << std::endl;
for(常量自动和更正单词:现有单词)
std::cout No,std::get
是你如何做的。但是也许你想要std::pair
,它有第一个
和第二个
成员,或者甚至是一个std::map
来执行查找?也许std::map
是我所需要的。对于向量,我会这样做:for(int I=0;Istd::map<std::string, int> existing_words = {};
if(word_input.length() == check_integrity)
{
// ...
existing_words[word_input] = getScore(word_input);
// ...
}
for (const auto& correct_word : existing_words)
std::cout << correct_word.first << ": " << correct_word.second << std::endl;
std::cout << counter << std::endl;