为什么我采用 fasta 格式 DNA 并将其转换为矢量的代码在 运行 上崩溃
why does my code which takes fasta formated DNA and transforms it into a vector crashes on run
我在代码块和 cygwin 中都进行了编译,但是当我 运行 它时它崩溃了。
source.txt 文件的格式如下:
>样本 1
ACTG
GCA
GTC
>样本 2
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;
}
A std::vector
对象一开始是空的。这意味着其中的任何索引都将越界并导致未定义的行为。
您需要向矢量添加 个元素,例如使用push_back
.
我在代码块和 cygwin 中都进行了编译,但是当我 运行 它时它崩溃了。
source.txt 文件的格式如下:
>样本 1
ACTG
GCA
GTC
>样本 2
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;
}
A std::vector
对象一开始是空的。这意味着其中的任何索引都将越界并导致未定义的行为。
您需要向矢量添加 个元素,例如使用push_back
.