我的问题在于我的constructChord 函数。我无法为我的 keyList 数组返回 3 个单独的索引
我真的明白了,对吗?
是的,你可以!使用 c++11 很容易,使用 c++17 很神奇。我在示例中使用了 int,因为您使用了 int,但几乎所有类型都可以进行元组处理。
c++11
#include <tuple>
std::tuple<int, int, int> Music::constructChord(ChordType chord)
{
// do something to calculate int a, b, c
return std::make_tuple(a, b, c); // just an example of course
}
c++17
#include <tuple>
std::tuple<int, int, int> Music::constructChord(ChordType chord)
{
// do something to calculate int a, b, c
return {a, b, c}; // that is really cool, isn´t it?
}
对输入进行清理和元组化
这是一个关于如何对输入进行清理和元组处理的快速破解示例。我已经放入了一个 main(),因此可以直接编译和使用它:
#include <iostream>
#include <map>
#include <regex>
int main()
{
// this map is only a stub of course, a lot is missing ...
std::map<std::string, int> sanitation = {{"a",0}, {"bb",1}, {"c",2}, {"c#",3}, {"#c",3}, {"db",3}, {"bd",3}};
// input block from your code
std::string myChord;
std::cout << "Please enter a chord, at least three different piano keys:\n";
getline(std::cin, myChord);
transform(myChord.begin(), myChord.end(), myChord.begin(), ::tolower);
// parsing the input
std::regex regex( R"(([a-g#]{1,2}) ([a-g#]{1,2}) ([a-g#]{1,2}))");
std::smatch m;
std::regex_search(myChord, m, regex);
// sanitizing und tupleing it
std::vector<int> matched;
for (int i=1; i<m.size(); i++)
{
auto hit = sanitation.find(m[i]);
if(hit!=sanitation.end())
matched.push_back(hit->second);
}
auto my_tuple = std::make_tuple(matched);
return 0;
}