在向量结构中显示向量结构

时间:2018-12-02 16:31:09

标签: c++ function vector struct iterator

我是一个真正的初学者,这对我来说真的很复杂。我一直在寻找答案,但我在这里找不到,或者如果我看过它,这似乎对我来说很复杂。

这就是我想要做的:

我有这个标题

#include <iostream>
#include <string>
#include <vector>

我有这个结构

struct stFecha {
    int dia;
    int mes;
    int ano;
};

struct stPersona {
    string cedula;
    string nombre;
    string apellido;
    stFecha fechaNacimiento;
    char estado;
};

struct stCuentaBancaria{
    string numeroCuenta;
    string nombreOficialBancario;
    double totalDebito;
    double totalCredito;
    vector<stPersona> clientesCuenta;
    char estado;

我声明了我将要使用的这些向量

vector<stPersona> clientes;
vector<stCuentaBancaria> cuentas;

这是我用来遍历结构并检查记录中是否已存在人员的代码。

for( vector<stPersona>::iterator it = clientes.begin();  !existe && it != clientes.end(); ++it )
{
    existe = cedula.compare( (*it).cedula ) == 0 ;
    if ( existe )
    {
        cout << "NOMBRE :"   << (*it).nombre   << '\n' 
             << "APELLIDO :" << (*it).apellido << '\n' 
             << "CEDULA :" << (*it).cedula << '\n'
             << "FECHA DE NACIMIENTO DD/MM/AAAA:\n"
             << "DIA: " << (*it).fechaNacimiento.dia << '\n'
             << "MES: " << (*it).fechaNacimiento.mes << '\n'
             << "A\xA5O: " << (*it).fechaNacimiento.ano << '\n'
             << "ESTADO: "<< (*it).estado << '\n';
    }

即使fechaNacimiento是一个结构,我也可以轻松访问该结构中的数据,因为它不是向量。

另一方面,在将新帐户添加到向量cuentas之前,我需要检查ID或cedula是否已注册到客户的clientes数据中。所以我正在使用以下代码查找记录是否存在。

stCuentaBancaria cuenta;
cout << "CEDULA DEL CLIENTE: ";
cin >> cedula;

bool existe = false;

for ( vector<stPersona>::iterator it = clientes.begin(); !existe && it != clientes.end(); ++it )
{
existe = cedula.compare( (*it).cedula ) == 0;
if ( existe )
{
    cuenta.clientesCuenta.push_back((*it));                             
}

从我的角度来看,应该将clientes类型为stPersona的记录复制到clientesCuenta以及结构{{中的结构stPersona 1}}代表银行帐户。到目前为止,我没有任何错误。

但是在这里我看不到如何让事情为我服务...

我希望查阅记录,当它找到所需的记录以在记录中显示数据时,但是当我这样做时,就像以前使用客户stCuentas的迭代器一样,此记录不起作用。它里面包含一个向量,给我一个错误

clientes

我尝试使用cout<<"\n\n2.CONSULTA POR CUENTA\n"; string cuenta; cout << "INTRODUCIR CUENTA A CONSULTAR .:"; cin >> cuenta; bool existe = false; for( vector<stCuentaBancaria>::iterator it = cuentas.begin(); !existe && it != cuentas.end(); ++it ) { existe = cuenta.compare( (*it).numeroCuenta ) == 0 ; if ( existe ) { cout << "NUMERO DE CUENTA :" << (*it).numeroCuenta << '\n' << "NOMBRE OFICIAL DE CUENTA :" << (*it).nombreOficialBancario << '\n' << "TOTAL DEBITO : " << (*it).totalDebito << '\n' << "TOTAL CREDITO: " << (*it).totalCredito << '\n' << "ESTADO: "<< (*it).estado << '\n' << "TUTORIALES DE CUENTA: " << (*it).clientesCuenta << '\n'; } ,但这是先前声明的向量(*it).clientesCuenta中的向量结构stPersonas

我不知道如何获得显示这些数据的权限,也不知道将来如何获得对其进行修改的权限。

请帮助。

附加说明:我正在通过函数访问此数据

cuentas

这就是我从主要功能发送数据的方式

int manejoCuentas(vector<stCuentaBancaria> &cuentas,vector<stPersona> &clientes, int &opcionMenu)

我的英语不太好,感谢您阅读本文,任何帮助都将非常受欢迎

2 个答案:

答案 0 :(得分:0)

重写函数以输出stCuentaBancaria向量

ostream&运算符<<(ostream&os,const vector&cuentas) for(int i = 0; i

答案 1 :(得分:0)

在运算符<<的三个重载之后,我们可以简化输出POD成员的代码。 由于stFechastPersonastCuentaBancaria都是POD类型,并且它们的成员都是公共的,因此我们不需要在它们上定义好友函数:

#include <ostream>

std::ostream& operator<<(std::ostream& o, const stFecha& fecha)
{
    o << "DIA   : " << fecha.dia  << '\n';
    o << "MES   : " << fecha.mes  << '\n';
    o << "A\\xA5O: " << fecha.ano;

    return o;
}

std::ostream& operator<<(std::ostream& o, const stPersona& persona)
{
    o << "NOMBRE   : " << persona.nombre   << '\n';
    o << "APELLIDO : " << persona.apellido << '\n';
    o << "CEDULA   : " << persona.cedula   << '\n';

    o << "FECHA DE NACIMIENTO DD/MM/AAAA:\n";
    o << persona.fechaNacimiento << '\n';

    o << "ESTADO   : " << persona.estado;

    return o;
}

std::ostream& operator<<(std::ostream& o, const stCuentaBancaria& bancaria)
{
    o << "NUMERO DE CUENTA         : " << bancaria.numeroCuenta          << '\n';
    o << "NOMBRE OFICIAL DE CUENTA : " << bancaria.nombreOficialBancario << '\n';
    o << "TOTAL DEBITO             : " <<bancaria.totalDebito            << '\n';
    o << "TOTAL CREDITO            : " << bancaria.totalCredito          << "\n\n";

    o << "TUTORIALES DE CUENTA\n"; 
    for(const auto& ceunta_i : bancaria.clientesCuenta){
        o << ceunta_i << "\n\n";
    }

    o << "ESTADO: "<< bancaria.estado;

    return o;
}

然后,您可以将std::vector<stCuentaBancaria> cuentas的每个元素的数据输出到std::cout或仅使用一个衬线的其他输出流,如下所示。

DEMO

std::cout << *it << std::endl;
相关问题