2014-08-28 91 views
-2
 
.i 8 
.o 8 
.ilb a b c d e f g h 
.ob a b c d e f g h 
00000000 00000000 
00000001 00000011 
................ 
................ 
.e 

我試過這樣的代碼。沒有編譯錯誤使用C++解析數據

#include <string> 
#include <iostream> 
#include <sstream> 
#include <vector> 
#include <bitset> 
#include <fstream> 
#include <stdio.h> 
#include <stdlib.h> 
#include "Line12.hpp" 
#include "Line34.hpp" 
#include "Line261.hpp" 

using namespace std; 
void printLine12(Line12 line1) 
{ 
    cout << "first:" << line1.getFirst() << endl; 
    cout << "in:" << line1.getIn() << endl;  
} 

int main(int argc, const char * argv[]) 
{ 
    std::vector<Line12> lines12; 
    std::vector<Line34> lines34; 
    std::vector<std::vector<std::bitset<8> > > a; 
    std::vector<Line261> lines261; 

    std::ifstream in("C:/Users/Lenovo/Desktop/hwb8_64.pla"); 
    std::string Line; 

    for(int i=1; i<=261;i++) 
    { 
     std::getline(in,Line); 
     if(i==1 || i==2) 
     { 
      Line12 s(Line); 
      lines12.push_back(s); 
     } 
     else if(i==3 || i==4) 
     { 
      Line34 s1(Line); 
      lines34.push_back(s1); 
     } 
     else if(i==261) 
     { 
      Line261 s2(Line); 
      lines261.push_back(s2); 
     } 
     else 
     { 
      a.push_back(std::vector< std::bitset<8> >()); 
      std::istringstream iss(Line); 
      std::string bits; 
      while (iss >> bits) 
      { 
       a.back().push_back(std::bitset<8>(bits)); 
      } 

     } 

    } 
    system ("PAUSE"); 
    for(int i=1; i<=261;i++) 
    { 
     if(i==1 || i==2) 
     { 
      Line12 s1 = lines12.at(i); 
      printLine12(s1); 
     } 
     else if(i==3 || i==4) 
     { 
      Line34 s2 = lines34.at(i); 
      printLine34(s2); 
     } 
     else if(i==261) 
     { 
      Line261 s3 = lines261.at(i); 
      printLine261(s3); 
     }  
     else 
     { 
      for (int x = 0; x < a.size(); ++x) 
      { 
       for (int y = 0; y < a[i].size(); ++y) 
       { 
        for (int z = 7; z >= 0; --z) 
        { 
         std::cout << a[x][y][z]; 
        } 
        std::cout << " "; 
       } 
       std::cout << std::endl; 
      } 
     } 

    }  
    system ("PAUSE"); 
    return 0; 
} 

但是當我運行代碼。它表明是這樣的。我應該如何糾正這一點。

terminate called after throwing an instance of 'std::invalid_argument' 

    what<>: bitset::_M_copy from ptr 

我該怎麼辦,糾正這個錯誤,並得到輸出。

回答

1

構造函數bitset需要string的1和0。它會拋出一個invalid_argument異常,如果你傳遞了不是1和0的東西。你確實傳遞了一些不僅僅是零和現在的東西。

請在發佈之前自己做一些基本的調試來驗證您的程序是否正常工作。

+0

我能知道我在代碼中出錯了嗎?我沒有把字符串傳給bitset。 – user3898956 2014-08-28 06:39:09

+2

@ user3898956不,你沒有。還是你調試過嗎?你知道嗎?這個字符串中的字符是什麼?你的電腦說它不僅是1和0。我相信你的計算機比我相信你能夠以某種方式感知*可能發生的事情。學習使用調試器。正確使用調試器將解決此問題,並解決您未來的許多問題。 – nvoigt 2014-08-28 07:33:19