【问题标题】:How can I return a vector from a function in c++?如何从 C++ 中的函数返回向量?
【发布时间】:2017-06-13 21:30:16
【问题描述】:
vector<double> function(vector<double> &X){
    vector<double> data2;
    for (int i = 0; i < X.size(); i++){
        data2.push_back(2*X[i]);
    }
    return data2;
}
int main(){
    vector<double> data;
    for (int i = 1; i <= 10; i++){
        data.push_back(i);
    }
    for (int i = 0; i < data.size(); i++){
        cout << function(data) << endl;
    }
return 0;
}

基本上,对于 data[i] = i 的给定人工创建的“数据”向量,我想要一个函数,它将向量的每个元素乘以 2,然后打印结果。但是,我似乎无法理解我做错了什么。

【问题讨论】:

  • 提示:function 的结果类型是什么,应该如何打印?
  • 你不能像这样打印std::vector。您需要遍历元素。
  • 这与从函数返回向量无关。您将得到相同的编译错误:std::vector&lt;v&gt;; cout &lt;&lt; v &lt;&lt; endl;。我敢肯定,在了解了这一点之后,您将没有任何问题,找出您的错误。
  • 当心,您的 function 名称与 std::function 冲突。至少出于可读性原因,请使用其他内容。

标签: c++ function vector


【解决方案1】:

function 返回一个std::vector,这是一种容器。而且我们不能使用std::cout来打印std::vector的元素。
我们应该进入容器获取元素并打印它们。
像这样:

data2 = function(data);
for (int i = 0; i < data2.size(); i++)
{
        cout << data2[i]<< endl;
}

【讨论】:

    【解决方案2】:

    首先,为了简单起见,让我们编写一个简单的函数来打印出向量的内容。现在,要查看向量的外观,我们只需调用该函数并查看它的外观即可。

    template <typename T>
    void printVector(const vector<T> &input){
        unsigned sz = input.size();
        cout<<"========"<<endl;
        for(unsigned i=0; i<sz; i++){
            cout<<input[i]<<endl;
        }
        cout<<"========"<<endl;
    }
    

    记住,如果一个函数接受一个泛型类的对象(例如std::vector),你需要指定它是一个模板函数。

    现在,回到你的问题。我假设您不想更改 data 本身的值,因为您声明了 data2

    那么您有两个选择(如果我理解您要解决的问题)。

    第一种选择是编写一个返回向量的函数。

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <typename T>
    vector<T> doublingFunc(const vector<T> &input){
        vector<T> output;
        /* it is good practice
           to catch the size of the vector
           once so that you aren't calling
           vector::size() each go through
           of the loop, but it is no big deal
        */
        unsigned sz = input.size();
        for(unsigned i=0; i<sz; i++){
            output.push_back(2*input[i]);
        }
        return output;
    }
    
    template <typename T>
    void printVector(const vector<T> &input){
        unsigned sz = input.size();
        cout<<"========"<<endl;
        for(unsigned i=0; i<sz; i++){
            cout<<input[i]<<endl;
        }
        cout<<"========"<<endl;
    }
    
    int main(){
        vector<double> data;
    
        for(int i=1; i<10; i++){
            data.push_back((double) i);
           //technically, the cast in unnecessary
        }
    
        printVector(data);
    
        vector<double> data2 = doublingFunc(data);
    
        printVector(data2);
    
        return 0;
    }
    

    第二种选择是编写一个函数,动态分配一个新向量,然后返回一个指向它的指针。

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <typename T>
    vector<T>* doublingFunc(const vector<T> &input){
        vector<T>* output = new vector<T>();
        /* it is good practice
           to catch the size of the vector
           once so that you aren't calling
           vector::size() each go through
           of the loop, but it is no big deal
        */
        unsigned sz = input.size();
        for(unsigned i=0; i<sz; i++){
            output->push_back(2*input[i]);
        }
        return output;
    }
    
    template <typename T>
    void printVector(const vector<T> &input){
        unsigned sz = input.size();
        cout<<"========"<<endl;
        for(unsigned i=0; i<sz; i++){
            cout<<input[i]<<endl;
        }
        cout<<"========"<<endl;
    }
    
    int main(){
        vector<double> data;
    
        for(int i=1; i<10; i++){
            data.push_back((double) i);
        }
    
        printVector(data);
    
        vector<double>* data2 = doublingFunc(data);
    
        /*this function takes a reference to a vector,
         so we need to dereference the pointer
       */
        printVector(*data2);
    
        //remember to delete dynamically allocated variables
        delete data2;
        return 0;
    }
    

    当然,如果你只想打印出向量中所有条目的两倍值,你可以使用函数:

    template <typename T>
    void printDouble(const vector<T> &input){
        unsigned sz = input.size();
        cout<<"========"<<endl;
        for(unsigned i=0; i<sz; i++){
            cout<<2 * input[i]<<endl;
        }
        cout<<"========"<<endl;
    }
    

    这对你来说可能是很多新东西,因为你似乎对 c++ 很陌生。我建议阅读this 网站。

    【讨论】:

      【解决方案3】:

      要让 std::cout 打印 std::vector,您必须为其重载流插入运算符 (

      例如

      std::ostream& operator << ( std::ostream& os, const std::vector<double>& v )
      {
          os << "{ ";
          for ( const auto& i : v )
          {
              os << i << ' ';
          }
          os << "}";
          return os;
      }
      

      要打印容器的内容,可以使用 C++11 range-for loop,如上例所示。

      你也可以对这个重载使用std::copy算法或者像这样直接使用:

      std::ostream& operator << ( std::ostream& os, const std::vector<double>& v )
      {
          os << "{ ";
          std::copy( begin(v), end(v), std::ostream_iterator<double>( os, " " ) );
          os << "}";
          return os;
      }
      

      在您的代码中,您可以简单地使用函数对向量的值进行操作,然后像这样打印它:

      void multiplyBy2( vector<double>& v )
      {
          for ( auto& i : v )
          {
              i *= 2;
          }
      }
      
      int main( void )
      {
          vector<double> v;
          // ...
          multiplyBy2( v );
          std::cout << v << std::endl;
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多