简体   繁体   English

试图打印向量的内容

[英]Trying to print contents of vector

I've been trying to get more and more comfortable with C++ and I've moved to trying to write some file manip stuff. 我一直在尝试使C ++变得越来越舒适,而我开始尝试编写一些文件操作手册。 I was halfway through something that would be able to parse fasta files and I got a little stuck: 我正在解决能够解析fasta文件的问题,我遇到了一些问题:

#include<fstream>
#include<iostream>
#include<string>
#include<vector>

using namespace std;

//A function for reading in DNA files in FASTA format.
void fastaRead(string file)
{
    ifstream inputFile;
    inputFile.open(file);
    if (inputFile.is_open()) {
        vector<string> seqNames;
        vector<string> sequences;
        string currentSeq;
        string line;
        while (getline(inputFile, line))
        {
            if (line[0] == '>') {
                seqNames.push_back(line);
            }
        }
    }
    for( int i = 0; i < seqNames.size(); i++){
        cout << seqNames[i] << endl;
    }
    inputFile.close();
}

int main()
{
    string fileName;
    cout << "Enter the filename and path of the fasta file" << endl;
    getline(cin, fileName);
    cout << "The file name specified was: " << fileName << endl;
    fastaRead(fileName);
    return 0;
}

The function should go through a text file like the one below: 该函数应该通过如下文本文件:

Hello World!
>foo
bleep bleep
>nope

and identify the ones beginning with '>' and push them onto the vector seqNames and then report back the contents to the command line. 并标识以“>”开头的内容,并将其推入向量seqNames中,然后将内容报告回命令行。 - So I'm trying to write the ability to detect fast format heads. - 所以我正在尝试编写检测快速格式磁头的功能。 However when I compile I am told: 但是当我编译时,我被告知:

n95753:Desktop wardb$ g++ testfasta.cpp
testfasta.cpp:25:25: error: use of undeclared identifier 'seqNames'
    for( int i = 0; i < seqNames.size(); i++){
                        ^
testfasta.cpp:26:17: error: use of undeclared identifier 'seqNames'
        cout << seqNames[i] << endl;

However I'm pretty sure I declared the vector in the line: vector<string> seqNames; 但是我很确定我在行中声明了向量: vector<string> seqNames;

Thanks, Ben. 谢谢,本。

This is because you declared the vectors in the inner scope of the if . 这是因为你在if的内部范围内声明了向量。 You need to move the declaration out, so that your while loop can see them as well: 您需要移出声明,以便您的while循环也可以看到它们:

vector<string> seqNames;
vector<string> sequences;
if (inputFile.is_open()) {
    string currentSeq;
    string line;
    while (getline(inputFile, line))
    {
        if (line[0] == '>') {
            seqNames.push_back(line);
        }
    }
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}
if (inputFile.is_open()) {
    vector<string> seqNames;
    vector<string> sequences;
    ...
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}

defines seqNames in a scope of if statement. if语句的范围内定义seqNames After the if statement, the identifier seqNames is undefined. if语句之后,标识符seqNames未定义。 Define it earlier in a scope that covers both if and for . 在涵盖iffor的范围中更早地定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM