Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 遍历std::string c++;_C++_Regex_Loops_Parsing_String Parsing - Fatal编程技术网

C++ 遍历std::string c++;

C++ 遍历std::string c++;,c++,regex,loops,parsing,string-parsing,C++,Regex,Loops,Parsing,String Parsing,我正在考虑一种方法,如何遍历用户给定的字符串。这与掷骰子有关;格式:xdy[z],其中x是滚动次数,dy是骰子类型,z只是一个整数 格式是这样的:从1-999(x)开始的数字,然后是字母d,然后是一个特定的数字[骰子类型](只有5个可供选择;4,6,12,20100),然后是方括号中的数字从1到100…因此一些示例看起来像这样…1d4[57],889d20[42],43d4[4],1d4[1]-999d100[100]是字符的范围,因此6个字符对12个字符。我不知道该怎么做,这是我现在所拥有的,

我正在考虑一种方法,如何遍历用户给定的字符串。这与掷骰子有关;格式:xdy[z],其中x是滚动次数,dy是骰子类型,z只是一个整数

格式是这样的:从1-999(x)开始的数字,然后是字母d,然后是一个特定的数字[骰子类型](只有5个可供选择;4,6,12,20100),然后是方括号中的数字从1到100…因此一些示例看起来像这样…1d4[57],889d20[42],43d4[4],1d4[1]-999d100[100]是字符的范围,因此6个字符对12个字符。我不知道该怎么做,这是我现在所拥有的,但似乎有更好的方法。我从用户那里得到的输入已经使用正则表达式进行了验证,以确保格式正确。我想将这些值存储在向量数组中,然后最终连接所有内容

void rollDie(std::string input)
{
    int bracketCount;
    std::vector<int> timesRolled;
    std::vector<int> diceType;
    std::vector<int> additional;
    bool d = false;
    bool bc = false;

    for (int i = 0; i < input.length; i++) //or length - 1
    {
        if (isdigit(input[i]))
        {
            if (bool d = false) 
            {
                timesRolled.push_back(input[i]);
            }
        }
        if(isalpha(input[i]))
        {
            d = true;
        }
        if (isdigit(input[i])) 
        {
            if (d = true)
            {
                diceType.push_back(input[i]);
            }
        }
        if (!isalpha(input[i]) && !isdigit(input[i]))
        {
            bracketCount++;
            bc = true;
            if (bracketCount = 2) break;
        }
        if (isdigit(input[i]))
        {
            if (bc = true) 
            {
                additional.push_back(input[i]);
            }
        }
    }
}
void rollDie(标准::字符串输入)
{
括号内计数;
std::向量时间滚动;
std::矢量型;
std::载体附加;
bool d=假;
boolbc=假;
对于(int i=0;i
如果要使用正则表达式验证输入,那么最好使用相同的正则表达式来提取值

比如:

    std::regex e{ R"-((\d{1,3})[Dd](4|6|12|20|100)\[(\d{1,3})\])-" };

    std::cout << "Enter dice roll: " << std::flush;

    std::smatch m;
    for(std::string line; std::getline(std::cin, line);)
    {
        if(std::regex_match(line, m, e))
            break; // if it's good we're done here

        // keep going until we get it right
        std::cout << "Error: bad format, please use: nnndxx[ddd]" << '\n';
    }

    int rolls = std::stoi(m[1]);
    int sides = std::stoi(m[2]);
    int extra = std::stoi(m[3]);

    std::cout << "Rolls: " << rolls << '\n';
    std::cout << "Sides: D" << sides << '\n';
    std::cout << "Extra: " << extra << '\n';
std::regex e{R-((\d{1,3})[Dd](4 | 6 | 12 | 20 | 100)\[(\d{1,3})\])-“;

std::cout
if(d=true)
if(bracketCount=2)
if(bc=true)
不可能正确。所有这些
if
检查都是赋值,然后测试新值是否真实(在所有情况下都是如此)。如果(d=false)
是相同的,但是由于赋值是错误的,测试总是失败。如果您使用正则表达式进行验证,为什么不使用正则表达式来获取数字?如果下面的答案对您不起作用,请提供反馈。如果有效,请接受/投票。谢谢这对我来说很有意义!我和你有相同的正则表达式,但只是为了验证表达式,不知道如何再次使用它,谢谢。我有一个问题,我如何打破你的每个语句?在我得到正确的值后,我想计算一些值,但我不能这样做,因为我陷入了困境loop@lucyb我编辑了答案。只需使用
break语句。我实际上会移动
std::smatch m出循环,并且循环体仅为:
if(std::regex_match(line,m,e))break;std::显然,格式更好,但很难在注释中格式化代码!