函数中的向量 - 如何返回

时间:2011-02-03 12:00:19

标签: c++ vector return-value return

我有一个应该从文件中逐行读取的函数,当一行不以'>'开头时,读取停止要么 ' '。它应该将行存储在vector中并返回它。
这是代码:

    #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;
    }

编译器(g ++)返回:

    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;
}

4 个答案:

答案 0 :(得分:10)

如果我理解你,你的getseq()应该返回一个字符串向量。因此你应该改变

string getseq(char * db_file)

vector<string> getseq(char * db_file)

如果你想在main()上打印它,你应该循环播放。

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;
     }
}

答案 1 :(得分:1)

你试图返回一个向量,你的方法必须返回字符串。 也许你必须将方法的签名改为

vector<string> getseq(char * db_file)

答案 2 :(得分:1)

好吧,你试图将一个向量作为字符串返回。这不起作用,因为它们是不同的类型,并且没有从一个到另一个定义的转换。您的函数的返回类型为string

解决方案1 ​​

在您的情况下,您可以将线附加到字符串而不是将它们添加到矢量?无论如何,您将结果用作字符串。

您可以将序号更改为string,并使用+=运算符向其附加数据。

解决方案2

您也可以将返回类型更改为vector<string>,但您需要循环播放这些项目并将其打印在main中。

vector<string> getseq(char * db_file)
{
    ...
    return seqs;
}

警告Lector:这将复制所有项目。如果你想避免这种情况,可以将向量作为函数的引用传递给它并添加到它。

使用迭代器循环非常简单:

// Get the strings as a vector. 
vector<string> seqs = getseq(argv[1]);

// This is just a matter of taste, we create an alias for the vector<string> iterator type.
typedef vector<string>:iterator_t string_iter;

// Loop till we hit the end of the vector.
for (string_iter i = seqs.begin(); i != seqs.end(); i++)
{
   cout << *i; // you could add endlines, commas here etc.
}

如果您想避免复制矢量,并且getseq所有字符串都会引用vector<string>

void getseq(char * db_file, vector<string> &seqs)
{
    ...
    // vector<string> seqs; this line is not needed anymore.

    ...
    // we don't need to return anything anymore
}

然后,您需要在主要内容中创建vector<string>,然后创建上面的代码:

// Get the strings as a vector. 
vector<string> seqs;   // Holds our strings.
getseq(argv[1], seqs); // 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_t 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;

评论后编辑

int main(int argc, char * argv[1])
{
    // This is what you need, sorry for the confusion. 
    // This copies the vector returned to seqs
    vector<string> seqs = getseq(argv[1]); 

    // 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;
}

答案 3 :(得分:1)

您的函数getseq被声明为返回std::string,但您尝试返回另一种类型的值 - std::vector - 因此您遇到了编译器错误。您需要返回std::string类型的变量(通过连接向量的元素创建)。

您的功能可能如下所示:

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; 
}

注意:从函数返回的字符串可能是一个非常大的对象,并且按值返回它可能很昂贵,因为在这种情况下实际返回的是该对象的副本(通过调用复制构造函数构造)。如果你的字符串被声明为 out参数,你只需填写函数内部就会更有效率:

void getseq(char* db_file, string& strSeqs);

string strSeqs;
getseq(argv[1], strSeqs);
cout << strSeqs << endl;