Looks like you have a set: {A,B} within another set: {{A,B},{B,C},{C,D}} within another set:
{{{A,B},{B,C},{C,D}},{{A,C},{C,E},{E,F},{F,P}}}
So you have a set of chars within a larger set within a larger set. Three levels of sets: So maybe three separate structs:
struct letterPair // bottom level
{
char firstLetter;
char secondLetter;
};
struct middleLevel
{
vector <letterPair> letterPairs;
};
struct topLevel
{
vector <middleLevel> outerSet;
};
That's one idea. There are other ways, obviously, but that is one way to organize it. Seems like each line of input would be one instance of middleLevel. Also looks like the first letter in a letterPair is the second letter in the previous letterPair, so your input file could work with this 3 levels of structs layout. Read the file into these structs, then go from there. I haven't worked with the boost libraries much so I'm not sure what "tokenizer" is, so I can't help you there. Anyway, this is just my two cents.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
If you mean something like this:
struct struct1
{
.
.
.
struct struct2
{
.
.
.
struct struct3
{
.
.
.
}
}
}
I think there may well be a way that is similar to the above layout, where there is a struct within a struct within a struct. However, I have never done it before so I don't know how. You could do this though:
vector < vector <letterPair> > outsideSet;
outsideSet.at (0).at (1).firstLetter = 'B';
outsideSet.at (0) would refer to the index 0 (i.e. first) set of pairs in your example:
{{A,B},{B,C},{C,D}}
outsideSet.at (0).at (1) would refer to the index 1 (i.e. second) set within that larger set:
{B,C}
outsideSet.at (0).at (1).firstLetter refers to 'B' within {A, B}.
The result is that they are in fact nested in the way that you refer to them.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
As suggested by you my current implementation look like
struct LetterPair
{
string First_element;
string second_element;
};
void createset()
{
..
....
vector<vector<LetterPair> > Outerset;
string Firstelement;
string Secondelement;
...
....
Outerset.push_back(Firstelement,Secondelement);
....
}
This is giving me compilation error which i m trying to insert the values of the elements what is the correct way to do this. Also let me know how do i find a particular elements pair of given index from this set.
Yeah, that's going to give you a compilation error. I see you changed LetterPair from characters to strings, which is fine, but not needed, at least not in the example you gave where each element is only one character in length.
You got an error because:
1) You can't push_back a pair of strings. You need to push_back a LetterPair.
2) You can't push_back a LetterPair onto Outerset You need to push_back a VECTOR of LetterPair onto Outerset.
Whether you define MiddleSet as a struct or not, you are still using it, even if you are only using it as a temporary variable. Here's something I wrote up. It compiled and ran without errors:
#include <vector>
#include <string>
using namespace std;
struct LetterPair
{
string First_element;
string second_element;
};
vector<vector<LetterPair> > Outerset;
void createset()
{
vector <LetterPair> MiddleSet;
LetterPair letterpair;
string Firstelement;
string Secondelement;
letterpair.First_element = "A";
letterpair.second_element = "B";
MiddleSet.push_back (letterpair);
letterpair.First_element = "B";
letterpair.second_element = "C";
MiddleSet.push_back (letterpair);
letterpair.First_element = "C";
letterpair.second_element = "D";
MiddleSet.push_back (letterpair);
Outerset.push_back (MiddleSet);
MiddleSet.clear ();
letterpair.First_element = "D";
letterpair.second_element = "E";
MiddleSet.push_back (letterpair);
letterpair.First_element = "E";
letterpair.second_element = "F";
MiddleSet.push_back (letterpair);
Outerset.push_back (MiddleSet);
MiddleSet.clear ();
}
int main ()
{
createset ();
return 0;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711