CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2013
    Posts
    12

    vectors with structure

    Hi,

    I have two structs like below,

    struct SP
    {
    string sp1;
    string sp2;
    };


    struct SL
    {
    string sl1;
    string sl2;
    string sl3[3];
    vector<SP> sl4;
    };

    vector<SL> slVect;


    Please suggest, How to add data to slVect ?

    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: vectors with structure

    Quote Originally Posted by srvolatile View Post
    Please suggest, How to add data to slVect ?
    Code:
    struct SP
    {
        string sp1;
        string sp2; 
    };
    
    
    struct SL
    {
        string sl1;
        string sl2;
        string sl3[3];  
        vector<SP> sl4;
    };
    
    vector<SL> slVect;
    
    SL slNew;
    slVect.push_back (slNew);
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: vectors with structure

    In what circumstances do you want to add data? As part of a processing loop? As initial data? From a file?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2013
    Posts
    12

    Re: vectors with structure

    Quote Originally Posted by srvolatile View Post
    Hi,

    Thanks for your help VictorN.

    SL slNew;
    slNew.sl1 = "S1";
    slNew.sl2 = "S2";
    slNew.sl3[0] = "S31";
    slNew.sl3[1] = "S32";
    slNew.sl3[2] = "S33";
    SP spNew;
    spNew.sp1 = "sp1";
    spNew.sp2 = "sp2";
    slNew.sl4.push_back(spNew);
    slVect.push_back (slNew);


    for (vector<SL>::iterator it = slVect.begin() ; it != slVect.end(); ++it)
    {
    cout << it->sl1 << "\n";
    cout << it->sl2 << "\n";
    cout << it->sl3[0] << "\n";
    cout << it->sl3[1] << "\n";
    cout << it->sl3[2] << "\n";
    };
    The above code output is as expected and I am facing trouble to access the data of slVect.sl4.
    Could you please suggest me, how to access the data from slVect.sl4 ? // I was getting error: "member is not available"
    Is it possible to add more data to slVect.sl4 (adding the data using push_back) at the slVect iterator 1 (I mean after the statement : slVect.push_back (slNew))?

    Thanks in advance.



    Hi 2kaud,
    In what circumstances do you want to add data? As part of a processing loop? As initial data? From a file?
    I am trying to understand STL Vectors and it isn't part of any project. Consider it as a initial data. Thanks for your reply.


    Note: Sorry, if you find any mistakes in my question.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: vectors with structure

    You can access using:

    Code:
    for (vector<SP>::const_iterator it4=it->sl4.begin(); it4!=sl4.end(); ++it4)
    {
          cout << it4->sp1 << " : " << it4->sp2 << "\n";
    }
    For readability, I sometimes do the following:

    Code:
    const vector<SP> & sl4 = it->sl4;  // note reference
    
    for (vector<SP>::const_iterator it4=sl4.begin(); it4!=sl4.end(); ++it4)
    {
          cout << it4->sp1 << " : " << it4->sp2 << "\n";
    }
    I made it a const (and const_iterator) since the vector is not being changed.
    And a reference since you do not want to copy the vector.

  6. #6
    Join Date
    Feb 2013
    Posts
    12

    Re: vectors with structure

    Thanks for your help, Philip Nicoletti .

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: vectors with structure

    If you are using c++11 then you can also simply the iterator access using auto and range based for statement. For initializing the vector if you have constructors for the struct it can make things a little easier and also you can overload the << operator for the structs. eg
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    using namespace std;
    
    struct SP
    {
    	string sp1;
    	string sp2;
    
    	SP(string s1, string s2) : sp1(s1), sp2(s2){}
    	SP(){}
    };
    
    ostream& operator <<(ostream& os, const SP& sp)
    {
    	return (os << sp.sp1 << " : " << sp.sp2 << endl);
    }
    
    typedef string ar3[3];
    typedef vector<SP> vsp;
    
    struct SL
    {
    	string sl1;
    	string sl2;
    	ar3 sl3;
    	vsp sl4;
    
    	SL() {}
    	SL(const string& s1, const string& s2, string s3[3], const vsp& s4) : sl1(s1), sl2(s2), sl4(s4) {
    		sl3[0] = s3[0];
    		sl3[1] = s3[1];
    		sl3[2] = s3[2];
    	}
    };
    
    ostream& operator<<(ostream& os, const SL& it)
    {
    	os << it.sl1 << "\n";
    	os << it.sl2 << "\n";
    	os << it.sl3[0] << "\n";
    	os << it.sl3[1] << "\n";
    	os << it.sl3[2] << "\n";
    
    	for (const auto& it4 : it.sl4)
    		os << it4;
    
    	return os;
    }
    
    int main()
    {
    	vector<SL> slVect;
    	vsp sp;
    	sp.push_back(SP("sp1", "sp2"));
    	sp.push_back(SP("sp3", "sp4"));
    	slVect.push_back(SL("s1", "s2", ar3{ "s30", "s31", "s32" }, sp));
    	
    	for (const auto& it : slVect)
    		cout << it;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured