C++ 如何在字符串输入中查找模式?

C++ 如何在字符串输入中查找模式?,c++,string,C++,String,我如何解析一个看起来像“xxxx xxxx”的字符串,并将这些xxxx部分作为一个数字?例如,用户将键入“9349-2341”,我将把这些数字作为两个不同的整数 我需要对一个随机数生成器这样做,它在这些变量之间选择数字 谢谢。您可以使用从字符串中提取数字。看起来是这样的: std::stringstream str_stream; std::string str_to_parse = "1234-5678"; int num[2]; str_stream << str_to_par

我如何解析一个看起来像“xxxx xxxx”的字符串,并将这些xxxx部分作为一个数字?例如,用户将键入“9349-2341”,我将把这些数字作为两个不同的整数

我需要对一个随机数生成器这样做,它在这些变量之间选择数字

谢谢。

您可以使用从字符串中提取数字。看起来是这样的:

std::stringstream str_stream;
std::string str_to_parse = "1234-5678";
int num[2];

str_stream << str_to_parse;
str_stream >> num[0];
str_stream.ignore(1); // otherwise it will extract negative number (-5678)
str_stream >> num[1];
std::stringstream str\u stream;
std::string str_to_parse=“1234-5678”;
int-num[2];
str_stream>num[0];
str_stream.ignore(1);//否则将提取负数(-5678)
str_stream>>num[1];
还有C函数,比如。例如,可以使用以下格式提取模式:
%d-%d

您可以使用从字符串中提取数字。看起来是这样的:

std::stringstream str_stream;
std::string str_to_parse = "1234-5678";
int num[2];

str_stream << str_to_parse;
str_stream >> num[0];
str_stream.ignore(1); // otherwise it will extract negative number (-5678)
str_stream >> num[1];
std::stringstream str\u stream;
std::string str_to_parse=“1234-5678”;
int-num[2];
str_stream>num[0];
str_stream.ignore(1);//否则将提取负数(-5678)
str_stream>>num[1];
还有C函数,比如。例如,可以使用以下格式提取模式:
%d-%d

使用正则表达式 如果您的输入被确认为类似于“xxxx xxxx”,其中“x”代表一个数字,您可以简单地使用以下功能:

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string input = "9349-2341";

    // This pattern matches any string begining with 4 digits and ending with 4 digits, both parts seperated by a slash
    string pattern = "([0-9]{4})-[0-9]{4}";
    smatch matcher;

    regex prog (pattern);

    if (regex_search(input, matcher, prog))
    {
        auto x = matcher[1];
        cout << x << " " << endl;
        input = matcher.suffix().str();
    }
    else
    {
        cout << "Invalid input!" << endl;
    }

    return 0;
}
或者,简单如下:

使用
sscanf
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
字符输入[]=“1234-5678”;
int结果,后缀;
sscanf(输入、%i-%i、&结果和后缀);
printf(“输出:'%i-%i.\n',结果,后缀);
返回0;
}
<>你应该使用正则表达式检查C++参考网站,例如.< /p> 如果您的输入被确认为类似于“xxxx xxxx”,其中“x”代表一个数字,您可以简单地使用以下功能:

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string input = "9349-2341";

    // This pattern matches any string begining with 4 digits and ending with 4 digits, both parts seperated by a slash
    string pattern = "([0-9]{4})-[0-9]{4}";
    smatch matcher;

    regex prog (pattern);

    if (regex_search(input, matcher, prog))
    {
        auto x = matcher[1];
        cout << x << " " << endl;
        input = matcher.suffix().str();
    }
    else
    {
        cout << "Invalid input!" << endl;
    }

    return 0;
}
或者,简单如下:

使用
sscanf
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
字符输入[]=“1234-5678”;
int结果,后缀;
sscanf(输入、%i-%i、&结果和后缀);
printf(“输出:'%i-%i.\n',结果,后缀);
返回0;
}
<>你应该看看C++参考网站,比如.< /P> //在
n1
n2


//你的随机数是在n1和n2之间生成的吗?你尝试了什么?这真的很简单,但是没有人应该为您编写代码。我还没有尝试过任何东西,因为我是初学者。我还知道一点点字符串操作,但这个问题足以让我明白outstackoverflow.com并不能替代它。您应该在C++书籍中找到大量使用字符串的例子,这些内容将有更多更详细和有用的信息,可以在StasOffFuff.com中简短地一两个段落回答。如果这是你的家庭作业,你有问题,你应该向你的老师寻求帮助。这就是他们的工作。我没有这方面的课程,我有一本书,里面有真正简单的例子,我只需要一个简单的方法来解决这个问题。仔细阅读string::find、string::substr和std::stoi。你试过什么?这真的很简单,但是没有人应该为您编写代码。我还没有尝试过任何东西,因为我是初学者。我还知道一点点字符串操作,但这个问题足以让我明白outstackoverflow.com并不能替代它。您应该在C++书籍中找到大量使用字符串的例子,这些内容将有更多更详细和有用的信息,可以在StasOffFuff.com中简短地一两个段落回答。如果这是你的家庭作业,你有问题,你应该向你的老师寻求帮助。这是他们的报酬。我没有得到这样的教程,我有一本简单的例子,我只需要一个简单的方法来解决这个问题。在String上::查找,String::SUBS:STD::St.I.我的代码与所有C++编译器绝对兼容,你怎么能说我的是C代码?在StAcExpLoad提供了很多他们想要的方式,这取决于这个问题的所有者决定应该选择哪一个答案是有用的。让我们通过使用命名空间STD添加<代码>,使它更为C++;代码>我的代码与所有C++编译器都是绝对兼容的,你怎么能说我的是C代码?在StAcExtPoad中,人们提供了很多他们想要的方式,这取决于这个问题的所有者决定应该选择哪个答案是有用的。让我们用C++使用名字空间STD添加<代码>;代码>
std::string str = "1234-5678";

std::string str1 = str.substr (0,4); 
std::string str2 = str.substr(5, 4);

int n1 = std::stoi(str1);
int n2 = std::stoi(str2);