2013-05-27 117 views
-1

我正在寫一個電子郵件提取函數來從某些html中獲取電子郵件。 中途通過我開始獲取一些內存訪問衝突錯誤, 我使用斷點來查找崩潰開始的位置並評論導致內存訪問衝突的行。 有人請幫我解決我的錯誤:)謝謝! 下面是代碼:Std ::向量存儲器訪問衝突

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"; 
} 

回答

0

琴絃需要指出的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++; 

希望我幫助。祝你好運:)

+0

任何人都可以請給我一個認真的答案? –