地址数组,带有枚举的返回值
Address array with return value of enum
这是我的问题:我定义了一些值:
enum HarmCons
{
val1 = 0,
val2 = 2,
val3 = 4,
val4 = 6
};
我有一个数组
int foo [7] = { 16, 2, 77, 40, 1222, 34, 5};
我从输入文件中读取的内容。我也有:
char types[15] = "val1,val3,val4";
现在,我想通过commata和地址(在循环中)将字符串分开, foo
中的条目。因此,字符串的第一部分是val1
,它应该返回16
。我尝试了foo[val1]
效果很好。问题是,通过分开字符串,我将始终得到一个字符串或其他内容,并且无法通过字符串来解决数组。是否有可能以某种方式使用结果来解决数组?
似乎您正在尝试使用锤子驾驶螺钉。数组是一个不错的数据结构,但不符合所有问题。
总是会得到一个字符串之类的东西,我无法通过字符串
来解决数组
的确,您不能。因此,您需要数组以外的其他东西。看来您有一个从字符串到整数值的映射。此类映射的数据结构是...地图(也称为关联数组,字典,...)
标准库为您提供了实现:std::map<std::string, int>
。根据用例,您可能需要更换枚举或用地图的数组。
这是一种接近它的方法。
一些笔记:
根据专业函数实现的转换器函数。
以小函数表示逻辑。
注释内联
-
#include <type_traits>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <vector>
#include <iostream>
#include <iomanip>
// general concept of value conversion
template<class From, class To>
struct converter;
// helper function to select correct converter class
template<class To, class From>
To convert(From&& from)
{
return converter<std::decay_t<From>, To>()(std::forward<From>(from));
};
enum HarmCons
{
val1 = 0,
val2 = 2,
val3 = 4,
val4 = 6
};
// specialise for converting from string to our enum
template<>
struct converter<std::string, HarmCons>
{
HarmCons operator()(std::string in) const {
using namespace std::literals;
std::transform(std::begin(in), std::end(in), std::begin(in), [](auto&& c) { return std::tolower(c); });
if (in == "val1") return HarmCons ::val1;
if (in == "val2") return HarmCons ::val2;
if (in == "val3") return HarmCons ::val3;
if (in == "val4") return HarmCons ::val4;
throw std::invalid_argument("out of range: "s + in);
}
};
// helper functions
template<class Sequence, class F>
void for_each_comma(Sequence&& seq, F&& f)
{
std::istringstream splitter(std::forward<Sequence>(seq));
std::string name;
while(std::getline(splitter, name, ','))
{
f(name);
}
};
std::vector<int> read_int_array(std::istream& is)
{
std::vector<int> values;
int N;
is >> N;
values.resize(N);
for (int i = 0 ; i < N ; ++i) {
is >> values[i];
}
return values;
}
std::string dequoted_string(std::istream& is)
{
std::string result;
is >> std::quoted(result);
return result;
}
// test against any istream
void test(std::istream& input)
{
// read in test data
input.exceptions(std::ios_base::badbit);
auto values = read_int_array(input);
// read in comma- delimted string
auto value_names = dequoted_string(input);
// perform conversion and lookup
for_each_comma(value_names, [&](auto&& name){
auto cons = convert<HarmCons>(name);
std::cout << values.at(cons) << 'n';
});
}
// our test
int main()
{
std::istringstream testinput(R"__(
7
16 2 77 40 1222 34 5
"val1,val3,val4"
)__");
test(testinput);
}
预期输出:
16
1222
5
如果您真正希望拆分字符串,则可以这样做,然后将最后一个字符与描述为chars的数字进行比较。
有点这样:
If ( char1 == '1')
Then set a int var to 1
并有一个循环进行相同的事情,但是随着添加时,数十个,数百个等等的数量增加。
然后,您可以使用该int var访问数组。
但是,与语句相比,开关情况可能是实施它的更好方法。
相关文章:
- 为什么 int 类型的枚举类值不能用作 int
- 如何使用默认值为构造函数中的枚举赋值?
- 使用 Doxygen 记录枚举类值,而不启用EXTRACT_ALL
- 在结构中使用枚举的值
- 在运行时使用枚举器值作为模板函数的模板参数的元程序
- 如何从枚举类值中指定模板函数参数中的数组大小?
- 为什么isocpp允许我们超过枚举类值
- 比较枚举的值会给出编译器错误
- C++枚举索引值
- 沿枚举 c++ 返回变量类型
- 比较枚举类值时的奇怪行为
- 了解 Levenberg Marquardt 枚举返回
- 使用对其他枚举值的操作定义枚举元素值
- 从枚举类值模板参数推断枚举类类型
- 如何将枚举返回值转换为C 异常
- 引用枚举枚举返回错误的价值
- C MFC中非命中枚举的值
- 为什么可以获取此枚举成员值,而不必先深入研究嵌套枚举
- 在C++中显示、更新和验证枚举的值
- switch()中未处理的枚举类值-异常或断言