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.
Printable View
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.
In what circumstances do you want to add data? As part of a processing loop? As initial data? From a file?
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.
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.Quote:
Hi 2kaud,
In what circumstances do you want to add data? As part of a processing loop? As initial data? From a file?
Note: Sorry, if you find any mistakes in my question.
You can access using:
For readability, I sometimes do the following:Code:for (vector<SP>::const_iterator it4=it->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.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";
}
And a reference since you do not want to copy the vector.
Thanks for your help, Philip Nicoletti .
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;
}