Search vector by line number

I've been working on a program that stores the data from a fasta file to a vector. The user can then search the vector by searching for a line number and the program will output the header line and the sequence line that comes beneath the header line. My code is nowhere near finished so I apologize in advance for that. My question is, how would I be able to search by line number as opposed to line content. For example if I type "55", the program will output ">55" and then the sequence beneath it. Right now, I have to type in the whole header line to search for it.
Here's a portion of my input file (fasta file):
seq1
GGGCCCAU-A

>se2
CAUUAGAU-G

>seq3
GCGCCCGU-A

>seq4
GCGGAAGU-A

>5
GUGGACGU-G
>6
GCCGGGGU-G
>7
GGGGCCUU-A

>8
GGUCCGGU-A
>9
GCGGGUGU-A
>seq10
GGGCCUGU-A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  //Homework #5 - Fasta File Reader Program
//Anneliese Schorpp-Dettmann

#include <iostream>       //directives
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>


using namespace std;

int main (){                                                                            //main program begins
ifstream fastafile;                                                                     //variables
string  content, line, filename;
vector <string> myvector;
string name;
int seqLength=0;
vector <string>:: iterator it;

 cout<<"Input fasta file name: " << endl;                                               //prompt user to input a fasta file name
   getline(cin, filename);

  fastafile.open(filename.c_str ());                                                    //open fasta file
  while (!fastafile.is_open ()) {

	  cout <<"Fasta file could not be opened. Session terminated."<<endl;

	  exit (EXIT_FAILURE);                                                              //ends program if fasta file cannot be opened
	  break;
  }


  while(getline (fastafile, line)) {
	  
     seqLength= line.length();
	 
		 fastafile >> line;                                                             //store data from fasta file to vector
	                      
	myvector.push_back(line);
	
	cout <<line<<endl; 

  }
 
cout<<"Select sequence number."<<endl;                                                  //prompt user to enter a sequence number
getline (cin, name);
it = find (myvector.begin(), myvector.end (), name);
if (it != myvector.end()) {
	
	cout<<"The name of your molecule is:"<<endl;                                       
	cout<<*it<<endl;
}
else {
	cout <<"The name of the sequence number you entered could not be found."<<endl;
	exit (EXIT_FAILURE);                                                                
}



cout<<"The sequence of your molecule is:"<<endl;





cout<<"The length of your sequence is:"<<seqLength<<endl;


cout<<"There are a total of "<< myvector.size ()/2 <<" sequences in your alignment."<<endl;
 


   return 0; 
   }
What does the "line number" actually mean and what is the formal description of this "fasta" format?
OP: a std::map might be more appropriate in this case with 55, as in your example, stored as the key and the sequence beneath it as the value
Hello naegleria73,

If you want to extract from the vector by using a number you could usemyvector[x] with 'x' being a subscript just as you would use with an array. Otherwise the search that you have will work.

Hope that helps,

Andy
Topic archived. No new replies allowed.