come_on_haha 2024-03-11 14:58 采纳率: 33.3%
浏览 17
已结题

c++用正则表达式碰到的问题

我在写正则表达式的时候,写道验证的程序test的时候在没有报错的情况下没办法继续运行了,能帮忙看看吗?

#include <iostream>
#include <vector> 
#include <stack> 
#include <variant>
#include <regex>
#include <string>
using namespace std;
struct Net
{
    string name = "";
    int element = 0;
};

struct Position
{
    int x = 0;
    int y = 0;
};


class Bus 
{
public:
    /*
    void displaystart()
    {
        cout << "x:" << start.x << "  " << "y:" << start.y << endl;
    }
    void displayend()
    {
        cout << "x:" << end.x << " " << "y:" << end.y << endl;
    }
    static void Printdata(? , int indentLevel)
    {

    }
    */
    Bus(string name, Position start, Position end, const vector<Net>& nets, int width);
    //这里只需要一个bus的数据,剩下重复内容用向量
    //有个问题,应该把向量nets作为类的输入
    ~Bus();
    //封装后的访问函数:

    //不封装了
    string current_name;
    Position current_start;
    Position current_end;
    vector<Net> current_nets;
    int current_width;



private:
    
};

//构造函数
Bus::Bus(string name, Position start, Position end, const vector<Net>& nets, int width) : current_name(name),current_start(start),current_end(end),current_nets(nets),current_width(width) {}
//析构函数
Bus::~Bus()
{

}


void Search_catch_Bus(string str,vector<Bus>& Bus_List_)
{
    
    smatch result;
    regex reg("[\\(\\)]");
    sregex_token_iterator pos1(str.begin(), str.end(), reg, -1);
    sregex_token_iterator end1;

    regex _Bus("Bus[0-9]+");
    regex _start("start");
    regex _end("end");
    regex _x("x");
    regex _y("y");
    regex _width("width");
    regex _nets("nets");
    regex _nets1("nets[0-9]+");
    regex _num("[0-9]+");
    



    string Bus_Name = "";
    bool Position1 = 0;
    bool Position2 = 0;




    while (pos1 != end1)
    {
        Position start_;
        Position end_;
        string Bus_Name_="";
        int width_ = 0;
        vector<Net> nets_;
        //构造函数没有默认的形式

        smatch result;
        
        if (Bus_Name != "Bus0" && regex_match(pos1->str(), _Bus))
        {
            Bus Bus_(Bus_Name, start_, end_, nets_, width_);

            Bus_List_.push_back(Bus_);
        }





        if (regex_match(pos1->str(), _Bus))
        {
            Bus_Name = pos1->str();
        }
        if (regex_match(pos1->str(), _start))
        {
            Position1 = 1;
            Position2 = 0;
        }
        if (regex_match(pos1->str(), _end))
        {
            Position1 = 0;
            Position2 = 1;
        }
        //以上写入了x和y
        if (regex_match(pos1->str(), _x))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            if (Position1)
            {
                start_.x = stoi(pos1->str());
            }
            else
            {
                end_.x = stoi(pos1->str());
            }


        }
        
        if (regex_match(pos1->str(), _y))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            if (Position1)
            {
                start_.y = stoi(pos1->str());
            }
            else
            {
                end_.y = stoi(pos1->str());
            }


        }
        
        if (regex_match(pos1->str(), _width))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            width_ = stoi(pos1->str());
        }
    

        //vector有element和name
        Net current_net;
        if (regex_match(pos1->str(), _nets))
        {
            nets_.clear();
        }

        if (regex_match(pos1->str(), _nets1))
        {
            current_net.name = pos1->str();

            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            current_net.element = stoi(pos1->str());
            nets_.push_back(current_net);
        }
        

        //现在已经载入所有数据
        //把他们一起放到一个容器中
        
        



        //完成!

        pos1++;

    }


    


}




class Test
{
public:
    static void Get_Bus(const vector<Bus>& vec)
    {
        string choice1;
        cout << "请输入想要查找总线(Bus?):" << endl;
        cin >> choice1;
        regex reg("\\d+");
        smatch result;
        if (regex_search(choice1, result, reg))
        {//stoi(result.str())=数字
            string choice2;
            int answer = stoi(result.str());
            cout << "start:" << "(" << vec[answer].current_start.x << "," << vec[answer].current_start.y << ")" << endl;
            cout << "end:" << "(" << vec[answer].current_end.x << "," << vec[answer].current_end.y << ")" << endl;
            cout << "width:" << vec[answer].current_width << endl;
            cout << "请输入想要查找的线(net?):" << endl;
            cin >> choice2;
            smatch result2;
            if (regex_search(choice2, result2, reg))
            {
                int answer2 = stoi(result2.str());
                cout << "数值为:" << vec[answer].current_nets[answer2].element << endl;
            }
        }

    }
};



