简体   繁体   English

C ++:将向量转移到子程序中

[英]C++: transfer a vector into a subprogram

In my example there are three similar vectors which I would like to print. 在我的示例中,我想打印三个相似的向量。 Could you help me understand how to transfer a vector into a subprogram so that not to repeat myself? 您能帮助我理解如何将向量转移到子程序中,以免重复我自己吗?

#include "stdafx.h";
#include <vector>;
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;


struct SPoint
{   
    int X;
    int Y;
};

vector<SPoint> points;
vector<SPoint> selected;
vector<SPoint> cleared;

void print_points()
{
    cout << "Points: "<< endl;
    for (int i = 0; i < points.size(); i++)
    {
        cout << '('<<points[i].X <<',' <<points[i].Y <<')'<< endl;
    }
    cout << endl;
}

void print_selected()
{
    cout << "Selected: "<< endl;
    for (int i = 0; i < selected.size(); i++)
    {
        cout << '('<<selected[i].X <<',' <<selected[i].Y <<')'<< endl;      
    }
    cout << endl;
}

void print_cleared()
{
    cout << "Cleared: "<< endl;
    for (int i = 0; i < cleared.size(); i++)
    {
        cout << '('<<cleared[i].X <<',' <<cleared[i].Y <<')'<< endl;        
    }
    cout << endl;
}



int main ()
{
    SPoint temp = {0, 0};

    for (int i = 0; i < 11;i++)
    {
        temp.X = i;
        temp.Y = i;
        points.push_back(temp);

    }

    for (int i = 5; i< 11;i++)
    {
        temp.X = i;
        temp.Y = i;
        points.push_back(temp);
    }

    print_points();
    print_selected();
    print_cleared();

    system ("pause");

  return 0;
}

You could do something like this: 您可以执行以下操作:

void
print(const std::vector<SPoint>& vect, const std::string& message)
{
    std::cout << message << ":" << std::endl;
    for (int i = 0, size = vect.size(); i < size; ++i)
        std::cout << vect[i].X << ":" << vector[i].Y << " ";
    std::endl;
}

print(points, "Points");
print(points, "Selected");
print(points, "Cleared");

Good luck 祝好运

To pass a vector as an argument to a function you do something like this: 要将向量作为参数传递给函数,您需要执行以下操作:

void func(const vector<SPoint>& points) {
  ... do stuff
}

Then you call the function in you code like this: 然后在代码中调用函数,如下所示:

  ...some stuff
  vector<SPoint> a;
  func(a);

Just use a const reference to a vector and pass it to the function: 只需对向量使用const引用并将其传递给函数:

void print(const vector<SPoint> &data) const {

}

...

print(points);

Here is a full C++ style approach: 这是完整的C ++样式方法:

struct SPoint
{   
    int X;
    int Y;
};

std::ostream& operator <<( std::ostream& stream, SPoint const& point )
{
    stream << '(' << point.X << ',' <<point.Y << ')';
    return stream;
}

void print_vector( std::ostream& stream, std::vector< SPoint > const& vector )
{
    std::copy(
        points.begin(), points.end()
      , std::ostream_iterator< SPoint >( std::cout, '\n' )
    );
}

and then: 接着:

print_vector( std::cout, points );
print_vector( std::cout, selected );
print_vector( std::cout, cleared );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM