1

I would like to iterate through a members of a struct. In other words my bigger struct vector have a smaller struct inside. I would like to access all of the inside structs which is small_strcut_subject in this case:

#include <iostream>
#include <vector>

#include "../common/myheader.h"

using namespace std;

struct small_struct {
    string name;

};

struct big_struct {
    struct small_struct small_struct_obj;
};


int main() {


    std::vector<big_struct> big_struct_obj;

    big_struct_obj.push_back(big_struct());

    big_struct_obj[0].small_struct_obj.name = "english";


    for (std::vector<big_struct>::iterator it = big_struct_obj.begin(); it != big_struct_obj.end(); ++it){

//      cout << big_struct_obj[*it].small_struct_obj.name << endl;
    }
}

There is question about how to iterate through a sttuct but if it is multiple struct such as mine, I coudnt find any solution.

Meric Ozcan
  • 678
  • 5
  • 25
  • Not super related, but I'd suggest having a look at range based for loops. They're essentially a bit of nice syntax sugar for a manual iterator loop. – George May 10 '19 at 06:29
  • I know that question my friend but it is not same, as you can also see, my question have multiple struct inside – Meric Ozcan May 10 '19 at 06:30
  • Sure it is, might look something like: `for( big_struct const& struc : big_struct_obj ) cout << struc.small_struct_obj.name;` – George May 10 '19 at 06:37

1 Answers1

1

You can just do:

  cout << it->small_struct_obj.name << endl;

If you know how to iterate through a vector of structures, all that remains is to access the members of the structure in the for loop and this can be done with the -> operator.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • should I delete the question? is it dublicate? – Meric Ozcan May 10 '19 at 06:24
  • Let me see if there is a duplicate. Then I will mark it as such. – P.W May 10 '19 at 06:25
  • man that question is not dublicate I know how to iterate through struct but I couldnt do it if there is struct inside struct!! – Meric Ozcan May 10 '19 at 06:38
  • Your answer is good, but you flaged my question as dublicate, but the questions you linked iterates only on one struct. My question have multiple struct inside!! – Meric Ozcan May 10 '19 at 06:40
  • @MericOzcan: I could not find an exact duplicate of your question. If you know how to iterate through a vector of structures, all that remains is to access the members of the structure which can be done with the `->` operator. I will add this info to the answer. – P.W May 10 '19 at 06:44
  • yes I tried but couldnt find a way. thats why I asked, I am a beginer at this point on c++, thats why I felt flagging questiona as dublicate was not right. Anyways I will set your answer as correct one. But mate why downvote my question!! it is still not dublicate, I saw many questions worst than this but got many vote ups! thats bad.. – Meric Ozcan May 10 '19 at 07:21
  • @MericOzcan: I did not downvote your question. People downvote for various reasons. Even my posts were downvoted on various occasions. Often, there is no direct correlation between the quality of a post and the votes it receives. – P.W May 10 '19 at 08:10