int main()
{
    /*
    string expression = "(Bus (Bus0 (start (x 15) (y 13)) (end (x 63) (y 24)) (width 134) (nets (net0 12) (net1 46))) (Bus1 (start (x 19) (y 13)) (end (x 63) (y 24)) (width 134) (nets (net0 35))))";
    //如果每个需要的元素能做出一个结构体呢?
    
    //之后实现在bus0的容器下的nets!!
    vector<Net> My_Nets;
    My_Nets.push_back({ "net0",16 });
    My_Nets.push_back({ "net1",16 });
    My_Nets.push_back({ "net2",13 });
    //注意:读取统一格式为:const auto&
    
    cout << My_Nets[0].name << "," << My_Nets[0].element << endl;
    //数据读取方面已经可以用了
    //  1.接下来学习类中的构造,析构等函数的规范
    vector<Bus> Bus_List;
    Position My_Start;
    string My_Name = "Bus0";
    My_Start.x = 15;
    My_Start.y = 16;
    Position My_End;
    My_End.x = 18;
    My_End.y = 29;
    int My_Width = 16;
    Bus My_Bus( My_Name,My_Start, My_End, My_Nets, My_Width);
    //接下来是验证:
    //还是选用封装好的函数吧
    Bus_List.push_back(My_Bus);

    

    Position start = Bus_List[0].getStart();
    cout << start.x << endl;

    vector<Net> check_nets = Bus_List[0].getNets();
    cout << check_nets[0].name << "," << check_nets[0].element << endl;

    //解决!!

    //现在来让我们看看怎么读取字符串?
    

    */


    string get = "(Bus(  Bus0(start(x   15)(y  13))(end(x 63)(y 24))(width 134)(nets (net0 12)(net1 46) ) ) (  Bus1(start(x   19)(y  13))(end(x 63)(y 24))(width 134)(nets (net0 35))  ) )";
    
    vector <Bus> Bus_List;
    Search_catch_Bus(get,Bus_List);
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/49252937b549487ba542a12b6bd9a52d.png "#left")

    //这一步先省略了4个不同字符串分开的操作
    //接下来看怎么填入数据
    Test::Get_Bus(Bus_List);



    cout << "\n" << endl;
    system("pause");
    return 0;
}

错误是这样的:

img

img

img

展开全部

  • 写回答

