c++ - 在 vector 结构中显示 vector 结构

标签 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

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是一个结构,我可以轻松访问此结构中的数据,因为它不是 vector 。

另一方面,在我向 vector 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 中找到的记录这是类型 stPersonaclientesCuenta这也是一个结构 stPersona在结构内 stCuentas代表银行账户。到目前为止,我没有收到任何错误。

但这里是我看不到如何让事情为我工作的地方......

我想查阅记录,当它找到所需的记录以显示记录中的数据时,但当我这样做时,就像以前一样使用客户的迭代器 clientes ,这个不行。它包含一个 vector ,它给我一个错误

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但这是 vector 结构 stPersonas vector 内 cuentas先前声明。

我不知道如何获得显示此数据的权限,也不知道将来如果找到它如何获得修改它的权限。

请帮忙。

补充说明:我正在通过函数访问这些数据

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

这就是我从主函数发送数据的方式

manejoCuentas(cuentas, clientes, opcion);

我的英语不是很好感谢阅读本文,我们非常欢迎任何帮助

最佳答案

定义运算符的以下三个重载 << ,我们可以简化输出 POD 成员的代码。 自 stFecha , stPersonastCuentaBancaria都是 POD 类型,而且它们的成员都是 public,我们不需要在它们上面定义友元函数:

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

关于c++ - 在 vector 结构中显示 vector 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582288/

相关文章:

c++ - 使用 C++ 将数组的数组插入 mongodb

c++ - Qt 无法正确设置 centralWidget()

swift - 函数会导致循环引用吗?

python - 如何创建一个空的 R 向量来添加新项目

r - 如何从向量或列表中消除 n 个连续值?

c++ - 如何在属性表中捕获 "tab changed"事件

c++ - ld 通过参数返回 1 个退出状态错误

c - 函数指针执行错误

c++ - 在 C++ 中调用函数

C++11:在 push_back()、clear() 之后,vector.size() 不会在 Eclipse 调试观察器中更新