2016-08-24 1 views
-2

코드 블록과 cygwin에서 모두 컴파일되었지만 실행할 때 충돌이 발생합니다.
source.txt 파일은 다음과 같은 형식의된다왜 fasta formated DNA를 취하여 실행시 벡터로 변환하는 코드가 발생합니까?

>에는 sample1
ACTG
GCA
에게 GTC
> sample2를
TAACG
GGCC
그리고 DTB를 같은 것을 보일 것입니다 :
DTB를 = (샘플 1, ACTGGCAGTC, 샘플 2, TAACGGGCC)

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <string> 
using namespace std; 
int main() 
{ 
    ifstream myfile; 
    int i=0; 
    string seq="",holder=""; 
    myfile.open("source.txt"); 
    vector<string> dtb; 
    while (myfile>> seq) 
    { 
     if (seq.substr(0,1)==">") 
     { 
      dtb[i]=seq.substr(1,seq.length()-1); 
      i++; 
      if (i!=0) 
       dtb[i]=holder; 
      holder=""; 
     } 
     else 
     { 
      holder+=seq; 
     } 
    } 
    cout<<dtb[0]<<"\n"<<dtb[1]<<"\n"<<dtb[2]<<"\n"<<dtb[3]; 
    return 0; 
} 
+0

이러한 문제를 해결하는 올바른 도구는 디버거입니다. 스택 오버플로를 묻기 전에 코드를 단계별로 실행해야합니다. 자세한 도움말은 [작은 프로그램 디버깅 방법 (Eric Lippert 작성)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)을 참조하십시오. 문제를 재현하는 [최소, 완료 및 확인 가능] (http://stackoverflow.com/help/mcve) 예제와 함께 해당 질문을 \ [편집]해야합니다. 디버거. –

답변

2

std::vector 개체가 비어 있습니다. 즉, 인덱스가 잘못되면 정의되지 않은 동작이 발생합니다.

벡터를 사용하여 요소를 벡터에 추가해야합니다. push_back.