19条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-11 14:58
    关注

    引自免费微信小程序:皆我百晓生

    在您的代码中,正则表达式被用于解析字符串 get 中的总线信息。根据您提供的错误信息,程序在访问向量 Bus_List 时遇到了“vector subscript out of range”的异常,这意味着当试图访问向量中不存在的索引时抛出了异常。

    问题出在函数 Search_catch_Bus 中,您创建了一个新的 Bus 对象并将其添加到 Bus_List 向量中,但在这个过程中存在逻辑错误。当检测到一个新总线(即匹配 _Bus 正则表达式)时,您没有正确地初始化 start_end_nets_ 的值,然后就直接创建了 Bus 实例,并将它添加到了列表中。

    修复这个问题的方法是确保在找到新总线名称之后,先正确收集与该总线相关的所有信息,然后再构造 Bus 对象并将其加入 Bus_List。以下是修改后的部分代码:

    while (pos1 != end1)
    {
        // 初始化每个循环开始时的相关变量
        Position start_{};
        Position end_{};
        string Bus_Name = "";
        int width_ = 0;
        vector<Net> nets_{};
    
        // 遍历整个输入字符串
        while (pos1 != end1)
        {
            if (regex_match(pos1->str(), _Bus))
            {
                Bus_Name = pos1->str();
                // 当发现新的总线时,清空之前的数据,重新开始收集
                start_ = Position{};
                end_ = Position{};
                nets_.clear();
            }
            else if (regex_match(pos1->str(), _start))
            {
                Position1 = 1;
                Position2 = 0;
            }
            // ... 其他位置和宽度等信息的处理
    
            // x, y 和 width 处理保持不变
    
            // 收集 Nets 相关信息
            if (regex_match(pos1->str(), _nets1))
            {
                current_net.name = pos1->str();
                pos1++;
                current_net.element = stoi(pos1->str());
                nets_.push_back(current_net);
            }
    
            // 当完成一个完整 Bus 的信息收集后,构建 Bus 对象并添加到列表
            if (pos1 == end1 || !regex_match(pos1->str(), _Bus))
            {
                if (!Bus_Name.empty())
                {
                    Bus Bus_(Bus_Name, start_, end_, nets_, width_);
                    Bus_List_.push_back(Bus_);
                    Bus_Name.clear(); // 清除已处理完的总线名
                }
                break; // 如果遍历结束或遇到下一个总线,跳出内层循环
            }
    
            pos1++;
        }
    }
    

    这样,在收集完一个完整的总线信息后才构造并添加 Bus 对象到向量中,可以避免向量越界的问题。同时,请注意对特殊情况的处理,如在遍历结束前最后一个总线的情况。

    展开全部

    评论 编辑记录
    专家-赤兔[在线] 2024-03-11 14:59

    如还有疑问请回复沟通。如已解决请采纳,感谢!

    回复
  • 码农阿豪@新空间 Java领域优质创作者 2024-03-11 15:01
    关注
    获得0.45元问题酬金
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    从代码中看起来有几个问题可能存在导致程序无法继续运行:
    1. 可能出现了死循环,导致程序无法向下执行。需要检查循环语句的条件是否合理,是否有循环变量没有更新等情况。
    2. 可能出现了异常情况,导致程序直接停止运行。需要检查代码中是否有未处理的异常,是否有越界或空指针等问题。
    3. 正则表达式的匹配可能出现问题。需要检查正则表达式的模式是否正确,以及是否能够匹配到需要的字符串。 以下是一个可能的解决方案: 在代码中加入try-catch语句,捕获异常并进行处理:
    void Search_catch_Bus(string str, vector<Bus>& Bus_List_){
        try{
            smatch result;
            regex reg("\\(\\)");
            sregex_token_iterator pos1(str.begin(), str.end(), reg, -1);
            sregex_token_iterator end1;
            regex _Bus("Bus[0-9]+");
            regex _start("start");
            regex _end("end");
            regex _x("x");
            regex _y("y");
            regex _width("width");
            regex _nets("nets");
            regex _nets1("nets[0-9]+");
            regex _num("[0-9]+");
            string Bus_Name = "";
            bool Position1 = false;
            bool Position2 = false;
            while (pos1 != end1)
            {
                Position start_;
                Position end_;
                string Bus_Name_="";
                int width_ = 0;
                vector<Net> nets_;
                if (Bus_Name != "Bus0" && regex_match(pos1->str(), result, _Bus))
                {
                    Bus Bus_(Bus_Name, start_, end_, nets_, width_);
                    Bus_List_.push_back(Bus_);
                }
                if (regex_match(pos1->str(), result, _Bus))
                {
                    Bus_Name = pos1->str();
                }
                if (regex_match(pos1->str(), result, _start))
                {
                    Position1 = true;
                    Position2 = false;
                }
                if (regex_match(pos1->str(), result, _end))
                {
                    Position1 = false;
                    Position2 = true;
                }
                if (regex_match(pos1->str(), result, _x))
                {
                    while (!regex_match(pos1->str(), _num))
                    {
                        pos1++;
                    }
                    if (Position1)
                    {
                        start_.x = stoi(pos1->str());
                    }
                    else
                    {
                        end_.x = stoi(pos1->str());
                    }
                }
                // ...其他代码部分省略
            }
        } catch(exception e){
            cout<<"出现异常:"<<e.what()<<endl;
        }
    }
    

    另外,需要检查正则表达式的模式是否正确,以及是否可以匹配到需要的字符串。可以写一个简单的测试函数,检查正则表达式的正确性:

    void regex_test(){
        string str = "Bus1(start(x: 2, y: 3))";
        regex _Bus("Bus[0-9]+");
        regex _start("start");
        smatch result;
        if(regex_match(str, result, _Bus)){
            cout<<"匹配到Bus:"<<result[0]<<endl;
        }
        if(regex_match(str, result, _start)){
            cout<<"匹配到start:"<<result[0]<<endl;
        }
    }
    

    执行该函数可以检查正则表达式是否正确。如果执行结果不符合预期,则需要重新检查正则表达式的模式和代码实现。

    展开全部

    评论
  • 阿里嘎多学长 2024-03-11 15:17
    关注
    获得0.30元问题酬金

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:

    你的代码在尝试访问Bus对象的成员变量时出现了问题,因为你在创建Bus对象时没有给它们提供正确的参数。在Search_catch_Bus函数中,你创建Bus对象时传入了空的Position对象和空的vector<Net>,但实际的构造函数需要具体的值。

    你需要在找到匹配的字符串后,将解析到的值传递给Bus的构造函数。下面是修改后的Search_catch_Bus函数的一部分,你可以参考这个思路来修复你的代码:

    while (pos1 != end1)
    {
        // ... (其他代码不变)
    
        if (regex_match(pos1->str(), _Bus))
        {
            Bus_Name = pos1->str();
        }
        // ... (其他代码不变)
    
        if (regex_match(pos1->str(), _start))
        {
            Position1 = 1;
            Position2 = 0;
        }
        if (regex_match(pos1->str(), _end))
        {
            Position1 = 0;
            Position2 = 1;
        }
        // ... (其他代码不变)
    
        if (regex_match(pos1->str(), _x))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            if (Position1)
            {
                start_.x = stoi(pos1->str());
            }
            else
            {
                end_.x = stoi(pos1->str());
            }
        }
        // ... (其他代码不变)
    
        if (regex_match(pos1->str(), _y))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            if (Position1)
            {
                start_.y = stoi(pos1->str());
            }
            else
            {
                end_.y = stoi(pos1->str());
            }
        }
        // ... (其他代码不变)
    
        if (regex_match(pos1->str(), _width))
        {
            while (!regex_match(pos1->str(), _num))
            {
                pos1++;
            }
            width_ = stoi(pos1->str());
        }
    
        // ... (其他代码不变)
    
        // 当找到nets后,解析并填充nets_
        if (regex_match(pos1->str(), _nets))
        {
            while (pos1 != end1)
            {
                if (regex_match(pos1->str(), _nets1))
                {
                    current_net.name = pos1->str();
    
                    while (!regex_match(pos1->str(), _num))
                    {
                        pos1++;
                    }
                    current_net.element = stoi(pos1->str());
                    nets_.push_back(current_net);
                }
                else if (regex_match(pos1->str(), _Bus)) // 如果遇到下一个Bus,跳出
                {
                    break;
                }
                pos1++;
            }
        }
    
        // 创建Bus对象并添加到列表
        if (!Bus_Name.empty() && !nets_.empty())
        {
            Bus Bus_(Bus_Name, start_, end_, nets_, width_);
            Bus_List_.push_back(Bus_);
            Bus_Name.clear();
            nets_.clear();
        }
    }
    

    这段代码首先收集必要的信息(Bus名、位置、宽度和nets),然后在找到完整的Bus信息后,创建Bus对象并将其添加到Bus_List_。注意,这里假设输入的字符串格式是正确的,并且Bus总是包含startendwidth和至少一个net。如果输入格式可能不一致,你需要添加更多的错误检查和处理。

    展开全部

    评论
  • GISer Liu 2024-03-11 15:23
    关注
    获得0.60元问题酬金

    该回答引用自GPT-4,由博主GISer Liu编写:

    根据您提供的代码和描述,问题可能出现在正则表达式的处理上,导致程序无法正确运行。让我们来分析并解决这个问题。
    首先,让我们分析代码中的正则表达式部分。在 Search_catch_Bus
    函数中,您使用了多个正则表达式来匹配不同的模式,但是在处理字符串时可能存在一些逻辑问题,导致无法正确提取所需的数据。
    在分析代码之前,我建议您先通过添加一些调试输出语句来检查代码的执行情况,以便更好地理解程序的运行流程。
    接下来,让我们逐步检查代码中的正则表达式匹配逻辑,并尝试修改以确保正确提取所需的数据。具体来说,您可以:

    1. 确保正则表达式能够正确匹配您期望的模式。
    2. 在匹配到相应模式后,确保正确提取数据并将其存储到对应的变量中。
    3. 确保在循环中正确处理不同的模式,并将提取到的数据组合成 Bus 对象并添加到 Bus_List_ 中。
      另外,建议您在处理字符串时,先将其进行适当的格式化或分割,以便更容易地提取所需的信息。
      针对代码中的正则表达式匹配逻辑,您可能需要逐步调试和修改代码,确保每一步都能正确提取所需的数据。
      另外,您提到程序运行后无法继续执行,这可能是由于程序陷入了无限循环或者其他逻辑错误导致的。建议您在代码中添加适当的输出语句来跟踪程序的执行流程,以便更好地定位问题所在。
      总的来说,您需要仔细检查代码中的正则表达式匹配逻辑,并确保正确提取所需的数据。如果您遇到了具体的报错信息或者程序无法继续执行的情况,请提供相关信息以便更好地帮助您解决问题。

    如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订

    用户答题指南

    评论
  • threenewbee 2024-03-11 15:53
    关注
    获得0.60元问题酬金

    这个最好远程帮你调试下,你要方便远程,给我留言下

    评论
    come_on_haha 2024-03-12 08:34

    在吗,请问可以帮忙一下吗?

    回复
    come_on_haha 2024-03-12 10:14

    其他人的办法都行不通,希望能建议一下,可以向日葵远程

    回复
  • bug菌¹ Java领域优质创作者 2024-03-11 16:24
    关注
    获得0.60元问题酬金

    该回答引用ChatGPT,望对题主有所帮助/启发;若有帮助,还望采纳🤝🤝🤝


    您的代码中存在一些问题,导致程序无法正常运行。以下是一些可能的修复方法:

    1. 缺少头文件:您需要包含 <regex> 头文件以使用正则表达式。

    2. 不正确的正则表达式:您在创建 regex 对象时使用了 [\\(\\)] 的正则表达式,这将匹配括号。然而,您的字符串中并没有括号,因此该表达式无法正确匹配任何内容。根据您的代码逻辑,您可能需要调整正则表达式以匹配其他内容。

    3. stoi 转换错误:在使用 stoi 将字符串转换为整数时,请确保字符串中只包含数字字符。否则,将引发异常。您可以在转换之前使用 regex_match 验证字符串是否只包含数字。

    4. 错误的索引访问:在 Get_Bus 函数中,当您尝试访问 vec 向量中的元素时,应该首先检查索引是否越界。您可以使用 vec.size() 函数来获取向量的大小,并在访问前进行检查。

    请尝试修复上述问题,如果还有其他问题,请提供更具体的错误信息以便更好地帮助您解决问题。

    评论
  • Java毕设王 2024-03-11 17:11
    关注
    获得0.60元问题酬金

    引用讯飞星火
    根据您提供的代码和错误信息,问题出在Bus类的构造函数中。在创建Bus对象时,您没有正确地初始化start_end_nets_成员变量。为了解决这个问题,您需要修改Search_catch_Bus函数中的相关代码。

    首先,将以下代码:

    Position start_;
    Position end_;
    string Bus_Name_="";
    int width_ = 0;
    vector<Net> nets_;
    

    替换为:

    Position start_ = {0, 0};
    Position end_ = {0, 0};
    string Bus_Name_ = "";
    int width_ = 0;
    vector<Net> nets_;
    

    然后,将以下代码:

    if (regex_match(pos1->str(), _Bus))
    {
        Bus Bus_(Bus_Name, start_, end_, nets_, width_);
    
        Bus_List.push_back(Bus_);
    }
    

    替换为:

    if (regex_match(pos1->str(), _Bus))
    {
        Bus_Name = pos1->str();
    }
    else if (Bus_Name != "")
    {
        Bus Bus_(Bus_Name, start_, end_, nets_, width_);
    
        Bus_List.push_back(Bus_);
    }
    

    这样,当遇到一个新的Bus名称时,会将其存储在Bus_Name变量中。然后在下一个Bus名称出现之前,使用当前的名称和其他成员变量创建一个Bus对象,并将其添加到Bus_List中。

    展开全部

    评论
  • 百锦再@新空间代码工作室 全栈领域优质创作者 2024-03-12 01:15
    关注
    获得0.45元问题酬金

    根据您提供的信息,我无法直接运行您的代码来进行调试。但是根据您描述的情况,可能有以下几个问题导致验证程序test无法继续运行:

    1. 在Test类中,有可能因为输入的原因导致选择choice1或choice2的正则表达式匹配失败,从而无法继续执行后续代码。您可以确认一下输入是否符合预期的格式,或者添加一些输入验证的逻辑。

    2. 另一个可能的问题是在处理start和end位置以及nets的正则表达式匹配过程中可能出现了问题,导致无法正确获取这些数据进而影响后续代码的运行。您可以检查一下这部分代码的逻辑是否正确。

    3. 另外,在处理完nets中的数据后,您可能需要对current_net重新赋值,或者确认当前的数据是正确的。否则在获取当前nets的element值时可能会出现问题。

    您可以尝试对上述问题进行检查和调试,看看是否可以解决您的验证程序无法继续运行的问题。希望对您有所帮助。如果您有其他问题或需要进一步的指导,请随时告诉我。


    有问题你别着急,评论留言都可以,看到马上就回复,尽量及时补充齐
    评论
  • 关注
    获得0.60元问题酬金


    本答案参考ChatGPT-3.5

    从你提供的代码来看,有以下几点问题:

    1. 在函数Search_catch_Bus中,你使用了迭代器sregex_token_iterator来分割字符串,但是在使用时没有检查迭代器是否有效,可能会导致访问无效迭代器而出现问题。建议在使用迭代器之前,添加条件判断语句检查迭代器是否有效。

    2. 在类Bus的构造函数中,你将参数nets作为输入,但是在创建Bus对象时并没有传入参数。你可能需要修改Search_catch_Bus函数的逻辑,确保nets_在创建Bus对象时传入。

    下面是对上述问题的解决方案:

    1. 修改Search_catch_Bus函数中迭代器的使用,添加条件判断语句,确保迭代器有效:
    sregex_token_iterator pos1(str.begin(), str.end(), reg, -1);
    sregex_token_iterator end1;
    
    while (pos1 != end1)
    {
        // 判断迭代器是否有效
        if (pos1->length() > 0)
        {
            // 迭代器有效,进行相应处理
            // ...
        }
    
        pos1++;
    }
    
    1. 修改Search_catch_Bus函数的逻辑,确保nets_在创建Bus对象时传入。可以将nets_作为局部变量在函数内部保存,并在创建Bus对象时传入。代码示例如下:
    void Search_catch_Bus(string str, vector<Bus>& Bus_List_)
    {
        // ...
    
        vector<Net> nets_;
        // 其他逻辑处理
    
        while (pos1 != end1)
        {
            // ...
    
            if (Bus_Name != "Bus0" && regex_match(pos1->str(), _Bus))
            {
                Bus Bus_(Bus_Name, start_, end_, nets_, width_);
    
                Bus_List_.push_back(Bus_);
            }
    
            // ...
    
            if (regex_match(pos1->str(), _nets))
            {
                nets_.clear();
            }
    
            // ...
    
            if (regex_match(pos1->str(), _nets1))
            {
                current_net.name = pos1->str();
                // ...
            }
    
            // ...
    
            pos1++;
        }
    
        // ...
    }
    

    以上是对你提供的代码问题的解决方案,希望可以帮助到你。如果还有其他问题,请详细描述,我将尽力帮助你解决。

    展开全部

    评论
  • yy64ll826 2024-03-12 08:06
    关注
    获得0.15元问题酬金

    你的代码在定义和构造Bus类时遇到了一些问题,并且你的Search_catch_Bus函数似乎还没有完成。首先,我将帮助你修复Bus类的定义和构造,然后我们可以讨论如何完成Search_catch_Bus函数。
    引用人工智能的回答

    让我们首先修复Bus类的定义,然后讨论如何继续编写Search_catch_Bus函数。
    
    cpp
    #include <iostream>
    #include <vector>
    #include <regex>
    #include <string>
    
    struct Net {
        std::string name = "";
        int element = 0;
    };
    
    struct Position {
        int x = 0;
        int y = 0;
    };
    
    class Bus {
    public:
        Bus(std::string name, Position start, Position end, const std::vector<Net>& nets, int width);
        &#126;Bus();
    
        // 访问器函数(如果需要)
        std::string get_name() const { return current_name; }
        Position get_start() const { return current_start; }
        Position get_end() const { return current_end; }
        const std::vector<Net>& get_nets() const { return current_nets; }
        int get_width() const { return current_width; }
    
    private:
        std::string current_name;
        Position current_start;
        Position current_end;
        std::vector<Net> current_nets;
        int current_width;
    };
    
    // 构造函数
    Bus::Bus(std::string name, Position start, Position end, const std::vector<Net>& nets, int width)
        : current_name(name), current_start(start), current_end(end), current_nets(nets), current_width(width) {}
    
    // 析构函数
    Bus::&#126;Bus() {}
    现在,让我们讨论如何继续编写Search_catch_Bus函数。这个函数的目的是解析一个字符串,提取有关总线的信息,并使用这些信息创建一个Bus对象。你可能需要使用正则表达式来匹配和提取字符串中的不同部分,然后使用这些信息来构造Bus对象。
    
    cpp
    void Search_catch_Bus(const std::string& str, std::vector<Bus>& bus_list) {
        std::smatch result;
        std::regex reg_bus("Bus[0-9]+");
        std::sregex_token_iterator pos1(str.begin(), str.end(), reg_bus, -1);
        std::sregex_token_iterator end1;
    
        while (pos1 != end1) {
            std::string bus_name = pos1->str();
            // 你需要继续编写代码来解析字符串,提取位置、网络和其他信息
            // 然后使用这些信息来创建一个Bus对象,并将其添加到bus_list中
            ++pos1;
        }
    }
    请注意,上述代码只是一个框架,你需要继续编写代码来解析字符串并提取所需的信息。这可能需要编写更多的正则表达式,并可能需要处理字符串中的不同部分以提取位置、网络和其他信息。一旦你提取了这些信息,你就可以使用它们来创建一个Bus对象,并将其添加到bus_list中。
    
    

    展开全部

    评论
  • 喵手 2024-03-12 14:09
    关注
    获得0.30元问题酬金

    该回答引用ChatGPT辅助答疑,若有帮助,还请题主采纳。


    根据您提供的错误信息,我注意到您的代码中有几个地方可能导致程序无法继续运行:

    1. 缺少头文件 <regex>:请确保您的代码中包含了正确的头文件,即 #include <regex>

    2. 未定义的函数 Search_catch_Bus:您在 main 函数中调用了 Search_catch_Bus 函数,但是在代码中没有定义该函数。请确认您是否遗漏了该函数的定义。

    3. 未定义的函数 Get_Bus:您在 main 函数中调用了 Test::Get_Bus 函数,但是在代码中没有定义该函数。请确认您是否遗漏了该函数的定义。

    请尝试修复这些问题并重新运行代码,如果仍然遇到问题,请提供具体的错误信息以便我更进一步地帮助您解决问题。

    评论
  • CSDN-Ada助手 CSDN-AI 官方账号 2024-03-12 19:00
    关注
    获得0.60元问题酬金

    【相关推荐】




    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论
  • 杨同学* 2024-03-18 11:41
    关注
    获得0.45元问题酬金

    在您提供的代码中,有一处可能导致程序无法继续运行的问题是正则表达式匹配错误。在Search_catch_Bus函数中,您使用了regex_match来匹配字符串,但应该使用regex_search来搜索字符串。regex_match会尝试从字符串的开头开始匹配整个字符串,而regex_search会在整个字符串中查找匹配项。

    请尝试将regex_match改为regex_search,如下所示:

    if (Bus_Name != "Bus0" && regex_search(pos1->str(), _Bus))
    

    另外,您还可以检查其他正则表达式匹配部分,确保使用regex_search来查找整个字符串的匹配项。如果仍然存在问题,建议添加一些调试输出来跟踪程序执行过程,以便更好地定位问题所在。

    希望这些修改能够帮助您解决程序无法继续运行的问题。如果您需要进一步帮助,请随时告诉我。

    评论
  • GIS工具开发 2024-03-18 13:13
    关注
    获得0.30元问题酬金

    确保在找到新总线名称之后,先正确收集与该总线相关的所有信息,然后再构造 Bus 对象并将其加入 Bus_List

    评论
  • 会跑的小鹿 2024-03-18 13:56
    关注
    获得0.30元问题酬金

    没有正确地初始化 start_、end_ 和 nets_ 的值

    评论
  • Minuw 2024-03-18 14:49
    关注
    获得0.45元问题酬金

    参考gpt
    在你的代码中,我注意到了一些问题可能导致程序无法继续运行:

    1. Search_catch_Bus 函数中,你定义了一个 smatch result 变量两次,这可能会导致混淆。建议删除其中一个定义,只保留一个即可。

    2. Search_catch_Bus 函数中,你使用了 while (!regex_match(pos1->str(), _num)) 来查找数字,但这可能导致无限循环,因为在某些情况下可能找不到匹配的数字。建议在循环之前先检查 pos1 != end1,以确保不会无限循环。

    3. Search_catch_Bus 函数中,你在 Bus 结构体的构造函数中使用了未初始化的 start_end_nets_width_,这可能导致未定义的行为。建议在构造函数之前先初始化这些变量。

    4. Test::Get_Bus 函数中,你使用了 regex_search,但没有检查是否找到了匹配项。建议在使用匹配结果之前先检查是否找到了匹配项。

    这些可能是导致程序无法继续运行的原因。你可以尝试解决这些问题,然后再次运行程序看看是否能够继续执行。如果问题仍然存在,可以提供更多信息或错误消息以便进一步帮助。

    评论
  • CrMylive. Python领域新星创作者 2024-03-11 16:59
    关注

    结合GPT给出回答如下请题主参考
    当使用正则表达式时,有几个常见的问题可能会导致程序无法继续运行。

    1. 语法错误:正则表达式具有特定的语法规则,如果在编写表达式时违反了这些规则,程序将无法通过编译或报告错误。请确保您的正则表达式语法正确。

    2. 死循环:如果正则表达式模式造成死循环,程序将一直运行而无法继续下去。死循环可能发生在使用复杂的模式或具有多个匹配条件的情况下,请确保您的模式设计合理。

    3. 匹配超时:在处理大量数据或复杂的模式时,正则表达式可能需要很长时间才能完成匹配操作。如果您的程序处于无限等待状态,请尝试使用更简单的模式或者加入超时处理机制。

    4. 内存溢出:正则表达式引擎在处理大规模数据时可能导致内存溢出,尤其是当模式中使用了大量重复或递归的元素时。请确保您的模式在性能和资源方面是可行的。

    根据您提供的信息,很难准确判断是哪个问题导致程序无法继续运行。您可以尝试将您的正则表达式模式和测试代码提供给我,我将尽力帮助您找出问题所在。

    评论
  • 无聊937 2024-03-12 01:27
    关注

    该回答引用Gemini: 你的程序在没有报错的情况下没办法继续运行,可能是因为你的正则表达式写错了。你可以尝试使用在线正则表达式测试工具来检查你的正则表达式是否有误。

    你也可以尝试使用其他正则表达式引擎来测试你的正则表达式。不同的正则表达式引擎可能对正则表达式的语法有不同的解释,所以使用其他正则表达式引擎来测试你的正则表达式是否有误也是一个好主意。

    评论
  • 小明爱吃火锅 2024-03-12 03:45
    关注

    引用文心一言及思考回答:

    while (pos1 != end1)
    {
        // 初始化每个循环开始时的相关变量
        Position start_{};
        Position end_{};
        string Bus_Name = "";
        int width_ = 0;
        vector<Net> nets_{};
     
        // 遍历整个输入字符串
        while (pos1 != end1)
        {
            if (regex_match(pos1->str(), _Bus))
            {
                Bus_Name = pos1->str();
                // 当发现新的总线时,清空之前的数据,重新开始收集
                start_ = Position{};
                end_ = Position{};
                nets_.clear();
            }
            else if (regex_match(pos1->str(), _start))
            {
                Position1 = 1;
                Position2 = 0;
            }
            // ... 其他位置和宽度等信息的处理
     
            // x, y 和 width 处理保持不变
     
            // 收集 Nets 相关信息
            if (regex_match(pos1->str(), _nets1))
            {
                current_net.name = pos1->str();
                pos1++;
                current_net.element = stoi(pos1->str());
                nets_.push_back(current_net);
            }
     
            // 当完成一个完整 Bus 的信息收集后,构建 Bus 对象并添加到列表
            if (pos1 == end1 || !regex_match(pos1->str(), _Bus))
            {
                if (!Bus_Name.empty())
                {
                    Bus Bus_(Bus_Name, start_, end_, nets_, width_);
                    Bus_List_.push_back(Bus_);
                    Bus_Name.clear(); // 清除已处理完的总线名
                }
                break; // 如果遍历结束或遇到下一个总线,跳出内层循环
            }
     
            pos1++;
        }
    }
    
    

    展开全部

    评论
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 3月18日
  • 创建了问题 3月11日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部