Understanding the Output Method of Variables Using Smart Pointers

Asked 1 years ago, Updated 1 years ago, 215 views

I want to print num1, num2 using std::cout.
I have the following codes (1) and (2), but I don't know how to write a for statement in this case.

structure Data{
std::shared_ptr<std::array<SomeData, 30>>ptr1;//(1)
std::shared_ptr<std::vector<std::vector<SomeData>>>ptr2;//(2)
};

structureSomeData{
int16_t num1;
int32_t num2;
SomeData():
num1(0),
num2(0){}
};

When using Equation (1)(2), what statements would be effective in outputting num1 and num2 values for the following functions?

void func(const Data&data_info)
{
    // Description
}

c++

2022-12-16 03:00

1 Answers

Smart pointers are defined to allow basic operations to be the same as regular pointers, so they can be written as follows:

void func(const Data&data_info)
{
  if(data_info.ptr1) {//null check
    for(const auto&some_data:*data_info.ptr1) {// Reverse Browse
      std::cout<<some_data.num1<<std::endl;
    }
  }
}


2022-12-16 05:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.