地址数组,带有枚举的返回值

Address array with return value of enum

本文关键字:枚举 返回值 数组 地址      更新时间:2023-10-16

这是我的问题:我定义了一些值:

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>。根据用例,您可能需要更换枚举或用地图的数组。

这是一种接近它的方法。

一些笔记:

  1. 根据专业函数实现的转换器函数。

  2. 以小函数表示逻辑。

  3. 注释内联

-

#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访问数组。
但是,与语句相比,开关情况可能是实施它的更好方法。