I've got a function that should read from file line by line, the reading stops when a line does not begin with '>' or ' '. It should store the lines in vector and return it.
This is code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
string getseq(char * db_file) // gets sequences from file
{
string seqdb;
vector<string> seqs;
ifstream ifs(db_file);
string line;
//vector<char> seqs[size/3];
while(ifs.good())
{
getline(ifs, seqdb);
if (seqdb[0] != '>' & seqdb[0]!=' ')
{
seqs.push_back(seqdb);
}
}
ifs.close();
//return seqs;
//return seqs;
}
int main(int argc, char * argv[1])
{
cout << "Sequences: \n" << getseq(argv[1]) << endl;
return 0;
}
Compiler (g++) returns:
fasta_parser.cpp: In function ‘std::string getseq(char*)’:
fasta_parser.cpp:32: error: conversion from ‘std::vector<std::basic_string<char, `std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’ to non-scalar type ‘std::string’ requested`
Anyone has any idea?
Edit: As Skurmendel ask, I am adding whole code because of memory security violation after
executing compiled code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std;
vector<string> getseq(char * db_file) // pobiera sekwencje z pliku
{
string seqdb;
vector<string> seqs;
ifstream ifs(db_file);
string line;
//vector<char> seqs[size/3];
while(ifs.good())
{
getline(ifs, seqdb);
if (seqdb[0] != '>' & seqdb[0]!=' ')
{
seqs.push_back(seqdb);
}
}
ifs.close();
return seqs;
}
int main(int argc, char * argv[1])
{
vector<string> seqs; // Holds our strings.
getseq(argv[1]); // We don't return anything.
// This is just a matter of taste, we create an alias for the vector<string> iterator type.
typedef vector<string>::iterator string_iter;
// Print prelude.
cout << "Sekwencje: \n";
// Loop till we hit the end of the vector.
for (string_iter i = seqs.begin(); i != seqs.end(); i++)
{
cout << *i << " "; // Do processing, add endlines, commas here etc.
}
cout << endl;
}
Vectors as return valuesYes, functions in C++ can return a value of type std::vector .
The C++ function std::vector::size() returns the number of elements present in the vector.
return vector<string> function The return Type of the function is a vector<string> so it has to return a vector of strings... all you do is create another vector<string> in main and asign it the function. Last edited by elad; 07-18-2005 at 09:24 AM. You're only born perfect.
vector::back()This function can be used to fetch the last element of a vector container.
If I understood you, your getseq() should return a vector of strings. Therefore you should change
string getseq(char * db_file)
to
vector<string> getseq(char * db_file)
And if you want to print it on main() you should do it in a loop.
int main() {
vector<string> str_vec = getseq(argv[1]);
for(vector<string>::iterator it = str_vec.begin(); it != str_vec.end(); it++) {
cout << *it << endl;
}
}
Your function getseq
is declared to return std::string
but you are trying to return value of another type - std::vector
- therefore you got that compiler error. You need to return variable of type std::string
(created by concatenating elements of your vector).
Your function could look like this:
string getseq(char* db_file)
{
string strSeqs;
vector<string> seqs;
... // fill the vector; manipulate with ifstream
for(vector<string>::iterator it = seqs.begin(); it != seqs.end(); ++it)
{
strSeqs += *it;
}
return strSeqs;
}
Note: string you are returning from a function can be quite big object and returning it by value can be expensive as what is actually returned in this case is a copy of that object (constructed by invoking copy constructor). It would be more efficient if your string is declared as out parameter which you just fill inside the function:
void getseq(char* db_file, string& strSeqs);
string strSeqs;
getseq(argv[1], strSeqs);
cout << strSeqs << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With