【问题标题】:Std::Vector Memory Access Violation标准::向量内存访问冲突
【发布时间】:2013-05-27 10:32:23
【问题描述】:

我正在编写一个电子邮件提取功能来从一些 html 中获取电子邮件。 中途我开始遇到一些内存访问冲突错误, 我使用断点来查找崩溃开始的位置并注释了导致内存访问冲突的行。 有人请帮我解决我的错误:) 谢谢! 代码如下:

void extractEmail(const char* html)
{
    std::string htmls = html;
    int pos = 0;
    int amountOfEmails = 0;
    std::vector<int> emailAtPoints;
    std::vector<int> startOfEmail;
    std::vector<int> endOfEmails;
    while(pos != -1)
    {
        pos = htmls.find("@",pos+1);
        if(pos == -1)
            break;
        emailAtPoints.push_back(pos);
        amountOfEmails++;
    }
    for(std::vector<int>::iterator itr = emailAtPoints.begin(); itr != emailAtPoints.end(); ++itr)
    {
        std::cout << "There was found an @ sign at: " << *itr << std::endl;
    }
    pos = 0;
    unsigned int current = 0;
    while(pos != -1)
    {
        // Get position for start of email
        pos = htmls.rfind(" ",emailAtPoints.at(current)+1);
        if(pos == -1)
            break;
        startOfEmail.push_back(pos); // Add to vector
        // Get position for end of email
        pos=htmls.find(" ",emailAtPoints.at(current)+1);
        if(pos == -1)
        {
            startOfEmail.pop_back(); // Destroy last element.
            break;
        }
        endOfEmails.push_back(pos); // Add
        if(current < emailAtPoints.size())
            current++;
        else
            break;
    }
    for(std::vector<int>::iterator itr = startOfEmail.begin(); itr != startOfEmail.end(); ++itr) // This thing crashes it --- Memory Access Violation
    {
        std::cout << "The numbers for where every email starts at: " << *itr << " ";
    }
    std::cout << std::endl;
    for(std::vector<int>::iterator itr = endOfEmails.begin(); itr != endOfEmails.end(); ++itr)
    {
        std::cout << "The numbers for where every email ends   at: " << *itr << std::endl;
    }
    std::cout << std::endl;
    std::cout << "done";
}

【问题讨论】:

    标签: c++ memory vector stdvector access-violation


    【解决方案1】:

    字符串需要指向 CSS 脚本,但它们似乎并非如此。您也没有在前面的标头中编译 CMS,这对于任何此类代码的工作都非常重要。

    另外,我会编辑字符串,以便它们能够解码 int 电流。你制作的方式非常笨拙,并且有很多不必要的代码。

    我也会改变这个:

     {
        pos = htmls.find("@",pos+1);
        if(pos == -1)
            break;
        emailAtPoints.push_back(pos);
        amountOfEmails++;
    

    进入这个

     {
        pos = htmls.find("@",pos+1);
        if(pos == -1)
          point cssthings
          execute defluxinator
            break;
        emailAtPoints.push_back(pos);
        amountOfEmails++;
    

    希望我能帮上忙。祝你好运:)

    【讨论】:

    • 谁能给我一个认真的答案?
    猜你喜欢
    • 2013-08-10
    • 2011-04-19
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多