मुझे एक फ़ंक्शन मिला है जो फ़ाइल लाइन से लाइन लाइन से पढ़ना चाहिए, जब कोई पंक्ति '>' या '' 'से शुरू नहीं होती है तो पढ़ना बंद हो जाता है। इसे वेक्टर में लाइनों को स्टोर करना चाहिए और इसे वापस करना चाहिए।
फ़ंक्शन में वेक्टर - वापसी कैसे करें
#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;
}
संकलक (छ ++) देता है:
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`
कोई भी किसी भी विचार है
इस कोड है?
संपादित करें: Skurmendel से पूछते हैं, मैं स्मृति सुरक्षा उल्लंघन के कारण पूरे कोड जोड़ने हूँ के बाद
कोड संकलित क्रियान्वित के रूप में:
#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;
}
आप क्या वापस करना चाहते हैं: यह अधिक कुशल अगर आपके स्ट्रिंग बाहर पैरामीटरके रूप में घोषित किया जाता है, जो आपने अभी समारोह के अंदर भरने हो सकता है? वेक्टर या स्ट्रिंग? – SuperSaiyan
[मत] [http://stackoverflow.com/questions/4881210/how-to-convert-a-string-to-a-ifstream/4881251#4881251) [while] का उपयोग करें (http: // stackoverflow। कॉम/प्रश्न/4324441/getlinestreamobj-line-reads-last-line-multiple-times/4324667 # 4324667) (स्ट्रीम। [अच्छा] (http://stackoverflow.com/questions/4874530/fstream-unix-problem-in -reading/4874650 # 4874650)())। –
@ थ्रस्टमास्टर: वेक्टर @ फ्रेड नर्क: इसके बजाय मुझे और क्यों उपयोग करने की आवश्यकता है? –